944,110 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2591
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 19th, 2007
0

Bad Code question

Expand Post »
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...

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 19th, 2007
0

Re: Bad Code question

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?
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 19th, 2007
0

Re: Bad Code question

> `srandom' undeclared (first use this function)

Depending on what compiler you are using that srandom function may not be recognised.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 19th, 2007
0

Re: Bad Code question

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.
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 19th, 2007
0

Re: Bad Code question

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 19th, 2007
0

Re: Bad Code question

i took his code and obviously put the srandom to srand and the random to rand... works fine
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Apr 19th, 2007
0

Re: Bad Code question

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)
  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.
Reputation Points: 11
Solved Threads: 2
Junior Poster
kylcrow is offline Offline
124 posts
since Apr 2007
Apr 19th, 2007
0

Re: Bad Code question

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Apr 19th, 2007
0

Re: Bad Code question

you tell us lol... does it work?
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Apr 19th, 2007
0

Re: Bad Code question

#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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Rock Paper Scissors scoring help
Next Thread in C++ Forum Timeline: ISP Subscription Program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC