void functions

I have an assignment where I need to use functions to make a program that lets you select two different guessing games. I tried putting each game in a void function and when I type in 1 or 2 for which game I want to play it alsys plays the first game. How do I fix this?

i have a pen in my pocket. what color is it ?
kathay14 wrote:
I tried putting each game in a void function and when I type in 1 or 2 for which game I want to play it alsys plays the first game. How do I fix this?

It's hard to say without seeing the code. Do you use an if or switch statement to decide which function to call? Make sure you don't mix characters with integers. 1 is not the same as '1'. The first one is an int and the second is a char. If you compare them they are not equal.

anup30 wrote:
i have a pen in my pocket. what color is it ?

If there is no light in the pocket there can't be any colours. Or you could say the pen is black if you count that as a colour.
#include <cstdlib>
#include <iostream>
sorry here is my codeL

#include <ctime>
using namespace std;


void playGame1()
{
while (true) {
srand(int(time(NULL)));//srand and time function
char answer;
int tries = 0;

do
{
int number_guess = rand() % 100 + 1;//rand function
cout << "choose a number between 1 and 100 and I'll guess it." << endl;
cout << "is " << number_guess << " your number?(y/n)" << endl;
++tries; // calculates how many tries it took the computer
cin >> answer;
if (answer == 'y')
cout << "It took me " << tries << " tries! " << endl;
} while
(answer == 'n'); // continue until the computer is wrong.
while (true){
while (true) {
std::cout << "Would you like to play again (Y/N)? ";
std::cin >> answer;
std::cin.ignore();

if (answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
}
else {
std::cout << "Please enter \'Y\' or \'N\'...\n";
}
}

if (answer == 'n' || answer == 'N') {
std::cout << "Thank you for playing!";
break;
}
else {
std::cout << "\n\n\n";
}
}

std::cout << "\n\nEnter anything to exit. . . ";
std::cin.ignore();
break;
}
}
void playGame2()
{
while (true)
{
int number;
int randomNum;
bool tryAgain = true;
char answer;


cout << "Guess a Number between 1 and 100" << endl;

while (tryAgain)
{

srand(int(time(NULL)));
randomNum = rand() % 100 + 1;


cout << "\nTry and guess my number: " << endl;
cin >> number;
int played = 0;
while (number != randomNum)
{
played++;
if (number < randomNum)
{
cout << "Your guess is too low...try again: " << endl;
cin >> number;
}
if (number > randomNum)
{
cout << "Your guess is too high...try again: " << endl;
cin >> number;
}

}

if (number == randomNum)
{
cout << "Congratulations you have won." << endl;
cout << "You took " << played << " tries." << endl;
break; // finish and ask for y or n
}
}
while (true) {
std::cout << "Would you like to play again (Y/N)? ";
std::cin >> answer;
std::cin.ignore();

if (answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
}
else {
std::cout << "Please enter \'Y\' or \'N\'...\n";
}
}

if (answer == 'n' || answer == 'N') {
std::cout << "Thank you for playing!";
break;
}
else {
std::cout << "\n\n\n";
}
}
{
std::cout << "\n\nEnter anything to exit. . . ";
std::cin.ignore();
}
}
int main()
{
int gameNumber = 0;
//pick whether to:
cout << "Welcome to the Guessing Game! ";
cout << "Would you like to play Game (1 or 2)";
cin >> gameNumber;
if (gameNumber = 1)
playGame1();
if (gameNumber = 2)
playGame2();

}
good observation Peter87. that was intended to explain without seeing the code you cannot figure out the problem. if i ask "what color my car is?" how can you even know that if i have a car?
http://www.cplusplus.com/forum/beginner/144927/
1
2
3
4
if (gameNumber = 1)
playGame1();
if (gameNumber = 2)
playGame2();

You are mistakenly using = (assignment) instead of == (comparison). The code should be:
1
2
3
4
if (gameNumber == 1)
playGame1();
if (gameNumber == 2)
playGame2();


Topic archived. No new replies allowed.