| | |
Is this correct?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Solved Threads: 0
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)
// Code Shark #include <iostream> #include <windows.h> #include <string> using namespace std; int main() { srand(time(NULL)); unsigned int digit = 0, digit2 = 0, correct = 0, wrong = 0; string answer; do { system("Cls"); digit = rand() % 9 + 1; digit2 = rand() % 9 + 1; cout << "Digit : " << digit << endl; cout << "Correct : " << correct << endl << endl; cout << "Will the next value be Higher, Lower or Equal : "; cin >> answer; if(answer == "H") { if(digit2 > digit) { cout << endl << "Correct!"; correct++; } } else if(answer == "L") { if(digit > digit2) { cout << endl << "Correct!"; correct++; } } else if(answer == "E") { if(digit == digit2) { cout << endl << "Correct!"; correct++; } } else if(answer == "H") { if(digit2 < digit || digit2 == digit) { wrong++; } } else if(answer == "L") { if(digit2 > digit || digit2 == digit) { wrong++; } } else if(answer == "E") { if(digit2 > digit || digit2 < digit) { wrong++; } } Sleep(1000); } while(!wrong); cout << "You were correct " << correct << " times."; cin.get(); cin.get(); return 0; }
I think this is what you meant--
c++ Syntax (Toggle Plain Text)
// Code Shark #include <iostream> #include <windows.h> #include <string> using namespace std; int main() { srand(time(NULL)); unsigned int digit = 0, digit2 = 0, correct = 0, wrong = 0; string answer; do { system("Cls"); digit = rand() % 9 + 1; digit2 = rand() % 9 + 1; cout << "Digit : " << digit << endl; cout << "Correct : " << correct << endl << endl; cout << "Will the next value be Higher, Lower or Equal : "; cin >> answer; if(answer == "H") { if(digit2 > digit) { cout << endl << "Correct!"; correct++; } else if(digit2 < digit || digit2 == digit) { wrong++; } } else if(answer == "L") { if(digit > digit2) { cout << endl << "Correct!"; correct++; } else if(digit2 > digit || digit2 == digit) { wrong++; } } else if(answer == "E") { if(digit == digit2) { cout << endl << "Correct!"; correct++; } else if(digit2 > digit || digit2 < digit) { wrong++; } } Sleep(1000); } while(!wrong); cout << "You were correct " << correct << " times."; cin.get(); cin.get(); return 0; }
Last edited by Alex Edwards; Jul 14th, 2008 at 1:51 pm.
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
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.
![]() |
Similar Threads
- database won't give correct permissions (MS SQL)
- Help function not returning correct value (C++)
- Returning a correct python list (Python)
- help me to correct this part of my program (C++)
- I cant correct these two errors (C++)
- correct errors in this program (C)
- code repeatedly 2 ask user 4 a password until correct (=Java) but cant c wots wrong. (Java)
Other Threads in the C++ Forum
- Previous Thread: Operator Overloading in Namespace, not working, printing address
- Next Thread: C++ help needed please
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






