Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie programmer with a big problem

Status
Not open for further replies.

Moscro

Programmer
Joined
Dec 10, 2003
Messages
2
Location
US
I have a problem with a loop that I can't seem to resolve. The code is as follows:

#include <iostream.h>
#include <process.h>

int main()
{
float grade1;
float grade2;
float grade3;
float grade4;
float grade5;
float average;
bool decide1;
bool decide2;
bool decide3;
bool decide4;
bool decide5;

do
{
cout << &quot;Please enter the student's first grade\n&quot;;
cin >> grade1;
decide1 = 1;
decide1 = ((grade1 >= 0) && (grade1 < 101));
}
while (decide1 != 1);

do
{
cout << &quot;Please enter the student's second grade\n&quot;;
cin >> grade2;
decide2 = 1;
decide2 = ((grade2 >=0) && (grade2 < 101));
}
while (decide2 != 1);

do
{
cout << &quot;Please enter the student's third grade\n&quot;;
cin >> grade3;
decide3 = 1;
decide3 = ((grade3 >=0) && (grade3 < 101));
}
while (decide3 != 1);

do
{
cout << &quot;Please enter the student's fourth grade\n&quot;;
cin >> grade4;
decide4 = 1;
decide4 = ((grade4 >= 0) && (grade4 < 101));
}
while (decide4 != 1);

do
{
cout << &quot;Please enter the student's fifth grade\n&quot;;
cin >> grade5;
decide5 = 1;
decide5 = ((grade5 >= 0) && (grade5 < 101));
}
while (decide5 != 1);

cout << &quot;You entered &quot; << grade1 << &quot; &quot; << grade2 << &quot; &quot; << grade3 << &quot; &quot; << grade4 << &quot; &quot; << grade5 << &quot;\n&quot;;
average = (grade1+grade2+grade3+grade4+grade5)/5;

cout << &quot;The student's average is &quot; << average << &quot;\n&quot;;
return 0;
}


Obviously this is a simple grade averaging program, but my problem resides within the do-while loops. If, for example, you were to enter a 'd' (without apostrophes), I get an infinite loop. Logically, it should ask the question again, and await input. I put the extra bool declarations in after the cins just before the program makes a decision about the boolian variables to try to fix the program, but it still has no effect. Anybody have any suggestions?
Thanks for your help!
Moscro
 
I believe you should learn how to use debuggers.

Ion Filipski
1c.bmp
 
Yes, Ion is right...
Let's go to study in C++ style. You have bool(ean) values. So write simply
Code:
while(!decide)
or
Code:
until(decide)
, why &quot;==1&quot; or &quot;==0&quot;? Type bool constants are true and false. There are conversions true to 1 and false to 0 but it's redundand scripting in this case.
Make a simplest fuction to input grade value from cin then use it in all loops. Try number for prompting may be this func parameter. Have you any objective? If so, make a function - so simple rule...
Your grade values have float type. Is d a float constant, eh?..
And use C++ standard header <iostream>, not old <iostream.h>. And what for you include <process.h>?..
 
When you try to input a character into an integer, it causes an error on cin. After that, cin won't work properly unless you clear the error.
 
Ok, thanks all for responding.

Ion: I've attempted to use the debugger, but am still very illiterate. I can't keep up with the steps, and see what's wrong at the same time.

ArkM: I would rather not open new functions. If I can help it, i'm going to try to resolve this within just one function. The 'd' is an example of a mistake that a user may make, they could enter any letter possible, all I want it to do is ask the question again in a do-while loop inside of main().

chipper: I was thinking that this was a possibility, and in another example of the program (at school) I reset both the bool and float variables to 1 before it asked the &quot;cout << &quot;Please enter ...&quot; again at the top of the 'do'. If I enter anything incorrect from the beginning after taking this step, it skips ALL of the 'do' loops, and reports that I entered '0'. I also tried a version of this program with a 'goto' loop instead of 'do-while' loops. I found that the same results were present. Is it the compiler? Should I try a new one?

Moscro

 
I think you misread me. I never said anything about reassigning values to the input character. That's completely unnecessary.

I said you have to clear the errors from cin. Whenever cin, the object, expects to find an integer and finds something that doesn't look like an integer, it goes into a state of error. From then on, cin is not usable until you clear its error bits.

i.e.
Code:
cin.clear();

There's nothing wrong with your compiler or your library. The compiler wouldn't really matter for this, and any iostreams library would have the same effects. What you're seeing is the correct behavior.


You might consider ArkM's suggestion a bit more. If you look at your code, you'll see a lot of redundancy and a variable for each loop you go through. That's not very good coding.

This is not a scalable solution; if you ever want to add input loops, you have to add a new variable and rewrite the loop code. Separating this input routine into a function and making that control variable local to your function is much better practice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top