The third task was to create a program that picks a random integer in the range of 1 to 100. After that , the game starts when the user has to guess which number does the program picked, with hints of ’too high’ or ’too low’.
The program continues to run until the user guesses the integer.
I added some extra steps, because I wanted to show more information to the users , like:
¿How many guesses they had to make to get the right answer?
This is my third program, created using Cygwin and Atom.
The task was to create a program that picks a random integer in the range of 1 to 100. After that , the game starts when the user has to guess which number does the program picked, with hints of ’too high’ or ’too low’.
The program continues to run until the user guesses the integer.
I added some extra steps, because I wanted to show more information to the users , like:
-How many guesses they had to make to get the right answer.
The resources that helped me were: -How to think like a computer scientist.
-cplusplus.com
-cppreference.com
-Programming hub, mobile app.
Here you will find my code and some captures:
Atom code.
#include <iostream>
#include <stdlib.h> // srand , rand
#include <time.h> // time
using namespace std;
int main(){
int secret , guess, attempts;
cout << “This program picks a random integer in the range of 1-100.” << endl;
cout << “***In order to win , you need to guess the number***” << endl;
do {
cout << “Write your guess:”<< endl;
cin >> guess;
attempts=attempts+1;
if (secret<guess){
cout << “The secret number is lower.” << endl;
}
else if (secret>guess){
cout << “The secret number is higher.” << endl;
}
} while (secret!=guess);
cout << “You won , congratulations!” << endl;
cout << “It took you ” << attempts << ” attempts.” << endl;
return 0;
}