WSQ 03 Pick a Number

--Originally published at Franco TC1017

 

morfeo

Man up! Chose the red one!

 

This program selects a random number every time the user runs it. I had difficulties at first because the program selected the same number everytime. This link helped me solve this http://www.cplusplus.com/reference/cstdlib/rand/ .

The program tells the user if his/her guess is too high or too low until he/she gets the right answer.

 

#include <iostream>
#include <stdlib.h> /*rand, srand*/
#include <stdio.h> /*NULL*/
#include <time.h> /*time*/

int main() {

int usernum;  /* This is the user input*/
int morfeosnum;  /* This variable will contain the random number*/

std::cout << “Hello Neo, I have a number between 1 and 100 in my head. Can you guess which is it?” << ‘\n’;

srand(time(NULL));  /* This makes a new random number everytime I run the code*/

morfeosnum = rand() % 100 + 1;  /* This sets my range of random numbers*/

do {
std::cin >> usernum;
if (usernum > morfeosnum) {
std::cout << “Your number is too high. Try again…” << ‘\n’;
} else if (usernum < morfeosnum) {
std::cout << “Your number is too low. Try again…” << ‘\n’;
} else {
std::cout << “You answered right, that is my number. Now… choose a pill…” << ‘\n’;
}
} while(morfeosnum != usernum);

return 0;
}

pick-a-number-code-blogpick-a-number-terminal-blog