Hi,
This is my first post in this forum and it is a 'help me' post :)
Anyway, I took a beginners class in C Programming and at the completion of the class we were assigned to do a project. The project is designing a billing system for a supermarket. Note : I am not here to ask you guys to do my project for me, that would be cheating :P

So, I have a question about some of the functions the program should do.
We are supposed to create a text file to store all the records of the stocks and quantities etc. of the supermarket. Now for adding these stocks, i have used this code,

void add()
{
clrscr();
int item_code = 0;
char item_name[80];
int qihand;
int item_quantity;
float item_cost;
printf("Addition Page Test");
FILE *quantity;
if((quantity = fopen("C:\\TEST\\XYZ\\quantity.dat","a+"))==NULL)
item_code=1;
else
{
do{
fscanf(quantity,"\n %i %[^/]%*c %i %f", &item_code, &item_name, &qihand, &item_cost);
} while (!feof(quantity));
item_code+=1;
}

printf("\nItem Code : %i", item_code);
printf("\nEnter Item Name :");
gets(item_name);
printf("\nEnter The Quantity :");
scanf ("%i",&qihand);
printf("\nEnter The Price Per Unit :");
scanf("%f",&item_cost);
printf("\nRecord Added To Database");
fprintf(quantity, "\n %i %s %i %.2f", item_code, item_name, qihand, item_cost);
fflush(stdin);
getch();
fclose(quantity);

menu();
}

Now, this actually works good until i get to the 3rd item to be added on to my text file. The thing is eventhough from what I understand, the new "item_code" should automatically be updated according to the last "item_code" that is present in the text file when entering a new record, it just stays at "2" no matter how many items i enter. Also, all the records gets saved in the text file fine.

So could someone help me get this numbering thing flowing.. it maybe a small tiny line i maybe missing due to my amaturishness...

Also, I was wondering if there was any easier way to use to add records to a text file.

Thanks alot for the help ! Its greatly appreciated.

first off, start indenting your code blocks.

you're setting yourself up for failure if you cant easily differentiate where nested conditional blocks start and stop.

it's also annoying as hell to try and read someone else's code who doesn't indent.

void add()
{
   clrscr();
   int item_code = 0;
   char item_name[80];
   int qihand;
   int item_quantity;
   float item_cost;
   
   printf("Addition Page Test");
   FILE *quantity;
   
   if((quantity = fopen("C:\\TEST\\XYZ\\quantity.dat","a+"))==NULL)
      item_code=1;
   else
   {
      do
      {
         fscanf(quantity,"\n %i %[^/]%*c %i %f", &item_code, &item_name, &qihand, &item_cost);
      } 
      while (!feof(quantity));
      item_code+=1;
   }
   
   printf("\nItem Code : %i", item_code);
   printf("\nEnter Item Name :");
   gets(item_name);
   printf("\nEnter The Quantity :");
   scanf ("%i",&qihand);
   printf("\nEnter The Price Per Unit :");
   scanf("%f",&item_cost);
   printf("\nRecord Added To Database");
   fprintf(quantity, "\n %i %s %i %.2f", item_code, item_name, qihand, item_cost);
   fflush(stdin);
   getch();
   
   fclose(quantity);
   
   menu();
}

now off the bat, line 20 looks suspicious. i can't be certain because i have no idea what your text file looks like, but putting the '\n' in front may cause trouble. its unusual style at least. but maybe it works okay. i cant run it since i dont have your text file to test against.

anyhow, as to your method ... i think a better (as in simpler, and more consistent) way would be to open the file for "r" read permission only, use a conditional while loop to "fgets" read each line and actually look at the "item code" field ... keeping track of the highest value read, and possibly allow some checking to handle irregular sequences found.

then when you want to append the entered value, close the file and reopen it for "a" append and just write the next line to it using fprintf, with the highest "item code" value that you found earlier, plus 1.

thats just my offhanded opinion, without actually running any code.

okay, i see that line 34, you also have the '\n' in front, so your scheme might work in this case. But I would suggest that you cease this habit of putting the '\n' in front of your strings right now, and develop the habit of always putting your newline characters at the end of the string.

