Reputation: 9
Having trouble with the second if statement to only read ten values. If I remove the program will compile. I need it in for the assignment I have tried several modifications, so what is here may not reflect my last attempt. C++ homework compile error with if statement. Details how many more details do I need to add before this will post, more details
#include <iostream>
#include <fstream>
using namespace std;
/**********************************************************************
* Get File
* This function will prompt the user for the name of the file and
* return it.
*********************************************************************/
void getFileName(char fileName[])
{
//prompt the user
cout << "Please enter the filename: ";
cin >> filename;
ofstream fout(fileName);
}
/**********************************************************************
* Read
* This function will read the file and return the average score of the
* ten values.
*********************************************************************/
float readFile(char fileName[])
{
cout.setf(ios::fixed); //no scientific notation
cout.precision(0); //no decimals
//In case of error
ifstream fin(filename);
if (fin.fail())
{
cout << "Error reading file \"" << fileName << "\"" << endl;
exit(0);
}
float data; //always need a variable to store the data
float i = 0; //don't forget to initialize the variable
float gradeSum = 0;
float average;
//Add up all the grades
while (fin >> data) //while there was not an error
{
gradeSum += data; //do the work
i++;
}
// Hint: Make sure you only read ten values.
if (i != 10)
{
//tell the user what happened
cout < "Error reading file \"" << fileName << "\"" << endl;
exit(0);
}
//Calculate average grade
average = gradeSum / i;
//Close file
fin.close();
return average;
}
/**********************************************************************
* Display function
* This function will display the average score to zero decimals of
* accuracy.
*********************************************************************/
void display(float average)
{
cout << "Average Grade: " << average << "%" << endl;
}
/**********************************************************************
* main calls average grade and file name
***********************************************************************/
int main()
{
char fileName[256];
getFileName(fileName);
int average = readFile(fileName);
return 0;
}
Upvotes: 0
Views: 110
Reputation: 523
your cout
is invalid. cout << "Average Grade: " << average << "%" << endl;
instead of "cout < Average Grade: " << average << "%" << endl;
Upvotes: 0
Reputation: 104569
Simple typo with regards to the stream operator.
This line:
cout < "Error reading file \"" << fileName << "\"" << endl;
Should be:
cout << "Error reading file \"" << fileName << "\"" << endl;
Also, you have other typos preventing your code from compiling: namely the mixed case usage of filename
and fileName
throughout. After I fixed those variable namings to be consistently fileName
, it compiled.
Upvotes: 1
Reputation: 1321
Note the position of your if statement in relation to your while loop. When does your if statement run? When does your while loop run?
With these in mind, how would you adjust your while loop to take into consideration your restriction (while loop stops executing when i = 10)?
I hope this helps you better understand what you need to do without spoiling too much of the answer.
P.S. The contents of the if block will quit the program on the spot if i != 10, without executing anything beyond.
if (i != 10)
{
//tell the user what happened
cout < "Error reading file \"" << fileName << "\"" << endl;
exit(0);
}
Upvotes: 2