| | |
Rock paper scissors using functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
hello everybody. I am trying to write a game of rock paper scissors where the user plays against the computer. The computer picks a random number and the user makes his/her own selection. then the selections are compaired and the winner is selected.
I need to write the following in functions:
get user choice
get computer choice
i was able to get the winner in a function already.
the program works as it stands now but i have no idea how to put the user and computer choice in a function. here is my code
any suggestions?
I need to write the following in functions:
get user choice
get computer choice
i was able to get the winner in a function already.
the program works as it stands now but i have no idea how to put the user and computer choice in a function. here is my code
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void winner(int selection, int compSelection); int main() { srand((unsigned)time(0)); int compSel = (rand()%2)+1; int userSel; cout << "Welcome to ~ Rock Paper Scissors ~!! I assume you know how to play,"; cout << " so lets begin. You are playing against the computer. Type 0 for"; cout << " rock, 1 for paper, and 2 for scissors\n"; cin >> userSel; cout << "Computer Selects " << compSel << endl; winner(userSel, compSel); system("pause"); return 0; } */ // ************************************************************************ // Winner // // task: Selects the winner // data in: user choice and computer choice // data out: Winner // // *********************************************************************** void winner (int selection, int compSelection) { if (selection == 0) { if (compSelection == 0) cout << "It's a tie!\n\n\n\n"; else if (compSelection == 1) cout << "Paper beats rock! Sorry, you lose!\n\n\n\n"; else if (compSelection == 2) cout << "Rock beats scissors! You win!\n\n\n\n"; } else if (selection == 1) { if (compSelection == 0) cout << "It's a tie!\n\n\n\n"; else if (compSelection == 1) cout << "Paper beats rock! You win!\n\n\n\n"; else if (compSelection == 2) cout << "Scissors beat paper! Sorry, you lose!\n\n\n\n"; } else if (selection == 2) { if (compSelection == 0) cout << "It's a tie!\n\n\n\n"; else if (compSelection == 1) cout << "Scissors beat paper! You win!\n\n\n\n"; else if (compSelection == 2) cout << "Rock beats scissors! Sorry, you lose!\n\n\n\n"; } }
any suggestions?
![]() |
Similar Threads
- help with rock paper scissor requirement (C++)
- Adding arrays, selections, and a quit option (C++)
- Rock Paper Scissors fun-ction (C++)
Other Threads in the C++ Forum
- Previous Thread: visualcplusplus gray-out
- Next Thread: string vs. variable
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






