• Welcome to Valhalla Legends Archive.
 

Problem with While Statement

Started by Mitosis, January 11, 2004, 06:03 PM

Previous topic - Next topic

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.

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:

cin >> choice;
if(choice == -81)
{
// output right
}
else
{
 // output wrong
}
//end


or, in a loop,

cin >> choice;

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


or, with a do..while loop:

do
{
 // ask for the number
 cin >> choice;
}
while(choice != -81); // loop as long as choice isn't -81
// output right


This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Kp

Quote from: iago on January 11, 2004, 06:13 PM
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 }

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Mitosis

Quote from: Kp on January 11, 2004, 06:17 PM
Quote from: iago on January 11, 2004, 06:13 PM
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 }

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.

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.

Moonshine

use

while (1) {
// ...
}

or alternatively

for (;;) {
 // ...
}


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

iago

Quote from: Moonshine on January 11, 2004, 08:43 PM
use

while (1) {
// ...
}

or alternatively

for (;;) {
 // ...
}


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

That's not a particularely good way to do things, especially if it's only a part of a program.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


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.

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?

K


#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;
}