Quantcast
Viewing all articles
Browse latest Browse all 26

Answer by MikelThief for C++: Prompt Requires Data to be Inputted Multiple Times to Proceed

Your code could look like this:

#include <iostream>
#include <ctime>//A user stated that using this piece of code would make a true randomization process.
#include <cstdlib>
using namespace std;

int main()//When inputing a number, sometimes the user has to input it more than once.
{
    int x;
int ranNum;
srand(time(0));

ranNum = rand() % 100 + 1;

cout << "Please input your guess for a random number between 1 - 100.\n";


while (x > ranNum || x < ranNum)
{
    cin >> x;
    if (x > ranNum && x<=100 & x>=1) cout << "Your input was greater than the system's generated random number. Please try again.\n";
    else if (x < ranNum && x<=100 & x>=1) cout << "Your input was smaller than the system's generated random number. Please try again.\n";
    else if (x>=100 || x<=1) cout << "Input invalid. Please input a number between 1 - 100.\n";

}
cout << "You guessed right!\n";

    return 0;
}

In future:

When you use while loop, it includes if statement in it. Use is properly. Also, try nt to nest if statements but use logical operators instead.


Viewing all articles
Browse latest Browse all 26

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>