-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbergame.cpp
More file actions
48 lines (43 loc) · 1.14 KB
/
Copy pathnumbergame.cpp
File metadata and controls
48 lines (43 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Created by Nitroo on 5/16/2026.
//
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "numbergame.h"
using namespace std;
void guessnumber()
{
int secret = rand() %100 +1; // to shrink range to the required range
int guess;
int trials = 7;
cout<<"Guess a number between 1 and 100."<<endl;
cout<<"Tip: keep dividing or doubling your guessed number."<<endl;
while (trials > 0)
{
cin>>guess;
if (guess < 1 || guess> 100){
cout<<"Choose a valid number,sport!"<<endl; // didn't use continue because I wish user must lose trial for irrelevant inputs
}
if (guess == secret)
{
cout<<"Bingo! You got it correct."<<endl;
break;
}
else if (guess < secret)
{
cout<<"Your guessed number is lower. "<<endl;
}
else
{
cout<<"Your guessed number is higher. "<<endl;
}
trials--;
cout<<"Trials : "<<trials<<endl;
if (trials == 0)
{
cout<<"Sorry! You used all of your trials. Try again. "<<endl;
break;
}
}
}