I'm attempting to create a program that reads from a text file and carries out functions on values stored in different account numbers(represented by numbers 1-5) as well as using a character next to these account numbers to carry out certain functions(i.e. "1 D 200.00" signifies a 200 value addition to the value of account 1). The text file is formated in this manner
20000.00
10000.00
2500.00
67500.00
15000.00
2 W 200.00
4 D 50.00
1 B
2 U
3 W 125.00
The first 5 values are scanned and are then used to signify the initial value of the accounts 1-5. Then each subsequent line read is meant to signify an action that is to be carried out on these now stored values. For example the line that reads "2 W 200.00" is to target account 2, use a withdraw function on this account, and use the float to modify the function( so 200.00 is subtracted from the value of account 2).
As a way to test that the values are being scanned and stored in the proper fashion, i'm first having the program run through a loop where it scans all of the values after the first 5 lines and print them. This is the code:
FILE*bankfile;
bankfile = fopen("bankfile.txt","r+");
for(i = 1; i < 15; i++)
{
if(i < 6)
{
fscanf(bankfile,"%f",&accValue);
switch(i)
{
case 1:
acc1 = accValue;
break;
case 2:
acc2 = accValue;
break;
case 3:
acc3 = accValue;
break;
case 4:
acc4 = accValue;
break;
case 5:
acc5 = accValue;
break;
}
}
else
{
fscanf(bankfile,"%d %c %f",&accNum,&accAction,&actValue);
printf("%d %c %f\n",accNum,accAction,actValue);
}
}
printf("Exited Loop");
When I run this code, the first two lines print as I would expect, but once the first line is reached that doesn't contain the float value that is being scanned, the program will go on to print the same value over and over until the loop is broken. This is what is printed:
2 W 200.00
4 D 50.00
1 B 2.00
1 B 2.00
1 B 2.00
etc until the program is finished.
Is there some way to get this program to recognize that a value for actValue does not exist for that line and the continue on to the next line?