| | |
Bad Code question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
I am writing code for a homework assignment. I have to write a function, and in the function prompt the user to enter either rock, paper, or scissors. I did it successfully declaring a char (choice) globably, but I was told this wasnt a good idea, even though it worked. Why wouldn't this be a good idea?
actually here is my code so far...
How would I return the users input using a function without returning a variable? char getUserChoice()
actually here is my code so far...
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; char getUserChoice(char); char choice; int main() { char again; int computer = 0; int player = 0; int tie = 0; do { char comp; srandom(time(0)); int n = random() % 3; n++; if (n == 1) { comp = 'R'; } else if (n == 2) { comp = 'P'; } else { comp = 'S'; } if (comp == getUserChoice(choice)) { cout << "It's a tie.\n"; tie += 1; } else if ((comp == 'R') && (choice == 'S')) { cout << "Rock smashes scissors. You Lose.\n"; computer += 1; } else if ((comp == 'P') && (choice == 'R')) { cout << "Paper covors rock. You Lose.\n"; computer += 1; } else if ((comp == 'S') && (choice == 'P')) { cout << "Scissors cut paper. You Lose.\n"; computer += 1; } else if ((comp == 'R') && (choice == 'P')) { cout << "Paper covors rock. You Win.\n"; player += 1; } else if ((comp == 'P') && (choice == 'S')) { cout << "Scissors cut paper.You Win.\n"; player += 1; } else if ((comp == 'S') && (choice == 'R')) { cout << "Rock smashes scissors.You Win.\n"; player += 1; } cout << "Computer wins: " << computer << endl << "User wins: " << player << endl << "Ties: " << tie << endl; cout << "Play again?(Yes or No): "; cin >> again; cin.ignore(1000, '\n'); again = toupper(again); } while (again == 'Y') ; return(0); } char getUserChoice(char) { cout << "Please enter Rock, Paper, or Scissors: "; cin >> choice; cin.ignore(1000, '\n'); choice = toupper(choice); return(choice); }
How would I return the users input using a function without returning a variable? char getUserChoice()
Last edited by kylcrow; Apr 19th, 2007 at 3:31 pm.
Your code really needs to be indented for readability purposes.
Anyways, global identifiers are typically considered bad style. They still work, but if you can come up with a solution without globals, then you should do that. And since you return the user's choice anyways, why do you need to keep it in a global variable?
Anyways, global identifiers are typically considered bad style. They still work, but if you can come up with a solution without globals, then you should do that. And since you return the user's choice anyways, why do you need to keep it in a global variable?
•
•
Join Date: Apr 2007
Posts: 110
Reputation:
Solved Threads: 2
The code worked fine the way I had it with my compiler, I was just asking how I could change it so that the function wouldn't need to return a variable... would this work?
c++ Syntax (Toggle Plain Text)
char getUserChoice () { int again = 0; do { char x; cout << "Please enter Rock, Paper, or Scissors: "; cin >> x; cin.ignore(1000, '\n'); x = toupper(x); if (x == 'R') { return ('R'); } else if (x == 'P') { return ('P'); } else if (x == 'S') { return ('S'); } else { again = 1; cout << "Not a good choice. "; } } while (again = 1); }
Last edited by kylcrow; Apr 19th, 2007 at 4:10 pm.
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; char getUserChoice ( void ); int main() { char again; int computer = 0; int player = 0; int tie = 0; do { char comp; srand ( time ( 0 ) ); int n = rand() % 3; n++; if ( n == 1 ) { comp = 'R'; } else if ( n == 2 ) { comp = 'P'; } else { comp = 'S'; } char summat = getUserChoice(); if ( comp == summat ) { cout << "It's a tie.\n"; tie += 1; } else if ( ( comp == 'R' ) && ( summat == 'S' ) ) { cout << "Rock smashes scissors. You Lose.\n"; computer += 1; } else if ( ( comp == 'P' ) && ( summat == 'R' ) ) { cout << "Paper covors rock. You Lose.\n"; computer += 1; } else if ( ( comp == 'S' ) && ( summat == 'P' ) ) { cout << "Scissors cut paper. You Lose.\n"; computer += 1; } else if ( ( comp == 'R' ) && ( summat == 'P' ) ) { cout << "Paper covors rock. You Win.\n"; player += 1; } else if ( ( comp == 'P' ) && ( summat == 'S' ) ) { cout << "Scissors cut paper.You Win.\n"; player += 1; } else if ( ( comp == 'S' ) && ( summat == 'R' ) ) { cout << "Rock smashes scissors.You Win.\n"; player += 1; } cout << "Computer wins: " << computer << endl << "User wins: " << player << endl << "Ties: " << tie << endl; cout << "Play again?(Yes or No): "; cin >> again; cin.ignore ( 1000, '\n' ); again = toupper ( again ); } while ( again == 'Y' ); return ( 0 ); } char getUserChoice ( void ) { char choice; cout << "Please enter Rock, Paper, or Scissors: "; cin >> choice; cin.ignore ( 1000, '\n' ); choice = toupper ( choice ); return ( choice ); }
Last edited by iamthwee; Apr 19th, 2007 at 4:20 pm.
*Voted best profile in the world*
![]() |
Other Threads in the C++ Forum
- Previous Thread: Rock Paper Scissors scoring help
- Next Thread: ISP Subscription Program
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






