I am finishing up my intro to C++ course at college and thus far, I havent had much trouble. However, I cant seem to grasp functions well enough.
The code I am writing creates a simple guessing game with several functions and the program is supposed to loop. I am going to include the entire code because im not sure exactly where I am going wrong.
// A simple guessing game where the user tries to guess a random number that the program selects. Program also offers guidance for incorrect entries, non-existant configuration
// files and invalid number range.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
void displayMenu();
void playGame();
void configureGame();
void writeConfiguration();
void quitGame();
int initialize(int& num1, int& num2);
int main ()
{
char choice;
displayMenu();
if((choice!='a') && (choice!='b') && (choice!='q'))
{
cout << choice <<" is an invalid Selection! Please try again." << endl;
cin >> choice;
}
switch (choice)
{
case'a': playGame();
break;
case'b': configureGame();
break;
case'q': quitGame();
break;
}
system ("pause");
return 0;
}
int initialize(int& num1, int& num2)
{
int num;
ifstream inputfile;
inputfile.open("C://Temp/Config.txt");
while (inputfile.is_open())
{
if(inputfile.fail())
{
cout << "Error opening config file! Goodbye!" << endl;
system ("pause");
}else
{
inputfile >> num1;
inputfile >> num2;
inputfile.close();
}
srand(time(0));
num=num1+rand()%num2+1;
}
return num;
};
void displayMenu()
{
char choice;
cout << "#########################################################" << endl;
cout << "# Guessing Game v1.0 #" << endl;
cout << "#########################################################" << endl;
cout << " " << endl;
cout << "Please select from the following menu options:" << endl;
cout << " a) Play the game" << endl;
cout << " b) Configure the game" << endl;
cout << " q) Quit the game" << endl;
cin >> choice;
};
void playGame()
{
int num1=0;
int num2=0;
int num;
num=initialize(num1, num2);
int count=0, guess;
cout << "I am thinking of a number between "<< num1 << " and " << num2 <<"..." << endl;
cout << "What is your guess?" << endl;
cin >> guess;
if ((guess<num1) && (guess>=num2))
{
cout << "Your guess is out of range. You just wasted a guess :-(" << endl;
count++;
cout << "What is your guess?" << endl;
cin >> guess;
}elseif (guess<num)
{
cout << "Higher!" << endl;
count++;
cout << "What is your guess?" << endl;
cin >> guess;
}elseif (guess>num)
{
cout << "Lower!" << endl;
count++;
cout << "What is your guess?" << endl;
cin >> guess;
}elseif (guess=num)
{
cout << "Great job! You guessed correctly! It took you " << count << " attempts." << endl;
}
};
void configureGame()
{
int num1, num2;
cout << "Enter the range of numbers to choose from separated by a space: " << endl;
cin >> num1, num2;
if (num1>num2)
{
cout << "Your selection was not valid." << endl;
cout << "Enter the range of numbers to choose from separated by a space: " << endl;
}else
{
writeConfiguration();
}
};
void writeConfiguration()
{
int num1, num2;
ofstream outputFile;
outputFile.open("C://Temp/Config.txt");
outputFile << num1 << endl;
outputFile << num2 << endl;
outputFile.close();
cout << "Write Complete!" << endl;
};
void quitGame()
{
exit(1);
};
Ive been working on it for about 4 days and I either must be missing something obvious or I am entirely too stressed to figure it out.
On top of that, I also have to write another version of the same program using a bool query for the loop process instead of my if statements. Both versions must perform exactly the same. I have so far been avoiding using bool because to be honest, I missed class that day and did not learn how to use it (havent used it all semester - still making an A though).
Any assistance will be greatly appreciated. If you guys need further info, let me know.
taking a quick look, I saw that in displayMenu() you read a char choice from the default input stream but you don't return it, hence since char choice is local to displayMenu() it is destroyed, and the built-in int choice that you declared earlier that you then go on to use is default initialised to garbage. Hence, whatever the user entered in displayMenu() isnt being used.
Redefine displayMenu() to the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
char displayMenu()
{
char choice; //this is local to the function
cout << "#########################################################" << endl;
cout << "# Guessing Game v1.0 #" << endl;
cout << "#########################################################" << endl;
cout << " " << endl;
cout << "Please select from the following menu options:" << endl;
cout << " a) Play the game" << endl;
cout << " b) Configure the game" << endl;
cout << " q) Quit the game" << endl;
cin >> choice;
return choice; //return copy of local variable
};
and assign this returned type to another char choice using:
This changed the program completely - now it wont even compile. The compiler doesnt see char displayMenu() as a function anymore even if I try to assign to another char choice (dont know why).
The reason I didnt return it is because in all of the void functions, the function is not supposed to return anything.
Is there something im missing?
Edit: I changed int choice to char choice and reflected that in the code.
as you can guess this would output Hello, world! because even though myBoolean is set to false
the ! operator means not so the statement is saying if myBoolean is not true execute the statement. for a more indepth explanation read the following: http://www.cplusplus.com/doc/tutorial/variables/
I understand what you are saying about the displayMenu, however, the instructions from the professor state:
---
displayMenu This function does not return anything and does not take in any parameters. It simply prints out the main menu with the various game options. The menu should look like the following:
#########################################################
# Guessing Game v1.0 #
#########################################################
Please select from the following menu options:
a) Play the game!
b) Configure the game
q) Quit the game
>
---
So, I dont really know if im allowed to change that... I dont mean to be argumentative... I have strict guidelines to follow =(
// A simple guessing game where the user tries to guess a random number that the program selects. Program also offers g\
uidance for incorrect entries, non-existant configuration
// files and invalid number range.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <stdexcept>
usingnamespace std;
void displayMenu();
void playGame();
void configureGame();
void writeConfiguration();
void quitGame();
int initialize(int& num1, int& num2);
int main ()
{
displayMenu();
char choice;
cin >> choice;
...
Also, you'll want to add a do-while loop when you play the game since at the moment the program terminates after two attempts:
void playGame()
{
int num1 = 0;
int num2 = 0;
int num = 0;
num = initialize(num1, num2);
int count = 1, guess;
cout << "I am thinking of a number between "<< num1 << " and " << num2 <<"..." << endl;
do
{
cout << "What is your guess?" << endl;
cin >> guess;
if ((guess<num1) && (guess>=num2))
{
cout << "Your guess is out of range. You just wasted a guess :-(" << endl;
count++;
}
elseif (guess<num)
{
cout << "Higher!" << endl;
count++;
}
elseif (guess>num)
{
cout << "Lower!" << endl;
count++;
}
}
while(guess!=num);
if(count > 1)
cout << "Great job! You guessed correctly! It took you " << count << " attempts." << endl;
else
cout << "Great job! You guessed correctly! It took you " << count << " attempt." << endl;
};
I tweaked the program as you suggest and now option 'a' works - except that in the "int initialize" function, if there is no config file, it should read
So if you choose option a with no config file, it should read that error then exit out.
Then you reopen program, choose option B, have enter in the numbers to random between.
After writing the config file, go back to displayMenu() and run the game normally.
Is it just me, or this getting extremely complicated?
Honestly, youre being a bigger help than my classmates and the professor combined. I really do appreciate your assistance.
Lastly, with regards to the option (b) part of the application you have several errors:
In configureGame you should have
cin >> num1 >> num2;
rather than
cin >> num1, num2;.
You also need to pass num1 and num2 to writeConfiguration when you call it in configureGame and redefine & redeclare it to take 2 integer arguments:
void writeConfiguration(int num1, int num2)
and remove the declaration of the local variables int1, int2 since these are default initialized and you will find that regardless of what values you enter, you will end up with two zeros in Config.txt.
void configureGame()
{
int num1, num2;
cout << "Enter the range of numbers to choose from separated by a space: " << endl;
cin >> num1 >> num2;
if (num1 >= num2)
{
cout << "Your selection was not valid." << endl;
cout << "Enter the range of numbers to choose from separated by a space: " << endl;
}
else
writeConfiguration(num1, num2);
};
void writeConfiguration(int num1, int num2)
{
ofstream outputFile;
outputFile.open("Config.txt");
outputFile << num1 << endl;
outputFile << num2 << endl;
outputFile.close();
cout << "Write Complete!" << endl;
};
I tweaked the program as you suggest and now option 'a' works - except that in the "int initialize" function, if there is no config file, it should read
So if you choose option a with no config file, it should read that error then exit out.
Then you reopen program, choose option B, have enter in the numbers to random between.
After writing the config file, go back to displayMenu() and run the game normally.
Is it just me, or this getting extremely complicated?
initialize should throw an exception if no Config.txt file is found and then terminate. The user must run option (b) or supply a correctly formatted Config.txt file themselves (with the correct path and filename) and then run option (a) to play the game.
I think this is what you want. If it isn't I hope it helped. Good luck!
Nobody has mentioned yet that he is putting semicolons after his function definitions. You're only supposed to do that in the declaration of the function. A function should look like this.
1 2 3
void test {
// some code
}
...and not like this:
1 2 3
void test {
// some code
};
The only time you should have a semicolon after a closing bracket is in a class, struct, or enum.
Nobody has mentioned yet that he is putting semicolons after his function definitions.
Well spotted. However, it should be made clear that an additional semicolon is purely benign. But in the interests of efficiency, yes, the semicolon shouldn't be there.