The input file will have the name of a lake on one line, then a comma, then the volume of that lake in units of hundreds of cubic miles. The next line will contain the name of another lake, a comma, then it's volume, etc, etc. Each line of the file contains exactly the name of a lake, a comma, and a float value for volume.The name of a lake may contain more than one word; for example "dead horse lake", but it will be on one formatted line. The volume may have decimals in it, as 16.9. Your program will use a subroutine that takes as arguments the name of a lake and its volume. This subroutine will then print out the name of the lake on one line to the screen, followed by a set of consecutive asterisks denoting the volume in units of hundreds of cubic miles on the next line rounded to the nearest hundred cubic miles. For example, if the lake is 15.6 cubic miles in volume, the subroutine will print out 16 asterisks on the line following the name of the lake.
Right now my program only reads the first line and displays the name and asterisk, but any other information in my lakes.txt file is not read and the program terminates. Could someone please tell me what I am doing wrong? The original project was one without the comma, but the prof. decided to add a comma in there. I just changed the %19 to %20 and added a comma in the brackets. I do not know what difference it made, but it worked. I would like to understand why.
I'm new to SO. Sorry if my text is a little off. I'm a new programmer and I would really love to understand and become a good programmer. The more elaborate and in depth your answers are, the more helpful they are to me. Thank you for your help!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
void asterisks_code( char lake[257], float vol );
int main()
{
char lake[257], tempt[100];
float vol;
FILE *fp;
fp = fopen( "lakes.txt", "r" );
if( fp == NULL) {//starts 1st if statement
printf( "File does not exist.");
return 0;
}//closes 1st if statement
while( ( fgets( tempt, sizeof (tempt), fp ) ) != NULL ) {
if (sscanf(tempt, " %19[A-Za-z, ]%f", lake, &vol) != 2) {//starts 2nd if statement
fprintf(stderr, "Unexpected data\n");
break;
}//closes 2nd if statement
asterisks_code( lake, vol );
}//closes while loop
fclose( fp );
return 0;
}//closes main function
void asterisks_code( char lake[257], float vol )
{//start of asterisks_code function
int counter;
printf( "%s\n", lake );
for( counter = 0; counter < roundf(vol); counter++ ) {//start of for loop
printf( "*" );
}//closes for loop
printf( "\n" );
}//closes asterisk_code function