Help with DO while loop?
Dec 9, 2013 at 11:15pm UTC
I am new with do while loops, meaning I am fine with others... but my do while loop wont 'loop' or wont start over. It's like it isn't there. Say I have this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
srand(time(NULL));
string his_choice, your_choice;
int num = rand() % 3 + 1;
cout<<"Rock, Paper, Scissors" <<endl;
cout<<endl<<"Enter 'exit' to exit game at anytime" <<endl;
if (num == 1)
his_choice = "ROCK" ;
else if (num == 2)
his_choice = "PAPER" ;
else if (num == 3)
his_choice = "SCISSORS" ;
do {
cout<<"Well, What do you choose?" <<endl;
cin>>your_choice;
cout<<his_choice<<endl;
if (your_choice == "rock" || his_choice == "SCISSORS" )
cout<<"YOU WIN THIS ROUND!" <<endl;
else if (your_choice == "paper" || his_choice == "ROCK" )
cout<<"YOU WIN" <<endl;
else if (your_choice == "scissors" || his_choice == "PAPER" )
cout<<"YOU WIN!" <<endl;
else if (your_choice == "scissors" || his_choice == "ROCK" )
cout<<"YOU LOSE!" <<endl;
else if (your_choice == "rock" || his_choice == "PAPER" )
cout<<"YOU LOSE!!!" <<endl;
else if (your_choice == "paper" || his_choice == "SCISSORS" )
cout<<"YOU LOSE!!!" <<endl;
}while (your_choice == "exit" );
}
it doesn't even loop. It just runs right through the 'do' and 'while' and achiles brackets. Anyway to fix this?
Last edited on Dec 9, 2013 at 11:16pm UTC
Dec 9, 2013 at 11:19pm UTC
Type exit
when your prompt from line 25 appears and see what happens. :)
Last edited on Dec 9, 2013 at 11:19pm UTC
Dec 9, 2013 at 11:38pm UTC
change the:
while (your_choice == "exit"
to
while (your_choice != "exit"
Dec 10, 2013 at 12:04am UTC
OOOHHHH!!! Yeah I remember seeing that somewhere. I used to only use do while loops and then started with for loops. I forgot entirely on how to do this. Thanks for the refresher!
Topic archived. No new replies allowed.