Stuck with the code

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?

cat is short for category.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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?
			}
			else if (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
}
In your addGrade function you have:
1
2
3
4
5
6
    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
Topic archived. No new replies allowed.