#WSQ06-Pick a number

This one was a little tricky, because I had to do some research of certain commands for it to properly run…

I made a program that picks a random integer in the range of 1 to 100.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

You will see that I did use some “if ” conditionals just for simple logical problems and also I did use another helpful tool in C++ called “do-while”, what this command does is that repeats in loops the command inside the “do instructions” until the “while condition” (which is also a logical statement) is true or false depending on your code.

//CODE:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main(int argc, char const *argv[]) {
int n1, n2, count=0;

std::cout << “This program can help you guess the number is picked.” << std::endl;
srand(time(NULL));
n1=rand()%100+1;
do {
count++;
std::cout << “Enter a number from 1 to 100.” << std::endl;
std::cin >> n2;
if (n2<n1) {
std::cout << “The number you just wrote is too low. Try again.” << std::endl;
}
else{
if (n2>n1) {
std::cout << “The number you just wrote is too high. Try again.” << std::endl;
}
else{
std::cout << “Congratulations you just hit the big one!!” << std::endl;
}
}
} while(n2!=n1);
std::cout << “You tried “<<count<<” time(s)” << std::endl;

return 0;
}

Captura de pantalla 2016-02-17 a las 18.35.17

CC BY-SA 4.0 #WSQ06-Pick a number by everolivares is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.