-
help! many problems
Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <windows.h>
using namespace std;
int main()
{
int counter; //While statement counter
int numberOfTests;
int studentTestTotal;
int testScore; //Test score from file
float studentAverage;
float classAverage;
string studentName; //Student Name
string fileName;
ifstream inFile;
cout << "Please enter the filename to retrieve scores from: ";
getline(cin,fileName);
inFile.open(fileName.c_str());
if (!inFile)
{
cout << "File failed to open";
}
inFile >> numberOfTests;
while (!inFile.eof())
{
inFile >> studentName;
cout << studentName;
studentTestTotal = 0;
studentAverage = 0;
while (counter <= numberOfTests)
{
inFile >> testScore;
cout << testScore;
studentTestTotal = studentTestTotal + testScore;
counter++;
}
cout << endl;
}
inFile.close();
system("pause");
return 0;
}
This is what i have so far. Basically I need it to read in names and numbers from a file and output them. Then it needs to calculate the average for each person's score and give them a letter grade. Finally it needs to take all of the averages and calculate a class average.
1) I do not reset the counter at all... Yet it compiles and works perfectly. If i reset the counter it doesn't.... WHY!?
2) when i use inFile to input the names and scores... it puts them on a separate line. How can i get them to go on one line.
3) if i add this line
Code:
studentAverage = studentTestTotal / numberOfTests;
the program once again does not work. It is driving me crazy help is greatly appreciated
-
I noticed a few people have looked at this topic. Any solutions advice suggestions anything would be appreciated...
-
1. Where were you planning to reset counter?
2. This sentence makes no sense.
3. Define "does not work".
-
Well yes to my knowledge I would have to reset counter but in this case it works without me doing it... idk why.
What i meant by the second statment is. I want the scores to appear like this eventually
Name score1 score2 score 3 average grade
but now it comes out like this
name
score1
score2
score3
thirdly.
Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <windows.h>
using namespace std;
int main()
{
int counter; //While statement counter
int numberOfTests;
int studentTestTotal;
int testScore; //Test score from file
float studentAverage;
float classAverage;
string studentName; //Student Name
string fileName;
ifstream inFile;
cout << "Please enter the filename to retrieve scores from: ";
getline(cin,fileName);
inFile.open(fileName.c_str());
if (!inFile)
{
cout << "File failed to open";
}
inFile >> numberOfTests;
while (!inFile.eof())
{
inFile >> studentName;
cout << studentName;
studentTestTotal = 0;
studentAverage = 0;
while (counter <= numberOfTests)
{
inFile >> testScore;
cout << testScore;
studentTestTotal = studentTestTotal + testScore;
counter++;
}
studentAverage = (studentTestTotal/numberOfTests);
cout << studentAverage;
cout << endl;
}
inFile.close();
system("pause");
return 0;
}
it add a zero to EVERY single line. If you were available to chat in AIM maybe I would greatly appreciate it.
-
1. Not resetting counter "works" if you only have one student in the file. If you have more than one, it will not read future students. (Or to be more specific, it will read every other piece of data in the file as a name, not as a grade.)
2. That has nothing to do with inFile << and everything to do with you printing out new lines (see above).
3. This is still back to 1 above -- you're not reading in any tests, consequently all the averages are zero.
-
ooo ok the first part makes sense... So how would I go about making it read them as tests making the division work? simply reset the counter? where would that be exactly? Is there anyway we could chat in a IM?
-
You need to reset the counter when you want the counter to go back to zero (i.e. when you are starting a new student). Note also that you are reading one too many numbers for your tests (i.e., if number of tests is 4, you will try to read 5 numbers).
-
DOH! gah thank you tabstop! you are amazing. My counter was my problem. I wasn't thinking it started at 0 and not 1. I appreciate it greatly.
-
crap one more quick question tab. Say i want to add headers to this went i output
How would I do the headers because one file might have 3 test scores while the other may have 4?
-
Put the "Test" part of the header in a loop.
-
o so basically like
Code:
while ( x < numberOfTests )
{
cout << "test" << x;
}
? correcto
-
Can i align the scores anyway? so the output looks decent
-
I got it just using setw()
Thanks for all the help tab
-
Code:
studentAverage = studentTestTotal / numberOfTests;
Let's see...
(float) = (int) / (int)
Something look funny there?