# Loopy Problem!!!

• 03-19-2002
icscou
Loopy Problem!!!
I'm doing a program for school and this piece of code won't work...

// This function gets the user to input the percent grade and returns the
// rounded version of that value.
{

// This variable is the percent grade and is input by the user. It is set to 150
// so the 'while' loop will function correctly.
float per_grd=150;

// This variable hold the value of the rounded percent grade.
int round_grd;

// This loop lets the user re-enter the percentage grade if it is invalid.
while (per_grd>100 || per_grd <0)
{

// Gets the user to input the percentage grade.
cin >> per_grd;

// The following checks it make sure the input is valid and if it is not, it
// displays an error message.
if (per_grd>100 || per_grd <0)
{

// Prints an error message to the screen, gets the user to press
// any key to continue then clears the screen. The loop then restarts.
printf ("\nTHAT IS AND INVALID ENTRY...PLEASE RE-ENTER!\n\n");
system ("PAUSE");
system ("CLS");

}

else
{

// Calls the round function then assigns the value to the 'round_grd'
// function.
round_grd=per_grd_round(per_grd);

}

}

// Returns the value of the rounded grade.
return round_grd;
}

... When you enter a letter the loop works, but it won't let you re-enter the value and just loops continuously.

Thanx, Ian

PS. I have attached the full code... please take a look and reply...
• 03-19-2002
JasonLikesJava
I'm not too sure of the problem... :(

Maybe you could state your while loop as while(1) and then use a break statement in your if statement to exit the loop.

Here is an easier way to round a number I think just FYI (you'll have to include math.c)

int round(double x)
{
if (ceil(x)-x <= x-floor(x))
{
return (int)ceil(x);
}
else
{
return (int)floor(x);
}
}
• 03-19-2002
swoopy
Try this:

// This loop lets the user re-enter the percentage grade if it is invalid.
while (per_grd>100 || per_grd <0)
{

// Gets the user to input the percentage grade.
cin >> per_grd;

if (cin.fail())
{
cin.clear();
cin.seekg(0);
per_grd = 150;
}

// The following checks it make sure the input is valid and if it is not, it
// displays an error message.
if (per_grd>100 || per_grd <0)
{

// Prints an error message to the screen, gets the user to press
// any key to continue then clears the screen. The loop then restarts.
printf ("\nTHAT IS AND INVALID ENTRY...PLEASE RE-ENTER!\n\n");
system ("PAUSE");
system ("CLS");

}

else
{

// Calls the round function then assigns the value to the 'round_grd'
// function.
round_grd=per_grd_round(per_grd);

}

}
• 03-19-2002
Brian
Quote:

Originally posted by JasonLikesJava

Maybe you could state your while loop as while(1) and then use a break statement in your if statement to exit the loop.

That's called bad programming...It's hard to look through code with gotos and breaks.
• 03-19-2002
JasonLikesJava
What???? I've never heard that a break is a bad programming technique.

Anyways, in the code it just seemed strange that the while and if say the same thing so it seemed clearer to me to use break.

I agree with that gotos are bad and have never used them since I left programming on my TI-83 during Algebra I (never really learned to use them since they aren't needed).
• 03-19-2002
Prelude
>What???? I've never heard that a break is a bad programming technique.
Not a bad programming technique, but it does indicate bad design. What break/continue/goto do is jump locally, this defeats the purpose of a structured program which should start at the beginning, and move logically to the end. And using infinite loops with break actually makes for longer code.
Code:

```int more = getFirst(); while ( more ) {   // Do stuff   more = getMore(); }```
Code:

```if ( getFirst() )   while ( 1 ) {     // Do stuff     if ( !getMore() )       break;   }```
Which is easier to follow, and which is shorter? Programmers who use infinite loops and breaks think they are making the program shorter and easier to understand when it actually is the opposite.

-Prelude
• 03-19-2002
JasonLikesJava
Right, in the second case it is shorter... in this case, but I don't think that is the case in the originally posted program.
• 03-19-2002
JasonLikesJava
Strike that.... FIRST case is shorter... but you know what I mean. :D
• 03-19-2002
Prelude
>but I don't think that is the case in the originally posted program
Probably because it wasn't designed properly. If you just throw code together without thinking about it then an infinite loop will be shortest, but if you redesign the program with structure in mind then without an infinite loop it will be shorter.

-Prelude