Jump to content

my c++ contains a conditional loop but its producing a infinite loop

LIQUIDFOX00200
Go to solution Solved by LIQUIDFOX00200,
1 hour ago, HunterAP said:

Just change:


AskToPlayAgain();
bPlayAgain = AskToPlayAgain;

To:


bPlayAgain = AskToPlayAgain();

When you run the first line in the first snippet of code, the return value of the function is not stored anywhere, and it's just thrown away.

When you run the second line of that same snippet, you're calling the memory address of the function, which for all intents and purposes of this code is simply running the function again.

 

Using parenthesis for function calls is a basic and fundamental part of basically all C based programming. Make sure not to do something like this is future programs: always remember to use parenthesis when you call functions.

 

The second code snippet that is just one line fixes your problem: it only calls the function twice, and makes sure that it is stored in a variable that you can use later, and it has the parenthesis in it to prevent issues with calling the function.

 

The final main function should look like this:


int main()
{ 
    bool bPlayAgain = false;
    do {
    PrintIntro();
    PlayGame();
    bPlayAgain = AskToPlayAgain();
    } 
    while (bPlayAgain);

    return 0; // exit the application
}

 

 

15 hours ago, Unimportant said:

@LIQUIDFOX00200

 

First of all, as already mentioned, you probably want to call AskToPlayAgain only once, but the second call, missing the () results in different behavior:


    AskToPlayAgain();
    bPlayAgain = AskToPlayAgain;  //<== AskToPlayAgain, without (), does not call the function but decays into the function's memory address.
				  //Since the memory address will never be 0 it will always result in bPlayAgain being set to true.

 

 

please take a look at this code:-

 

 

#include <iostream>
#include <string>

using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
bool AskToPlayAgain();

// the entry point for our application
int main()

    bool bPlayAgain = false;
    do {
    PrintIntro();
    PlayGame();
    AskToPlayAgain();
    bPlayAgain = AskToPlayAgain;
    } 
    while (bPlayAgain);

    return 0; // exit the application
}


// introduce the game
void PrintIntro()
{
    constexpr int WORLD_LENGTH = 9;
    cout << "Welcome to Bulls and Cows, a fun word puzzel game.\n";
    cout << "Can you guess the " << WORLD_LENGTH;
    cout << " letter isogram I'm thinking of?\n";
    cout << endl;
    return;
}

void PlayGame()
{
    // loop for the number of turns asking for guesses
    constexpr int NUMBER_OF_TURNS = 5;
    for (int count = 1; count <= NUMBER_OF_TURNS; count++) {
        string Guess = GetGuess();
        cout << "Your guess was: " << Guess << endl;
        cout << endl;
    }
}

string GetGuess()
{
    // get a guess from the player
    cout << "Enter your guess: ";
    string Guess = "";
    getline(cin, Guess);
    return Guess;
}

bool AskToPlayAgain()
{ //Ask the player that if he/she wants to play again or not! 
    cout << "Do you wanna play again? (Y/N)";
    string Response = "";
    getline(cin, Response);
    return (Response[0] == 'y') || (Response[0] == 'Y');
}

 

 

 

 

 

as you can see i programmed a conditional loop (response == 'y') but its producing a infinite loop!!! whatever i press it starts again!!! 

 

 

please help

Link to comment
Share on other sites

Link to post
Share on other sites

I think your issue is with the bool AskToPlayAgain function.  Most likely what it is returning.

 

Ah, ok, reading around a bit:  0 = false, 1 = true.

2023 BOINC Pentathlon Event

F@H & BOINC Installation on Linux Guide

My CPU Army: 5800X, E5-2670V3, 1950X, 5960X J Batch, 10750H *lappy

My GPU Army:3080Ti, 960 FTW @ 1551MHz, RTX 2070 Max-Q *lappy

My Console Brigade: Gamecube, Wii, Wii U, Switch, PS2 Fatty, Xbox One S, Xbox One X

My Tablet Squad: iPad Air 5th Gen, Samsung Tab S, Nexus 7 (1st gen)

3D Printer Unit: Prusa MK3S, Prusa Mini, EPAX E10

VR Headset: Quest 2

 

Hardware lost to Kevdog's Law of Folding

OG Titan, 5960X, ThermalTake BlackWidow 850 Watt PSU

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not a programmer but my brother says:

 

askToPlayAgain is never declared as a variable is it?

 

"

AskToPlayAgain();

bPlayAgain = AskToPlayAgain();

"

 

just needs to be

 

"

bPlayAgain = AskToPlayAgain();

"

Link to comment
Share on other sites

Link to post
Share on other sites

@LIQUIDFOX00200

 

First of all, as already mentioned, you probably want to call AskToPlayAgain only once, but the second call, missing the () results in different behavior:

    AskToPlayAgain();
    bPlayAgain = AskToPlayAgain;  //<== AskToPlayAgain, without (), does not call the function but decays into the function's memory address.
				  //Since the memory address will never be 0 it will always result in bPlayAgain being set to true.

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, LIQUIDFOX00200 said:

