问题
I'm having trouble getting my cin loop to terminate in my program. My program uses Linux redirection to read in input from the file hw07data, the data file look like this:
100 20 50 100 40 -1
A34F 90 15 50 99 32 -1
N12O 80 15 34 90 22 -1
The first portion is the total points for the class, the next lines are the students ID number followed by their scores, all terminated by -1.
My issue: my while loop never terminates when i run the command ./a.out < hw07data, could anyone look over my code and give me some hints? I dont want the answer, as this is homework, i just need some guidance. Thanks!!
#include <iostream>
#include <iomanip>
using namespace std;
const int SENTINEL = -1; //signal to end part of file
const int LTAB = 8; //left tab
const int RTAB = 13; //right tab
int main()
{
cout << "Grant Mercer Assignment 7 Section 1002\n\n";
cout << setprecision(2) << fixed << showpoint;
cout << left << setw(LTAB) << "ID CODE" <<
right << setw(RTAB) << "POINTS" <<
setw(RTAB) << "PCT" << setw(RTAB) <<
"GRADE" << endl;
double Percentage, //holds students percentage
AvgPercentage;
int Earnedpoints, //earned points for specific student
Totalpoints, //total points possible for all students
AvgPoints, //average points for all students
NumClass; //counts the number of students
Totalpoints = Earnedpoints = //set all vals equal to zero
AvgPoints = AvgPercentage = Percentage = NumClass = 0;
//first and last char for studentID
char Fchar,Lchar, Num1, Num2, Grade;
int TmpVal = 0; //temporary value
cin >> TmpVal;
while(TmpVal != -1) //reading in TOTAL POINTS
{
Totalpoints += TmpVal; //add scores onto each other
cin >> TmpVal; //read in next value
}
while(cin) //WHILE LOOP ISSUE HERE!
{
//read in student initials
cin >> Fchar >> Num1 >> Num2 >> Lchar >> TmpVal;
while(TmpVal != -1)
{
Earnedpoints += TmpVal; //read in points earned
cin >> TmpVal;
}
//calculate percentage
Percentage = ((double)Earnedpoints / Totalpoints) * 100;
AvgPercentage += Percentage;
NumClass++;
if(Percentage >= 90) //determine grade for student
Grade = 'A';
else if(Percentage >= 80 && Percentage < 90)
Grade = 'B';
else if(Percentage >= 70 && Percentage < 80)
Grade = 'C';
else if(Percentage >= 60 && Percentage < 70)
Grade = 'D';
else if(Percentage < 60)
Grade = 'F';
//display information on student
cout << left << Fchar << Num1 << Num2 << setw(LTAB) << Lchar << right << setw(RTAB-3) << Earnedpoints <<
setw(RTAB) << Percentage << setw(RTAB) << Grade << endl;
TmpVal = Earnedpoints = 0;
}
AvgPercentage /= NumClass;
cout << endl << left << setw(LTAB+20) << "Class size: " << right << setw(RTAB) << NumClass << endl;
cout << left << setw(LTAB+20) << "Total points possible: " << right << setw(RTAB) << Totalpoints << endl;
cout << left << setw(LTAB+20) << "Average point total: " << right << setw(RTAB) << AvgPoints << endl;
cout << left << setw(LTAB+20) << "Average percentage: " << right << setw(RTAB) << AvgPercentage << endl;
}
The output continues to ask for new input.
回答1:
You may find answer there How to read until EOF from cin in C++
So you can read cin
line by line using getline
and parse the resulting lines, like that:
#include <iostream>
#include <sstream>
#include <string>
int main() {
int a, b;
std::string line;
while (std::getline(std::cin, line)) {
std::stringstream stream(line);
stream >> a >> b;
std::cout << "a: " << a << " - b: " << b << std::endl;
}
return 0;
}
EDIT: Do not forget to check parsing results and stream state for any failure!
回答2:
You should always check that you input was successful:
if (std::cin >> TmpVal) {
// do simething with the read value
}
else {
// deal with failed input
}
In case of a failure you night want to check eof()
inducating that the failure was due to having reached the end of the input.
To deal with errors, have a look at std::istream::clear()
and std::istream::ignore()
.
来源:https://stackoverflow.com/questions/13031414/cin-loop-never-terminating