-
Notifications
You must be signed in to change notification settings - Fork 1
/
gjettespill_test.cpp
74 lines (67 loc) · 1.79 KB
/
gjettespill_test.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
int main() {
cout<<" "<<endl;
cout<<"Welcome to guessing game!\n";
cout<<"Try guessing number that was randomly generated by a machine.\n";
cout<<"Number generated will be in range from 1 to N\n";
cout<<" "<<endl;
cout<<"Enter a max number (N) you wish to be generated: ";
int randN;
cin>>randN;
int enGangTil=1;
int antRunder=0;
int totGjett=0;
while (enGangTil==1){
antRunder++;
int gjettinger=1;
int randNum=(rand()%randN);
cout<<"What is you guess? ";
int guess;
cin>>guess;
while (randNum != guess) {
if (guess<randNum) {
cout<<"To low\n";
cout<<"What is you guess: ";
cin>>guess;
gjettinger++;
}
else if(guess>randNum){
cout<<"To high\n";
cout<<"What is you guess: ";
cin>>guess;
gjettinger++;
}
}
totGjett=totGjett+gjettinger;
cout<<"That is correct!\n";
cout<<"It took you "<<gjettinger<<" attempts to guess right.\n";
if (gjettinger<5) {
cout<<"You had less than 5 attempts, you did well!\n";
}
else {
cout<<"You had more than 5 attempts, try doing a better job next time!\n";
}
cout<<"Do you want to continue playing? [Y/n]: ";
char answer;
cin>>answer;
while (answer !='N' && answer!='n' && answer!='Y' && answer!='y') {
std::cout << "Please type Y or n: ";
cin>>answer;
}
if (answer=='y' || answer=='Y'){
enGangTil=1;
}
else {
enGangTil=0;
}
}
int Average=totGjett/antRunder;
cout<<"Hope you had a nice time playing!\n";
cout<<"Douring " <<antRunder<<" round you played, \n";
cout<<"your average was "<<Average<<" attempts per round.\n";
cout<<" "<<endl;
}