It might sound petty, but I'm certain this will eventually cause you problems, and sooner rather than later. especially reading and writing to files. (There are some cases when formatting output to a terminal, that you might want to put one or more newlines at the start of the string ...but that's the exception rather than the rule.)

also, before i forget:

NEVER use "fflush(stdin)"

it's wrong. in fact, it's meaningless. if it "works" for you right now, then your compiler is wrong, and if you start relying on it in your programming, you will have your ass handed to you.

learn instead to handle your input stream correctly in the first place an you never will need the concept of FFLUSH STDIN... whatever the hell such a concept even means. i truly don't know.

.

About the line \n, it was bad and I've removed it. Thanks for the heads up.

Also about fflush(stdin), i've been tought to use to to flush any 'extra' info that maybe accidentally added to any variable inorder to get a correct input, but since that is bad practice, i will have to learn to handle them correctly like you said :)

As for the indented code blocks, I am sorry. But my compiler and editor doesn't automatically indent the code, and WE were told specifically to use that old compiler for this project for some mysterious reason.

anyhow, as to your method ... i think a better (as in simpler, and more consistent) way would be to open the file for "r" read permission only, use a conditional while loop to "fgets" read each line and actually look at the "item code" field ... keeping track of the highest value read, and possibly allow some checking to handle irregular sequences found.

then when you want to append the entered value, close the file and reopen it for "a" append and just write the next line to it using fprintf, with the highest "item code" value that you found earlier, plus 1.

I'll try this method as soon as i get home. Thanks for the help :)

Also about fflush(stdin), i've been tought to use to to flush any 'extra' info that maybe accidentally added to any variable inorder to get a correct input, but since that is bad practice, i will have to learn to handle them correctly like you said :)

its not just bad practice, its totally wrong. it's not me who's saying it either, it's in the ANSI C standard.

look, you can flush an output buffer; it sends the buffered data to the stdout device (terminal) and so those bytes are "processed" or "handled" correctly. the only change is in the timing: you manually forced them out of the buffer before it would have naturally gone on it's own.

there's no analogue to flushing an input buffer. Where will you flush it to? Back out through the stdin (keyboard) device? Will the keys somehow untype themselves? Of course not, but the other option is even worse: instead of processing these bytes correctly (such as fflush(stdout) does), it just deletes them?? Whatever you want to say about these bytes, they are still part of the user input. Who's to say if they are valid or invalid, when you don't even know what they are? the bytes need to be handled correctly, not blindly ignored.

ANSI C specifically states that you can not fflush a stdin buffer, and that such an event is undefined and not intended to be performed with the fflush command.

If your compiler supports such a concept, it's *wrong*. If your instructor tells you to do such a thing, he's *wrong*. Do yourself a favor, right now, and stop using it. If/when you have unexpected input in the buffer, find out why and learn to handle it correctly.

As for the indented code blocks, I am sorry. But my compiler and editor doesn't automatically indent the code, and WE were told specifically to use that old compiler for this project for some mysterious reason.

more evidence supporting my newly-developing theory that your instructor is demented.

regardless, just because the IDE editor doesn't auto-indent, doesn't mean YOU don't have to indent. Good lord, man, do it manually! What do you think people did before there were integrated development environments? They typed shit out in plain vanilla text editors or monochrome terminals, and sometimes they even had to use spaces to indent.

It's certainly not a good reason to refuse to indent your code here. There are only but a few rules to this forum, and since people give free assistance here, the least you guys can do is follow them. And they're not arbitrary: non-indented code is a real bitch to read or debug.. People who keep posting such will basically be ignored, and just won't receive any help. Your choice

im also surprised your editor doesnt auto-indent. theres probably an option/setting that needs to be enabled, to turn that option on. in any event, being tied to a compiller doesn't mean you have to use the editor. use any code editor with syntactical highlighting. Crimson Editor and TextPad are a couple, there are certainly more.


.

I am learning a lot after I entered this forum. Thanks a lot, and then why so harsh, I am scared reading this.

y'all have to excuse me, mmkay? i been off my meds for a couple months now.


.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.