954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is this correct?

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;
}
Code Shark
Newbie Poster
14 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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;
}
Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
 

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?

Code Shark
Newbie Poster
14 posts since Jul 2008
Reputation Points: 10
Solved Threads: 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?

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You