making only 3 attempts to guess the value

#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
int main()
{
string user,key,name,selection,item;
int atmp,guess,password,A,B,C,value;
do
{
cout<<"Username: ";
cin>>user;
cout<<"Password: ";
cin>>password;
}
while(user!="admin" or password!=1234);
system("cls");
cout<<"*Welcome to the game*"<<endl;
cout<<"---------------------"<<endl;
cout<<"press any key to continue";
cin>>key;
system("cls");

A=1000;
B=2000;
C=100000;
cout<<"***NAME THE PRICE***"<<endl;
cout<<"---------------------"<<endl;
cout<<"A. Standard Fan"<<endl;
cout<<"B. Microvawe"<<endl;
cout<<"C. UHD TV"<<endl;
cout<<"enter selection: ";
cin>>selection;
cout<<"---------------------"<<endl;
if (selection=="A")
{
item="Standard fan";
value=1000;
}
else if (selection =="B")
{
item="Microvawe";
value=2000;
}
else if (selection=="C")
{
item="UHD TV";
value=100000;
}
cout<<"guess the price of "<<item<<endl;
do
{
cout<<"enter guess: ";
cin>>guess;
}
while (guess!=value or guess);
cout<<"---------------------"<<endl;
cout<<" *congratulations*"<<endl;
cout<<" yeah we made it";
return 0;
}
JSYK, your question hasn’t received much attention because your code is not in [code] blocks and is very hard to read, and you haven’t actually asked a question.

Nevertheless, the answer is: use a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool guess_value( int value )
{
  cout << "Enter your best guess: ";
  for (int n = 0; n < 3; n++)
  {
    int guess;
    cin >> guess;
    if (guess == value) return true;

    cout << "Sorry. Try again: ";
  }
  return false;
}
1
2
3
4
5
6
7
8
9
10
11
12
if (guess_value( value ))
{
  cout << "---------------------\n";
  cout << " *congratulations*\n";
  cout << " yeah we made it\n";
}
else
{
  cout << "---------------------\n";
  cout << " So sorry. You are out of guesses.\n";
  cout << " :-( \n";
}

Hope this helps.
Topic archived. No new replies allowed.