Valhalla Legends Forums Archive | C/C++ Programming | Problem with While Statement

AuthorMessageTime
Mitosis
Alright guys I am having problems with this, see I am trying to make it so my application wont shut off. This is the code I got.

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
int choice;
while (choice = (-81) )
cout << "What is 9*9?" << endl;
cin >> choice;
if (choice == 81)
cout << "Congradulations!" << endl;
else
cout << "Sorry incorrect, try again" << endl;
system("PAUSE");   
return 0;
}

Then when I compile it will just keep saying "What is 9*9?". Spamming that on and on. Compile it and you will see what I mean. Any advice on how to make this work, would/will be apprecaited.
January 12, 2004, 12:03 AM
iago
There are a lot of problems there.

Firstly, you're missing [ code] [/code ] tags.

Second, that while isn't doing anything.
- to compare choice to 81, you have to use ==, not =
- you need to have a beginning and end for the body of the while loop, { and }

Next, you should have an if OR a while loop, since a while loop is very similar to the idea of an if. So either this:
[code]
cin >> choice;
if(choice == -81)
{
// output right
}
else
{
// output wrong
}
//end
[/code]

or, in a loop,
[code]
cin >> choice;

while(choice != -81) // loop as long as choice is NOT equal to -81
{
// output wrong here
cin >> choice;
}
// output right here
[/code]

or, with a do..while loop:
[code]
do
{
// ask for the number
cin >> choice;
}
while(choice != -81); // loop as long as choice isn't -81
// output right
[/code]

January 12, 2004, 12:13 AM
Kp
[quote author=iago link=board=30;threadid=4691;start=0#msg39319 date=1073866437]
Second, that while isn't doing anything.
- to compare choice to 81, you have to use ==, not =
- you need to have a beginning and end for the body of the while loop, { and }[/quote]

Actually, it's doing quite a bit, and he does not need braces (at least not in the example replacements you gave - his code is so messy I have no idea what he's trying to do :)). As written, the code assigns -81 to choice, then, if the result is not zero (and it never is), runs the next statement, which is the cout. After having printed the line, it assigns choice again and, since it is still not zero (and never will be), prints the line again. Hence the spam.
January 12, 2004, 12:17 AM
Mitosis
[quote author=Kp link=board=30;threadid=4691;start=0#msg39323 date=1073866670]
[quote author=iago link=board=30;threadid=4691;start=0#msg39319 date=1073866437]
Second, that while isn't doing anything.
- to compare choice to 81, you have to use ==, not =
- you need to have a beginning and end for the body of the while loop, { and }[/quote]

Actually, it's doing quite a bit, and he does not need braces (at least not in the example replacements you gave - his code is so messy I have no idea what he's trying to do :)). As written, the code assigns -81 to choice, then, if the result is not zero (and it never is), runs the next statement, which is the cout. After having printed the line, it assigns choice again and, since it is still not zero (and never will be), prints the line again. Hence the spam.
[/quote]

Im trying to make it so that when you give the right answer or the wrong one, the program wont shut off. I want it to keep going so you can do another question or something else. Like instead of picking one thing, then it tells you what it was and the next time you press something the program shuts down. I dont want that. I wanna try and make it so it stays exacuted.
January 12, 2004, 12:40 AM
Moonshine
use
[code]
while (1) {
// ...
}

or alternatively

for (;;) {
// ...
}[/code]

These two loops are basically saying "Loop forever until the programme terminates (via return,exit,an exception..etc) or a break; is reached."
January 12, 2004, 2:43 AM
iago
[quote author=Moonshine link=board=30;threadid=4691;start=0#msg39350 date=1073875435]
use
[code]
while (1) {
// ...
}

or alternatively

for (;;) {
// ...
}[/code]

These two loops are basically saying "Loop forever until the programme terminates (via return,exit,an exception..etc) or a break; is reached."
[/quote]

That's not a particularely good way to do things, especially if it's only a part of a program.
January 12, 2004, 3:58 AM
Arta
Nah. It's often ok. For the main loop in a program, for example, or when you have multiple conditions that should cause the loop to break but that don't nest nicely.
January 12, 2004, 5:58 AM
Mitosis
Alright, I tried those and the application is just plain blank. You cant type in anything at all. It simply does nothing. Is the loop supposed to be in a certain place?
January 12, 2004, 9:43 PM
K
[code]
#include <iostream>
#include <cstdlib>
#include <string>

using std::string;
using std::cout;
using std::cin;
using std::endl;

int main(int argc, char *argv[])
{
int choice;

while(1)
{
cout << "what is 9 * 9? " << endl;
cin >> choice;

if (choice == 9 * 9)
{
cout << "correct!" << endl;
break;
}
else
{
cout << "sorry, try again." << endl;
}
}

cout << "press enter to exit." << endl;

string sCatch;

getline(cin, sCatch);

return 0;
}
[/code]
January 13, 2004, 1:17 AM

Search