Question:

Need help with a loop

by  |  earlier

0 LIKES UnLike

include <iostream>

using namespace std;

int main ()

{

char one, two, p, s, r, P, R, S, ans, y, Y;

do

{

cout << "Do you want to play a Rock, Paper or Scissors Game?\n";

cin >> ans;

if ((ans == 'y')|| (ans == 'Y'))

{

cout << "Player 1 choose R, P or S.\n";

cin >> one;

cout << "Player 2 choose R, P or S.\n";

cin >> two;

if ((one == 'p' || one == 'P') & (two == 's' || two == 'S'))

cout << "Player 2 wins Scissor cuts paper" << endl;

else if ((one == 'p' || one == 'P') & (two == 'r' || two == 'R'))

cout << "Player 1 wins Paper covers Rock" << endl;

else if ((one == 's' || one == 'S') & (two == 'r' || two == 'R'))

cout << "Player 2 wins Rock destroys scissors" << endl;

else if ((one == 's' || one == 'S') & (two == 'p' || two == 'P'))

cout << "Player 1 wins Scissors cut Paper" << endl;

else if ((one == 'r' || one == 'R') & (two == 's' || two == 'S'))

cout << "Player 1 wins Rock destroys Scissors" << endl;

else if ((one == 'r' || one == 'R') & (two == 'p' || two == 'P'))

cout << "Player 2 wins Paper covers Rock" << endl;

else if ((one == 's' || one == 'S') & (two == 's' || two == 'S'))

cout << "Tie" << endl;

else if ((one == 'p' || one == 'P') & (two == 'p' || two == 'P'))

cout << "Tie" << endl;

else if ((one == 'r' || one == 'R') & (two == 'r' || two == 'R'))

cout << "Tie" << endl;

else

cout << "you're too stupid to play this game" << endl;

}

else

cout << "Goodbye\n";

}while (one !=0);

return 0;

}

After i run this program i get it to say goodbye when i dont want to play rock paper or scissors but i can get the program to end, it just asks if i want to play another game

 Tags:

   Report

3 ANSWERS


  1. Please do not delete your questions after people answer them, especially if you actually use their answers. It&#039;s rude. Either vote for a best answer or let the question go to a vote.

    That said, change this:

    cout &lt;&lt; &quot;Do you want to play a Rock, Paper or Scissors Game?\n&quot;;

    cin &gt;&gt; ans;

    while(ans != &#039;N&#039; || ans != &#039;n&#039;) {

    // rest of your code

    }

    And change this:

    else

    cout &lt;&lt; &quot;Goodbye\n&quot;;

    }while (one !=0);

    return 0;

    }

    To:

    cout &lt;&lt; &quot;Goodbye\n&quot;;

    return 0;

    }


  2. Your condition to get out of your loop is if one == 0.  If you want out of the loop, you&#039;re going to have to set the condition in your last else statement.

    else

    {

    cout &lt;&lt; &quot;Goodbye\n&quot;;

    one = 0;

    }

  3. to exit you&#039;re checking one!=0  .....one is set by a char input so it will never be the value 0 but rather the character &#039;0&#039;.

    An easy way is to rewrite the else to exit when you print goodbye

    else

    {

    cout &lt;&lt; &quot;Goodbye\n&quot;;

    one=0;  // this will be evaluated in the while check and exit

    }

You're reading: Need help with a loop

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.