So the task is relatively simple; create a program that will generate a random number, and then prompt the user to input a number. If the is lower or greater than the generated number, a message will display whether you're lower or higher than the number. The program also records how many attempts you've made. Every task of the program is to be made as a function.
This is what i've made so far. And it compiles fine.
/* Magic's Number game */
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main ()
{
const int MAX_RANGE = 100; //Setting the max range
//Intializing the Input, answer, and the number of guesses
int answer, guess;
int tryCount = 0;
/* initialize random seed: */
srand ( time(NULL) );
/* generate secret number: */
answer = rand() % MAX_RANGE + 1;
cout << answer << endl; //Comment out this line for a serious game.
do
{
//If the answer is lower or higher than the guess, the tryCount goes up
cout << "Guess the number (1 to 100): " << endl;
cin >> guess;
if (answer<guess)
{
cout << "The secret number is lower" << endl;
tryCount += 1;
}
else if (answer>guess)
{
cout << "The secret number is higher" << endl;
tryCount += 1;
}
}
//If the answer equals the guess, the number of guesses is outputted
while (answer!=guess);
cout << "The number of wrong attempts is: " << tryCount << endl;
//Congratulates the user and exits the program.
cout << "Congratulations!" << endl;
return 0;
}
Theres nothing wrong with it. Everything compiles fine, but each task of the program has to be written as a function. And i'm not exactly sure what they mean by that.
Can anyone help? I think i need separate functions that probably replace the If statements.