Best way to do a loop with numbers 1,2,3

Started by
7 comments, last by graveyard filla 19 years, 7 months ago
hi I've been programming for a few days and i wanted to try a loop with the numbers 1,2,3 this is all i can think of

#include<iostream>

using namespace std;
int main()

int num;
{
        cout<<"enter a 1,2 or 3 to quit\n";
    if(num=1)
        cout<<"now enter 2 or 3 to quit\n";
    if(num=2)
        cout<<"now enter 1 or 3 to quit\n";
    if(num=3)
       cin>>num;
    return 0;
}
but i know that its wrong.
-----------------------------------Panic and anxiety Disorder HQ
Advertisement
what exactly are you trying to do? Have them entered in sequence? Have them all entered? Have just one entered? What is the goal of the code.

I am trying to figure out the flow of your code, and it seems pretty wishy-washy...which is completely understandable for a few day old coder. If you could give us a little more information on what you want, I am sure we can help you out no problem!
What I want to do is make a never ending loop where you keep pressing 1 and 2 or 3 to exit the program.
-----------------------------------Panic and anxiety Disorder HQ
homework?

-me
No just playing around with c++ :) .
-----------------------------------Panic and anxiety Disorder HQ
Still not enough info.

Present us what you want in the way you want the flow of the program to go. For example, if you want the user to have to press 1, 2, and then 3 to quit, tell us just that.

If you want them to press just one of the above to quit, tell us. What do they need to hit...EXACTLY...to quit.

Think about how you want it executed, then try writing the code.
Quote:Original post by hothead
What I want to do is make a never ending loop where you keep pressing 1 and 2 or 3 to exit the program.


To make a loop, you first need a loop statement, like for, while or do/while. Look that up.

Additionally, in C++, local variable definitions do not go in between the function name and the function's braces (hey, it's not Pascal, OK?), so move your int num; into the block.

Finally, beware, in C++, num=1 is an assignment, which will evaluate to whatever value you assigned (1 is non-zero, thus true and the 'test' always succeeds), num==1 is a test, which is probably what you want.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
#include <iostream>int main(){   int input;   do{      cout << "Input 1, 2, or 3.  3 will quit: ";      cin >> input;   }while(input != 3)   cout << "You input 3!  Quitting!"   return 1;}


That is a do/while loop. It always runs atleast once. You get the user input, and if it does not equal 3, it keeps looping to get input again. If it does equal three, the while loop quits, and it outputs the user input 3, and quits.

Now be wary of what Fruny said. He made some good points.
i think he just wanted a sort of "mind game", you put in 1, they ask for 2 or 3, you put in 2, it asks for 1 or 3, etc.. it just keeps on making you go in circles. this is how to do it:

#include <iostream>using namespace std;int main(){   bool done = false;   int num;   cout << "Enter 1, 2, or 3" <<endl;   cin >> num;   while(!done)   {      if(num == 1)      {         cout << " Enter 2 or 3 "<<endl;         cin >> num;      }      else if(num == 2)      {         cout << " Enter a 1 or 3 "<<endl;         cin >> num;      }      else      {         cout<< " Enter a 1 or 2 "<<endl;         cin >> num;      }   }      return 0;}
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement