Hi, I have this problem with my program where when I execute it, the program does not fully run. My intro and catCalculate functions work fine but after addGrade function runs and stops taking inputs, the program goes to c.proceed and stops taking inputs. What is wrong with this code? Also, how would I demand the program to run again using a new set of variables to add to my previous calculations?
int main()
{
compute c;
c.intro(cin,cout);
c.catCalculate(cin, cout);
c.addGrade(cin, cout);
c.proceed(cin, cout);
return 0;
}
void compute::addGrade(istream& in, ostream& out)
{
int count = 0;
compute temp;
in >> temp.gradeValue; //used to compute single grading assignments to add up to gradeValue
while (!in.fail())
{
char c = in.peek();
if (isalpha(c))
{
in.clear();
break; ///////////////////SOMETHING IS WRONG WITH THIS FUNCTION, WHILE LOOP DOES COMPUTE HOWEVER
in.ignore(1000, '\n');
}
else
{
gradeValue = gradeValue + temp.gradeValue;
in >> temp.gradeValue;
count++;
}
} //input grade values until the user is supposed to stop
total = ((gradeValue) / (count * 100)) * 100;
} //end calculating grade for a category
void compute::proceed(istream& in, ostream& out)
{
out << "Are there more categories? Type 'Y' to continue or 'N' to stop." << endl;
in >> reply; ////////////SOMETHING IS WRONG WITH THIS FUNCTION, DOES NOT WAIT FOR A REPLY
//if the user continues, then the process will be continued again and
//the functions will be called again
try
{
if (reply != 'Y' || reply != 'y' || reply != 'n' || reply != 'N')
throw 2;
while (!in.fail())
{
if (reply == 'Y' || reply == 'y')
{
catCalculate(in, out);
addGrade(in,out); /////////how do I rerun the program?
}
elseif (reply == 'N' || reply == 'n')
{
if (catCount == 1)
{
out << "There is only " << catCount << "category inputted." << endl;
out << category << " is worth " << catPercent << " out of a hundred." << endl;
out << "Your grade in this class is: " << total << endl << endl << endl;
PressToContinue();
}
else
{
out << "There are " << catCount << " categories inputted." << endl;
out << "Your grade in this class is: " << total << endl;
PressToContinue();
}
}
}
} //end try statement
catch (int error)
{
if (error == 2)
out << "Enter 'Y' or 'y' to continue or 'N' or 'n' to stop calculating." << endl;
} //end catch statement
}
while (!in.fail()) {
char c = in.peek();
if (isalpha(c)) {
in.clear();
break;
in.ignore(1000, '\n');
When loop is finished, you have either stream in failed state (if loop terminated by loop condition) so all subsequent inputs fails; or at least one character still sitting in input buffer, so next input operation reads it.
I thought in.clear() would eliminate the in failed state? So does my code have a logic error then? I forgot to mention I requested the user for my program to input "stop" when done inputting grades. So I thought once the program sees a letter, such as 's', the the while loop would stop and proceed to the next program.
I thought in.clear() would eliminate the in failed state?
It won't, because that branch of code doesn't get executed when in.fail() == true
You need to do it after the while loop, something like:
1 2 3 4 5 6 7 8
while (!in.fail())
{
// here, in.fail() == false
...
}
// we only get here because in.fail() == true, so do something about it
...
in.ignore( numeric_limits<streamsize>::max(), '\n');
Also, I don't know if clearing the input stream gets rid of the stuff in your input buffer