943,673 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 605
  • C++ RSS
Jul 14th, 2008
0

Is this correct?

Expand Post »
Hello, I made this program that has random values and you have to choose to say if the next number will be higher - or lower. I completed the code and successfully had no errors but when the user is mistaken nothing comes up. Am I missing something?

C++ Syntax (Toggle Plain Text)
  1. // Code Shark
  2.  
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. srand(time(NULL));
  11.  
  12. unsigned int digit = 0, digit2 = 0, correct = 0, wrong = 0;
  13. string answer;
  14.  
  15. do {
  16. system("Cls");
  17.  
  18. digit = rand() % 9 + 1;
  19. digit2 = rand() % 9 + 1;
  20.  
  21. cout << "Digit : " << digit << endl;
  22. cout << "Correct : " << correct << endl << endl;
  23. cout << "Will the next value be Higher, Lower or Equal : ";
  24. cin >> answer;
  25.  
  26. if(answer == "H") {
  27. if(digit2 > digit) {
  28. cout << endl << "Correct!";
  29. correct++;
  30. }
  31. }
  32.  
  33. else if(answer == "L") {
  34. if(digit > digit2) {
  35. cout << endl << "Correct!";
  36. correct++;
  37. }
  38. }
  39.  
  40. else if(answer == "E") {
  41. if(digit == digit2) {
  42. cout << endl << "Correct!";
  43. correct++;
  44. }
  45. }
  46.  
  47. else if(answer == "H") {
  48. if(digit2 < digit || digit2 == digit) {
  49. wrong++;
  50. }
  51. }
  52.  
  53. else if(answer == "L") {
  54. if(digit2 > digit || digit2 == digit) {
  55. wrong++;
  56. }
  57. }
  58.  
  59. else if(answer == "E") {
  60. if(digit2 > digit || digit2 < digit) {
  61. wrong++;
  62. }
  63. }
  64.  
  65. Sleep(1000);
  66.  
  67. }
  68.  
  69. while(!wrong);
  70.  
  71. cout << "You were correct " << correct << " times.";
  72.  
  73. cin.get();
  74. cin.get();
  75.  
  76. return 0;
  77. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Code Shark is offline Offline
14 posts
since Jul 2008
Jul 14th, 2008
0

Re: Is this correct?

I think this is what you meant--

c++ Syntax (Toggle Plain Text)
  1. // Code Shark
  2.  
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. srand(time(NULL));
  11.  
  12. unsigned int digit = 0, digit2 = 0, correct = 0, wrong = 0;
  13. string answer;
  14.  
  15. do {
  16. system("Cls");
  17.  
  18. digit = rand() % 9 + 1;
  19. digit2 = rand() % 9 + 1;
  20.  
  21. cout << "Digit : " << digit << endl;
  22. cout << "Correct : " << correct << endl << endl;
  23. cout << "Will the next value be Higher, Lower or Equal : ";
  24. cin >> answer;
  25.  
  26. if(answer == "H") {
  27. if(digit2 > digit) {
  28. cout << endl << "Correct!";
  29. correct++;
  30. }
  31. else if(digit2 < digit || digit2 == digit) {
  32. wrong++;
  33. }
  34. }
  35.  
  36. else if(answer == "L") {
  37. if(digit > digit2) {
  38. cout << endl << "Correct!";
  39. correct++;
  40. }
  41. else if(digit2 > digit || digit2 == digit) {
  42. wrong++;
  43. }
  44. }
  45.  
  46. else if(answer == "E") {
  47. if(digit == digit2) {
  48. cout << endl << "Correct!";
  49. correct++;
  50. }
  51. else if(digit2 > digit || digit2 < digit) {
  52. wrong++;
  53. }
  54. }
  55.  
  56. Sleep(1000);
  57.  
  58. }
  59.  
  60. while(!wrong);
  61.  
  62. cout << "You were correct " << correct << " times.";
  63.  
  64. cin.get();
  65. cin.get();
  66.  
  67. return 0;
  68. }
Last edited by Alex Edwards; Jul 14th, 2008 at 1:51 pm.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jul 19th, 2008
0

Re: Is this correct?

Hello, I was wondering if all i need to add was another do loop outside of the current do loop for the program to keep asking H, L, or E when an invalid character was entered?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Code Shark is offline Offline
14 posts
since Jul 2008
Jul 19th, 2008
0

Re: Is this correct?

Click to Expand / Collapse  Quote originally posted by Code Shark ...
Hello, I was wondering if all i need to add was another do loop outside of the current do loop for the program to keep asking H, L, or E when an invalid character was entered?
You can make another loop (I would actually put it inside the current loop rather than outside if you do that) or you can just keep it the way it is. Running Alex's revised code, it already asks again when it gets bad input. If you want to add an error message to display when the user enters bad data, add an "else" statement after the if-else statements that displays the error message, but doesn't change the correct or wrong variables.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

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: Operator Overloading in Namespace, not working, printing address
Next Thread in C++ Forum Timeline: C++ help needed please





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


Follow us on Twitter


© 2011 DaniWeb® LLC