This is my first year of BIT and im doing C++ for first year atm.
Now i have this assignment which is i have to generate random number, and prompt the user to quess the number. when they quest it right i have to prompt that they are right and ask if they want to plau again. i have to put it into function call here is the requirement. "For each quess entered by the user, the program should call a funtion to determine wether the random number is greater or smaller than the user's quess. Here is my complete code so far without function call.
//---------------------------------------------------------------------------
#include <iostream>
#include <conio>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#pragma hdrstop
using namespace std;
#include <tchar.h>
//---------------------------------------------------------------------------
#pragma argsused
int main()
{
int answer;
int quess;
char playAgain;
srand( time( 0 ) );
answer = 1 + rand() % 10;
cout << "Quess a number from 1 to 10: ";
cin >> quess;
while ( quess != answer )
{
if ( quess > answer )
{
cout << "Too high. try again: ";
cin >> quess;
}
else
if ( quess < answer )
{
cout << "too low. try again: ";
cin >> quess;
}
}
cout << answer << "\nExcellent quess!" << "\nPlayagain?(Y|N)?: ";
cin >> playAgain;
while ( playAgain == 'y' || playAgain == 'Y' )
{
srand( time( 0 ) );
answer = 1 + rand() % 10;
cout << "Quess a number from 1 to 10: ";
cin >> quess;
while ( quess != answer )
{
if ( quess > answer )
{
cout << "Too high. try again: ";
cin >> quess;
}
else
if ( quess < answer )
{
cout << "too low. try again: ";
cin >> quess;
}
}
cout << answer << "\nNice! playagain?(Y|N)?: ";
cin >> playAgain;
}
return 0;
}
//---------------------------------------------------------------------------
And here is the programe with function but it does not meet the requirement.
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <iostream>
#include <conio>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#pragma hdrstop
using namespace std;
#include <tchar.h>
char funtionCall( char, int, int );
//---------------------------------------------------------------------------
#pragma argsused
int main()
{
char playAgain;
int answer;
int quess;
srand( time( 0 ) );
answer = 1 + rand() % 10;
cout << "Quess a number from 1 to 10: ";
cin >> quess;
functionCall( playAgain, answer, quess );
while ( playAgain == 'y' || playAgain == 'Y' )
{
srand( time( 0 ) );
answer = 1 + rand() % 10;
cout << "Quess a number from 1 to 10: ";
cin >> quess;
while ( quess != answer )
{
if ( quess > answer )
{
cout << "Too high. try again: ";
cin >> quess;
}
else
if ( quess < answer )
{
cout << "too low. try again: ";
cin >> quess;
}
}
cout << answer << "\nNice! playagain?(Y|N)?: ";
cin >> playAgain;
}
return 0;
}
//---------------------------------------------------------------------------
//
//
//
/
//---------------------------------------------------------------------------
char funtionCall( char playAgain, int answer, int quess )
{
while ( quess != answer )
{
if ( quess > answer )
{
cout << "Too high. try again: ";
cin >> quess;
}
else
if ( quess < answer )
{
cout << "too low. try again: ";
cin >> quess;
}
}
}
//---------------------------------------------------------------------------
Please any idea i can put it into function call!