| | |
Rock Paper Scissors fun-ction
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 114
Reputation:
Solved Threads: 2
Hello everyone, I am having trouble fine tuning my code. I like to have all the bulky parts of my main in functions, and I can not seem to figure out how to write this code as a function. Anyone tell me where to start?
Thats my main this is the code I would like to be a function.
c++ Syntax (Toggle Plain Text)
int main() { int player = 0; int computer = 0; int tie = 0; do { char userChoice; char compChoice; compChoice = getCompChoice(); userChoice = getUserChoice(); cout << "User picked: " << userChoice << endl; cout << "Computer picked: " << compChoice << endl; if (compChoice == userChoice) { cout << "It's a tie.\n"; tie += 1; } else if ((compChoice == 'R') && (userChoice == 'S')) { cout << "Rock smashes scissors. You Lose.\n"; computer += 1; } else if ((compChoice == 'P') && (userChoice == 'R')) { cout << "Paper covors rock. You Lose.\n"; computer += 1; } else if ((compChoice == 'S') && (userChoice == 'P')) { cout << "Scissors cut paper. You Lose.\n"; computer += 1; } else if ((compChoice == 'R') && (userChoice == 'P')) { cout << "Paper covors rock. You Win.\n"; player += 1; } else if ((compChoice == 'P') && (userChoice == 'S')) { cout << "Scissors cut paper. You Win.\n"; player += 1; } else if ((compChoice == 'S') && (userChoice == 'R')) { cout << "Rock smashes scissors. You Win.\n"; player += 1; } cout << "User: " << player << endl << "Computer: " << computer << endl << "Ties: " << tie << endl; } while (doItAgain()); return (0); }
c++ Syntax (Toggle Plain Text)
if (compChoice == userChoice) { cout << "It's a tie.\n"; tie += 1; } else if ((compChoice == 'R') && (userChoice == 'S')) { cout << "Rock smashes scissors. You Lose.\n"; computer += 1; } else if ((compChoice == 'P') && (userChoice == 'R')) { cout << "Paper covors rock. You Lose.\n"; computer += 1; } else if ((compChoice == 'S') && (userChoice == 'P')) { cout << "Scissors cut paper. You Lose.\n"; computer += 1; } else if ((compChoice == 'R') && (userChoice == 'P')) { cout << "Paper covors rock. You Win.\n"; player += 1; } else if ((compChoice == 'P') && (userChoice == 'S')) { cout << "Scissors cut paper. You Win.\n"; player += 1; } else if ((compChoice == 'S') && (userChoice == 'R')) { cout << "Rock smashes scissors. You Win.\n"; player += 1; }
Last edited by kylcrow; Apr 23rd, 2007 at 10:04 pm.
•
•
Join Date: Apr 2007
Posts: 114
Reputation:
Solved Threads: 2
ok that is what I was wondering. But is it pssible to pass chars and int&
I am honestly not trying to get you to write my code, just a lil help where to go from here.
c++ Syntax (Toggle Plain Text)
char result(char compChoice, char userChoice, int& player, int& computer, int& tie) { if (compChoice == userChoice) { tie += 1; return ("It's a tie.\n") } else if ((compChoice == 'R') && (userChoice == 'S')) { computer += 1; return ("Rock smashes scissors. You Lose.\n") } else if ((compChoice == 'P') && (userChoice == 'R')) { computer += 1; return ("Paper covors rock. You Lose.\n") } else if ((compChoice == 'S') && (userChoice == 'P')) { computer += 1; return ("Scissors cut paper. You Lose.\n") } else if ((compChoice == 'R') && (userChoice == 'P')) { player += 1; return ("Paper covors rock. You Win.\n") } else if ((compChoice == 'P') && (userChoice == 'S')) { player += 1; return ("Scissors cut paper. You Win.\n") } else if ((compChoice == 'S') && (userChoice == 'R')) { player += 1; return ("Rock smashes scissors. You Win.\n") } }
I am honestly not trying to get you to write my code, just a lil help where to go from here.
looks ok to me except the function should return char* not char. See lines 13 and 14 below.
The rest is simple
The rest is simple
c Syntax (Toggle Plain Text)
int main() { int player = 0; int computer = 0; int tie = 0; do { char userChoice; char compChoice; const char* msg; compChoice = getCompChoice(); userChoice = getUserChoice(); msg = result(compChoice, userChoice, player, computer, tie); cout << msg << "\n"; cout << "User picked: " << userChoice << endl; cout << "Computer picked: " << compChoice << endl; cout << "User: " << player << endl << "Computer: " << computer << endl << "Ties: " << tie << endl; } while (doItAgain()); return (0); }
Last edited by Ancient Dragon; Apr 23rd, 2007 at 10:40 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Visual C++ Installer problems
- Next Thread: for-loop help please
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






