Is this correct?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 14
Reputation: Code Shark is an unknown quantity at this point 
Solved Threads: 0
Code Shark Code Shark is offline Offline
Newbie Poster

Is this correct?

 
0
  #1
Jul 14th, 2008
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?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Is this correct?

 
0
  #2
Jul 14th, 2008
I think this is what you meant--

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 14
Reputation: Code Shark is an unknown quantity at this point 
Solved Threads: 0
Code Shark Code Shark is offline Offline
Newbie Poster

Re: Is this correct?

 
0
  #3
Jul 19th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Is this correct?

 
0
  #4
Jul 19th, 2008
Originally Posted by Code Shark View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC