T1 Q3
This commit is contained in:
parent
a7efeab624
commit
18c5f972c2
36
main.cpp
36
main.cpp
@ -1,14 +1,30 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
using string = std::string;
|
int main()
|
||||||
|
{
|
||||||
int main() {
|
using std::cout;
|
||||||
std::cout << "Enter your name:\n";
|
using std::cin;
|
||||||
string name;
|
srand(time(nullptr));
|
||||||
std::cin >> name;
|
int gcount{ 1 };
|
||||||
std::cout << "Hello, " << name << "!\n";
|
int n { 20 };
|
||||||
for (int i {0}; i < static_cast<int>(name.size()) + 8; i++) {
|
int secret { rand()%n + 1 };
|
||||||
std::cout << "=";
|
cout << "Guess a number between 1 and " << n << ": ";
|
||||||
|
int guess;
|
||||||
|
cin >> guess;
|
||||||
|
while (guess != secret)
|
||||||
|
{
|
||||||
|
if (guess > secret) {
|
||||||
|
cout << "Number too big ; ";
|
||||||
|
} else {
|
||||||
|
cout << "Number too small ; ";
|
||||||
|
}
|
||||||
|
gcount++;
|
||||||
|
cout << "Guess again: ";
|
||||||
|
cin >> guess;
|
||||||
}
|
}
|
||||||
std::cout << "\n";
|
cout << "Correct!\n";
|
||||||
|
cout << "Took " << gcount << " tries.\n";
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user