-1

Hello I'm having a problem where when I call my function after the user enters "Y" to start the game, the cout gets read but it becomes an infinite loop. It works just fine when you enter "N" or something thats not supposed to be entered. I am using a header file called functions to well, put all the functions if that has anything to do with it. I am still in the very early learning stages of programming, and run into so many speed bumps and just not quite sure where to turn. Any help is appreciated. (P.S. I have not yet started on the gameStart() function just because of this problem. That's not whats it's going to be in the end.)

#ifndef FUNCTIONS_H;
#define FUNCTIONS_H
#include <iostream>
using namespace std;

void startScreen()
{
void gameStart();
char answer;

cout << "Welcome to __________\n\n";
cout << "This is my fisrt ever actual program I made out of my own free will lol.\n";
cout << "It is a Text-Based Adventure game. In this game you will make a character,\n";
cout << "and explore the land of Spelet, battling enemies, leveling up, getting loot,\n";
cout << "and learning skills! You do not need to capitalize anything but your character\n";
cout << "name. If a question has (something like this), those are the choices for that \n";
cout << "interaction! Thank you for trying out my terrible little game! :)\n";
cout << "I really hope y'all enjoy it!\n\n";

cout << "Would you like to play?\n";
cin >> answer;

do
{
    if (answer == 'Y' || answer == 'y')
    {
        gameStart();
    }
    else if (answer == 'N' || answer == 'n')
    {
        cout << "Program will now close...\n";
        system("pause");
        exit(0);
    }
    else
    {
        cout << "Enter a Y for yes or an N for no.\n";
        cout << "Would you like to play?\n";
        cin >> answer;
    }
}
while (answer != 'N', 'n' || 'Y', 'y');
}

void gameStart()
{
cout << "\n\"BOOM-BOOM-BOOM...\"\n\n" << endl;
}

#endif
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Hoeins
  • 49
  • 4

2 Answers2

3

maybe you need:

while (answer != 'N' && answer != 'n' && answer != 'Y' && answer != 'y')
halong
  • 70
  • 6
1

The comma operator doesn't do what you think it does. It "discards the result," as my link says.

You want the && operator (AND operator) instead:

while (answer != 'N' && answer != 'n' && answer != 'Y' && answer != 'y');