Bad Code question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Bad Code question

 
0
  #1
Apr 19th, 2007
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...

  1.  
  2. #include <iostream>
  3. #include <ctime>
  4. #include <cstdlib>
  5. using namespace std;
  6. char getUserChoice(char);
  7. char choice;
  8. int main()
  9. {
  10. char again;
  11. int computer = 0;
  12. int player = 0;
  13. int tie = 0;
  14. do {
  15. char comp;
  16. srandom(time(0));
  17. int n = random() % 3;
  18. n++;
  19.  
  20. if (n == 1) {
  21. comp = 'R';
  22. } else if (n == 2) {
  23. comp = 'P';
  24. } else {
  25. comp = 'S';
  26. }
  27.  
  28. if (comp == getUserChoice(choice)) {
  29. cout << "It's a tie.\n";
  30. tie += 1;
  31. } else if ((comp == 'R') && (choice == 'S')) {
  32. cout << "Rock smashes scissors. You Lose.\n";
  33. computer += 1;
  34. } else if ((comp == 'P') && (choice == 'R')) {
  35. cout << "Paper covors rock. You Lose.\n";
  36. computer += 1;
  37. } else if ((comp == 'S') && (choice == 'P')) {
  38. cout << "Scissors cut paper. You Lose.\n";
  39. computer += 1;
  40. } else if ((comp == 'R') && (choice == 'P')) {
  41. cout << "Paper covors rock. You Win.\n";
  42. player += 1;
  43. } else if ((comp == 'P') && (choice == 'S')) {
  44. cout << "Scissors cut paper.You Win.\n";
  45. player += 1;
  46. } else if ((comp == 'S') && (choice == 'R')) {
  47. cout << "Rock smashes scissors.You Win.\n";
  48. player += 1;
  49. }
  50. cout << "Computer wins: " << computer << endl << "User wins: " << player << endl << "Ties: "
  51. << tie << endl;
  52.  
  53. cout << "Play again?(Yes or No): ";
  54. cin >> again;
  55. cin.ignore(1000, '\n');
  56. again = toupper(again);
  57. } while (again == 'Y')
  58. ;
  59. return(0);
  60. }
  61.  
  62. char getUserChoice(char)
  63. {
  64. cout << "Please enter Rock, Paper, or Scissors: ";
  65. cin >> choice;
  66. cin.ignore(1000, '\n');
  67. choice = toupper(choice);
  68. return(choice);
  69. }

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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Bad Code question

 
0
  #2
Apr 19th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Bad Code question

 
0
  #3
Apr 19th, 2007
> `srandom' undeclared (first use this function)

Depending on what compiler you are using that srandom function may not be recognised.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: Bad Code question

 
0
  #4
Apr 19th, 2007
Thats what I was really needing. A way to return an R S or P without returning a variable. Im not expecting someone to do it for me, just point me in the right direction. (Sorry about the indents I am trying to copy and paste from putty and its not extremely simple.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Bad Code question

 
0
  #5
Apr 19th, 2007
Well the problem is your comparison. You cannot use == to compare chars. Wait you're using a single char... oops.
Last edited by iamthwee; Apr 19th, 2007 at 4:03 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Bad Code question

 
0
  #6
Apr 19th, 2007
i took his code and obviously put the srandom to srand and the random to rand... works fine
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 110
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: Bad Code question

 
0
  #7
Apr 19th, 2007
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?

  1.  
  2. char getUserChoice ()
  3. {
  4. int again = 0;
  5. do {
  6. char x;
  7. cout << "Please enter Rock, Paper, or Scissors: ";
  8. cin >> x;
  9. cin.ignore(1000, '\n');
  10. x = toupper(x);
  11. if (x == 'R') {
  12. return ('R');
  13. } else if (x == 'P') {
  14. return ('P');
  15. } else if (x == 'S') {
  16. return ('S');
  17. } else {
  18. again = 1;
  19. cout << "Not a good choice. ";
  20. }
  21. } while (again = 1);
  22. }
Last edited by kylcrow; Apr 19th, 2007 at 4:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Bad Code question

 
0
  #8
Apr 19th, 2007
Maybe you need to read a tutorial about functions and the scope of variables within a function? Because you have more or less got it. You just have to rename a few of your variables.
Last edited by iamthwee; Apr 19th, 2007 at 4:12 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 103
Reputation: mariocatch is an unknown quantity at this point 
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: Bad Code question

 
0
  #9
Apr 19th, 2007
you tell us lol... does it work?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Bad Code question

 
0
  #10
Apr 19th, 2007
#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*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC