• 0

Basic c++ help (newb)


Question

Here is my code. I'm trying to make it loop, and have the user be able to choose to end the loop with something like this

Code:

char continue;

cout<<"Enter 'Y' to continue or 'N' to quit;

cin<<continue;

I got something close to what I wanted but not quite. Can somebody help me??

Code:

#include <iostream>

using namespace std;

//Write a program to score the paper-rock-scissor game. Each of two users types

//in either P, R, or S. The program then announces the winner as well as the basis

//for determining the winner: Paper covers rock, Rock breaks scissors,

//Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase

//as well as uppercase letters. Your program should include a loop that lets

//the user play again until the user says she or he is done.

int main (int argc, char* argv[])

{

cout<<"Welcome to Rock Papper Scissors!!! \n";

cout<<"The instructions are simple enter either P, R, or S,\n";

cout<<"which signify rock, paper, and scissors respectively. \n";

char user_1_pick, user_2_pick;

cout<<endl;

cout<<"Player 1 please enter your pick (P,R, or S):";

cin>>user_1_pick;

cout<<endl;

cout<<"Player 2 please enter your pick (P,R, or S):";

cin>>user_2_pick;

if (((user_1_pick == 'S')||(user_1_pick== 's')) && ((user_2_pick == 'R')||(user_2_pick == 'r')))

{

cout<<endl;

cout<<"Rock annihilates scissors. \n";

cout<<"Player 2 you win!!";

cout<<endl;

}

else if (((user_1_pick == 'R')||(user_1_pick == 'r'))&& ((user_2_pick== 'p')||(user_2_pick=='P')))

{

cout<<endl;

cout<<"Paper suffocates rock. \n";

cout<<"Player 2 you win!!";

cout<<endl;

}

else if (((user_1_pick == 'P')||(user_1_pick=='p'))&&((user_2_pick=='s')||(user_2_pick=='S')))

{

cout<<endl;

cout<<"Scissors slices through paper. \n";

cout<<"Player 2 you win!!";

cout<<endl;

}

else if (((user_1_pick == 'R')||(user_1_pick=='r'))&&((user_2_pick=='s')||(user_2_pick=='S')))

{

cout<<endl;

cout<<"Rock annihilates scissors. \n";

cout<<"Player 1 you win!!";

cout<<endl;

}

else if (((user_1_pick=='S')||(user_1_pick=='s'))&&((user_2_pick=='p')||(user_2_pick=='P')))

{

cout<<endl;

cout<<"Scissors slice through paper. \n";

cout<<"Player 1 you win!!";

cout<<endl;

}

else if (((user_1_pick=='P')||(user_1_pick=='p'))&&((user_2_pick=='r')||(user_2_pick=='R')))

{

cout<<endl;

cout<<"Paper suffocates rock. \n";

cout<<"Player 1 you win!!";

cout<<endl;

}

else

{

cout<<"Nobody wins!! \n";

cout<<endl;

}

system("pause"); //This will avoid closing the cmd.

return 0;

}

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

enclose the whole thing from the first cout statement like this:

do {

// all your code

cout << "Play again? (y/n)" << endl;

cin >> choice;

}while(choice == 'Y' || choice == 'y');

Link to comment
Share on other sites

  • 0
Here is my code. I'm trying to make it loop, and have the user be able to choose to end the loop with something like this

Code:

char continue;

cout<<"Enter 'Y' to continue or 'N' to quit;

cin<<continue;

I got something close to what I wanted but not quite. Can somebody help me??

Code:

#include <iostream>

using namespace std;

//Write a program to score the paper-rock-scissor game. Each of two users types

//in either P, R, or S. The program then announces the winner as well as the basis

//for determining the winner: Paper covers rock, Rock breaks scissors,

//Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase

//as well as uppercase letters. Your program should include a loop that lets

//the user play again until the user says she or he is done.

int main (int argc, char* argv[])

{

cout<<"Welcome to Rock Papper Scissors!!! \n";

cout<<"The instructions are simple enter either P, R, or S,\n";

cout<<"which signify rock, paper, and scissors respectively. \n";

char user_1_pick, user_2_pick;

cout<<endl;

cout<<"Player 1 please enter your pick (P,R, or S):";

cin>>user_1_pick;

cout<<endl;

cout<<"Player 2 please enter your pick (P,R, or S):";

cin>>user_2_pick;

if (((user_1_pick == 'S')||(user_1_pick== 's')) && ((user_2_pick == 'R')||(user_2_pick == 'r')))

{

cout<<endl;

cout<<"Rock annihilates scissors. \n";

cout<<"Player 2 you win!!";

cout<<endl;

}

else if (((user_1_pick == 'R')||(user_1_pick == 'r'))&& ((user_2_pick== 'p')||(user_2_pick=='P')))

{

cout<<endl;

cout<<"Paper suffocates rock. \n";

cout<<"Player 2 you win!!";

cout<<endl;

}

else if (((user_1_pick == 'P')||(user_1_pick=='p'))&&((user_2_pick=='s')||(user_2_pick=='S')))

{

cout<<endl;

cout<<"Scissors slices through paper. \n";

cout<<"Player 2 you win!!";

cout<<endl;

}

else if (((user_1_pick == 'R')||(user_1_pick=='r'))&&((user_2_pick=='s')||(user_2_pick=='S')))

{

cout<<endl;

cout<<"Rock annihilates scissors. \n";

cout<<"Player 1 you win!!";

cout<<endl;

}

else if (((user_1_pick=='S')||(user_1_pick=='s'))&&((user_2_pick=='p')||(user_2_pick=='P')))

{

cout<<endl;

cout<<"Scissors slice through paper. \n";

cout<<"Player 1 you win!!";

cout<<endl;

}

else if (((user_1_pick=='P')||(user_1_pick=='p'))&&((user_2_pick=='r')||(user_2_pick=='R')))

{

cout<<endl;

cout<<"Paper suffocates rock. \n";

cout<<"Player 1 you win!!";

cout<<endl;

}

else

{

cout<<"Nobody wins!! \n";

cout<<endl;

}

system("pause"); //This will avoid closing the cmd.

return 0;

}

How about wrapping your if statements in the following while statement:

while (continue != 'n')
{
}

I hope this helps. :)

Link to comment
Share on other sites

  • 0
enclose the whole thing from the first cout statement like this:

do {

// all your code

cout << "Play again? (y/n)" << endl;

cin >> choice;

}while(choice == 'Y' || choice == 'y');

Thanks all it works now!!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.