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?

// 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;
}

Recommended Answers

All 3 Replies

I think this is what you meant--

// 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;
}

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.