bPlayAgain = AskToPlayAgain;

So I missed () in asktoplayagain ?? 

11 hours ago, Ithanul said:

I think your issue is with the bool AskToPlayAgain function.  Most likely what it is returning.

 

Ah, ok, reading around a bit:  0 = false, 1 = true.

 

9 hours ago, Unimportant said:

@LIQUIDFOX00200

 

First of all, as already mentioned, you probably want to call AskToPlayAgain only once, but the second call, missing the () results in different behavior:


    AskToPlayAgain();
    bPlayAgain = AskToPlayAgain;  //<== AskToPlayAgain, without (), does not call the function but decays into the function's memory address.
				  //Since the memory address will never be 0 it will always result in bPlayAgain being set to true.

 

 

10 hours ago, T3500 said:

I'm not a programmer but my brother says:

 

askToPlayAgain is never declared as a variable is it?

 

"

AskToPlayAgain();

bPlayAgain = AskToPlayAgain();

"

 

just needs to be

 

"

bPlayAgain = AskToPlayAgain();

"

 

11 hours ago, Ithanul said:

I think your issue is with the bool AskToPlayAgain function.  Most likely what it is returning.

 

Ah, ok, reading around a bit:  0 = false, 1 = true.

 

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Unimportant said:

@LIQUIDFOX00200

 

First of all, as already mentioned, you probably want to call AskToPlayAgain only once, but the second call, missing the () results in different behavior:


    AskToPlayAgain();
    bPlayAgain = AskToPlayAgain;  //<== AskToPlayAgain, without (), does not call the function but decays into the function's memory address.
				  //Since the memory address will never be 0 it will always result in bPlayAgain being set to true.

 

okay its fixed but now its asking 2 times!! that if you want to play again or not!!! first choice is ignored and second choice will work!

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, LIQUIDFOX00200 said:

okay its fixed but now its asking 2 times!! that if you want to play again or not!!! first choice is ignored and second choice will work!

 

Just change:

AskToPlayAgain();
bPlayAgain = AskToPlayAgain;

To:

bPlayAgain = AskToPlayAgain();

When you run the first line in the first snippet of code, the return value of the function is not stored anywhere, and it's just thrown away.

When you run the second line of that same snippet, you're calling the memory address of the function, which for all intents and purposes of this code is simply running the function again.

 

Using parenthesis for function calls is a basic and fundamental part of basically all C based programming. Make sure not to do something like this is future programs: always remember to use parenthesis when you call functions.

 

The second code snippet that is just one line fixes your problem: it only calls the function twice, and makes sure that it is stored in a variable that you can use later, and it has the parenthesis in it to prevent issues with calling the function.

 

The final main function should look like this:

int main()
{ 
    bool bPlayAgain = false;
    do {
    PrintIntro();
    PlayGame();
    bPlayAgain = AskToPlayAgain();
    } 
    while (bPlayAgain);

    return 0; // exit the application
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, HunterAP said:

Just change:


AskToPlayAgain();
bPlayAgain = AskToPlayAgain;

To:


bPlayAgain = AskToPlayAgain();

When you run the first line in the first snippet of code, the return value of the function is not stored anywhere, and it's just thrown away.

When you run the second line of that same snippet, you're calling the memory address of the function, which for all intents and purposes of this code is simply running the function again.

 

Using parenthesis for function calls is a basic and fundamental part of basically all C based programming. Make sure not to do something like this is future programs: always remember to use parenthesis when you call functions.

 

The second code snippet that is just one line fixes your problem: it only calls the function twice, and makes sure that it is stored in a variable that you can use later, and it has the parenthesis in it to prevent issues with calling the function.

 

The final main function should look like this:


int main()
{ 
    bool bPlayAgain = false;
    do {
    PrintIntro();
    PlayGame();
    bPlayAgain = AskToPlayAgain();
    } 
    while (bPlayAgain);

    return 0; // exit the application
}

 

 

15 hours ago, Unimportant said:

@LIQUIDFOX00200

 

First of all, as already mentioned, you probably want to call AskToPlayAgain only once, but the second call, missing the () results in different behavior:


    AskToPlayAgain();
    bPlayAgain = AskToPlayAgain;  //<== AskToPlayAgain, without (), does not call the function but decays into the function's memory address.
				  //Since the memory address will never be 0 it will always result in bPlayAgain being set to true.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, HunterAP said:

 


AskToPlayAgain();
bPlayAgain = AskToPlayAgain;

 

When you run the second line of that same snippet, you're calling the memory address of the function, which for all intents and purposes of this code is simply running the function again.

No, it doesn't. A function name without the parentheses decays into a pointer to the function's memory address. It does not call/execute the function:

bPlayAgain = AskToPlayAgain;
bPlayAgain = &AskToPlayAgain;  //Both these lines are equivalent.

So, all that line does is assign the memory address of the AskToPlayAgain function to the bPlayAgain bool variable, using normal conversion rules, which will always result in bPlayAgain being set to true.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×