-1

I am reading from a file (containing numbers seperated by newline) and calculating the amount. The problem is while reading the file and storing the amount in array it is skipping the first letter of the first amount, eg:- if the first line has 324, then it will read 24 and if the line has 3 then it will get junk vale but rest of the line is fine.

The code is

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
static int den[9]={1000,500,100,50,20,10,5,2,1};
void main()
{
 clrscr();
 unsigned long long amt[9];
 unsigned long long temp=0,total=0;
 int c=0,i=0,j=0,gd=DETECT,gm,x=66,y=22,font=8;
 fflush(stdin);
 FILE *fp;
 fp=fopen("OPENCASH.TXT","r");
 while((c=getc(fp))!=EOF)
 {
  fscanf(fp,"%llu",&amt[i]);
  i++;
 }
 initgraph(&gd,&gm,"C:\\TC\\BGI");
 settextstyle(font,HORIZ_DIR,1);
 printf(" \t     ");
 for (i=0;j<50;j++)
 {
  printf("_");
 }
 outtextxy(x, y, "   The Closing Cash Denomination");
 printf("\n\n\n\t   ");
 for (i=0;i<50;i++)
 {
  printf("_");
 }
 font=5;
 settextstyle(font,HORIZ_DIR,1);
 x=30;
 y=87;
 outtextxy(x,y,"1000");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,7);
 printf("%llu",amt[0]);
 outtextxy(205,--y,"=");
 temp=den[0]*amt[0];
 total=total+temp;
 gotoxy(35,7);
 printf("%llu",temp);
 ++y;
 font=5;
 x=42;
 y=y+34;
 outtextxy(x,y,"500");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,9);
 printf("%llu",amt[1]);
 outtextxy(205,--y,"=");
 temp=den[1]*amt[1];
 total=total+temp;
 gotoxy(35,9);
 printf("%llu",temp);
 ++y;
 font=5;
 y=y+32;
 outtextxy(x,y,"100");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,11);
 printf("%llu",amt[2]);
 outtextxy(205,--y,"=");
 temp=den[2]*amt[2];
 total=total+temp;
 gotoxy(35,11);
 printf("%llu",temp);
 ++y;
 font=5;
 x=54;
 y=y+31;
 outtextxy(x,y,"50");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,13);
 printf("%llu",amt[3]);
 outtextxy(205,--y,"=");
 temp=den[3]*amt[3];
 total=total+temp;
 gotoxy(35,13);
 printf("%llu",temp);
 ++y;
 font=5;
 y=y+31;
 outtextxy(x,y,"20");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,15);
 printf("%llu",amt[4]);
 outtextxy(205,--y,"=");
 temp=den[4]*amt[4];
 total=total+temp;
 gotoxy(35,15);
 printf("%llu",temp);
 ++y;
 font=5;
 y=y+34;
 outtextxy(x,y,"10");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,17);
 printf("%llu",amt[5]);
 outtextxy(205,--y,"=");
 temp=den[5]*amt[5];
 total=total+temp;
 gotoxy(35,17);
 printf("%llu",temp);
 ++y;
 font=5;
 x=64;
 y=y+32;
 outtextxy(x,y,"5");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,19);
 printf("%llu",amt[6]);
 outtextxy(205,--y,"=");
 temp=den[6]*amt[6];
 total=total+temp;
 gotoxy(35,19);
 printf("%llu",temp);
 ++y;
 font=5;
 y=y+31;
 outtextxy(x,y,"2");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,21);
 printf("%llu",amt[7]);
 outtextxy(205,--y,"=");
 temp=den[7]*amt[7];
 total=total+temp;
 gotoxy(35,21);
 printf("%llu",temp);
 ++y;
 font=5;
 y=y+31;
 outtextxy(x,y,"1");
 font=6;
 settextstyle(font,HORIZ_DIR,1);
 outtextxy(104,y,"X");
 gotoxy(20,23);
 printf("%llu",amt[8]);
 outtextxy(205,--y,"=");
 temp=den[8]*amt[8];
 total=total+temp;
 gotoxy(35,23);
 printf("%llu",temp);
 y=y+9;
 outtextxy(264,y,"___________");
 y=y+27;
 outtextxy(150,y,"Total Cash");
 gotoxy(35,25);
 printf("%llu",total);
 getch();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139

3 Answers3

1

The while loop is wrong,

while((c=getc(fp))!=EOF)
 {
  fscanf(fp,"%llu",&amt[i]);
  i++;
 }

getc() consumes a character,

test as follows,

 while(fscanf(fp,"%llu",&amt[i]) > 0)
 {
    i++;
 }

You can also use the function ungetc() to cancel the effects of getc().

Deepu
  • 7,592
  • 4
  • 25
  • 47
1

There are numerous serious problems with this code.

Obvious bugs:

  • The while loop, as others have already pointed out. Replace it with a check of the result of fscanf. Do not use feof for this.
  • fflush cannot be used on stdin or any other input stream. This invokes undefined behavior and your program might crash or behave in unexpected ways.
  • Your program will only work on a system that has the compiler installed. You need to add the BGI library object file to your project.
  • You don't check if the file was successfully opened or not.
  • You don't call fclose so your program creates resource leaks and needlessly keeps files open.

Non-standard gibberish:

  • unsigned long long was not supported by the C language before the C99 standard, which your crap compiler does not support. Whatever long long means on your compiler, it might be non-portable. Likewise, the %ll format specifier is not well-defined for your compiler.
  • Since this is for a hosted system (DOS), you must declare main as int main (void) or your code will not compile on a conforming C compiler.
  • Since main always returns 0, you cannot omit the return 0 statement on a hosted system. This rule was relaxed in C99, but since your compiler does not conform to the C99 standard (or indeed any C standard), you must always have return 0 at the end.

Other issues:

  • Don't use "magic numbers" all over the place, use constants. Either const int or #defines.
  • Your indention is obscure and unreadable. 2 or 4 spaces is convention.
  • You must use functions and modular programming. This is just one big unreadable mess.
  • You never call closegraph(). Whether this creates resource leaks or not, I'm not sure.
  • Mixing printf and outtextxy on the same screen will look like crap. But then again, this is for DOS so maybe crap is desired.
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I will try all the above flaws to correctly implement. **AND** Please help me out to install or insert**BGI library object file to your project.** – Vipul Sonker Apr 10 '13 at 03:03
  • @VipulSonker From what I remember, you create a new project then add a file from the Turbo C library called `egavga.bgi`. It is an object file of sorts that you can link together with your project. If I remember correctly, you can copy it into your project folder. – Lundin Apr 10 '13 at 06:22
0

The first character is consumed by the getc function.

You can change your reading loop as follow:

while(!feof(fp)) {
    fscanf(fp,"%llu",&amt[i]);
    i++;
}
Bechir
  • 987
  • 10
  • 26