Maker Pro
Maker Pro

Why isnt my code looping?

Electric-T

Jun 4, 2017
212
Joined
Jun 4, 2017
Messages
212
I wrote this in C++ on visual studio 2017.

Code:
//Guess The Number
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{       

    char restart = 'y';
    while (restart == 'y');
    {



        string player1;
        cout << "Enter Your Name Player 1" << endl;
        getline(cin, player1);

        string player2;
        cout << "Enter Your Name Player 2" << endl;
        getline(cin, player2);



        cout << player1 << "," << "Guess a number between 1 and 100" << endl;
        int player1guess;
        cin >> player1guess;



        cout << "Your turn" << "," << player2 << endl;
        int player2guess;
        cin >> player2guess;





        int correctguess;
        correctguess = 42;



        if (player1guess == correctguess)

            cout << player1 << " " << "wins!" << endl;



        else if (player2guess == correctguess)

            cout << player2 << " " << "wins!" << endl;



        else {

            cout << "You both lose :)" << endl;
        }

        cout << "Play Again? y/n" << endl;
        cin >> restart;
       
       
    }

    //end game
   




    return 0;

}
 

Rixen

Feb 16, 2016
98
Joined
Feb 16, 2016
Messages
98
Hi,

Remove the semicolon here..

while (restart == 'y');

to

while (restart == 'y')

Edit: Fun assignment that one, I had the exact same in the C class a couple months ago :D
 

Electric-T

Jun 4, 2017
212
Joined
Jun 4, 2017
Messages
212
Lol thank you.
Entire code v.s. misplaced semicolon.
Semi colon everytime lol
 

Rixen

Feb 16, 2016
98
Joined
Feb 16, 2016
Messages
98
Hey so i spoke too soon....its still not looping.

Uhh.. while it's abit wonky it does seem to be looping for me.

6bFLi08.png
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,728
Joined
Nov 17, 2011
Messages
13,728
Maybe Y vs. y? Your code doesn't contain any checks for plausibility.
You could make the check case insensitive by using tolower():
Code:
cin >> restart;
restart = tolower(restart);
I'm not familiar enough with C++ to know whether this would work in short form:
Code:
tolower(cin) >> restart;
??
 

Electric-T

Jun 4, 2017
212
Joined
Jun 4, 2017
Messages
212
Maybe Y vs. y? Your code doesn't contain any checks for plausibility.
You could make the check case insensitive by using tolower():
Code:
cin >> restart;
restart = tolower(restart);
I'm not familiar enough with C++ to know whether this would work in short form:
Code:
tolower(cin) >> restart;
??
Im still pretty new to c++ too. I guess its worth a try but seeing as it works for Rixen and not for me im thinking its my compiler or something.
 

hevans1944

Hop - AC8NS
Jun 21, 2012
4,886
Joined
Jun 21, 2012
Messages
4,886
seeing as it works for Rixen and not for me im thinking its my compiler or something.
@Rixen didn't post the code he actually compiled and used, just a screen shot of the program running. Don't go assuming there is anything wrong with your compiler or something. Check your code again for syntax and logic errors. Single-step through it if possible and examine whether the variables change as you expect them. If necessary, insert break-points and print out intermediate values of variables.

And what kind of program only allows one "guess" per player? Surely either or both players always guess wrong 99 times out of 100 tries. A decent program would provide feedback to the players (your guess is too high, or your guess is too low) and limit the number of guesses with a count-down variable that is initialed to a number slightly larger than a binary tree search requires. But you are a long way from writing a program like that, aren't you?
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,728
Joined
Nov 17, 2011
Messages
13,728
Y vs y can easily be checked without altering the code: check your input: do you enter upper case or lower case letters?

For debugging it helps simply to output the received input to verify it was read corrrectly. Something like:
Code:
cout << "Play Again? y/n" << endl;
        cin >> restart;
cout << "Character received: " << restart; //for debugging only
 

Electric-T

Jun 4, 2017
212
Joined
Jun 4, 2017
Messages
212
@Rixen didn't post the code he actually compiled and used, just a screen shot of the program running. Don't go assuming there is anything wrong with your compiler or something. Check your code again for syntax and logic errors. Single-step through it if possible and examine whether the variables change as you expect them. If necessary, insert break-points and print out intermediate values of variables.

And what kind of program only allows one "guess" per player? Surely either or both players always guess wrong 99 times out of 100 tries. A decent program would provide feedback to the players (your guess is too high, or your guess is too low) and limit the number of guesses with a count-down variable that is initialed to a number slightly larger than a binary tree search requires. But you are a long way from writing a program like that, aren't you?
Yes im just started learning this maybe 4 days ago haha. I plan to evolve the program as time goes on. For instance, im also trying to add a rand() function to make "correct guess" different everytime the code runs. And eventually what you said, give the players more tries. Maybe even say who was closer. But as for now, i cant even make a successful loop haha. One step at a time
 

Rixen

Feb 16, 2016
98
Joined
Feb 16, 2016
Messages
98
Here was my solution to that similar assignment, written in standard C, it's a really fun one once you get going, the professor jokingly said we could attempt a highscore list.. That was really tricky, with the file handling etc.
but well, I got carried away :)

Maybe you can use this for some ideas..
5BtDW2t.png
dzCyU0A.png
EaQFodp.png


Edit: You can find really good explanations on srand() and rand() on tutorialspoint.. :)
 
Last edited:

Electric-T

Jun 4, 2017
212
Joined
Jun 4, 2017
Messages
212
Here was my solution to that similar assignment, written in standard C, it's a really fun one once you get going, the professor jokingly said we could attempt a highscore list.. That was really tricky, with the file handling etc.
but well, I got carried away :)

Maybe you can use this for some ideas..
5BtDW2t.png
dzCyU0A.png
EaQFodp.png


Edit: You can find really good explanations on srand() and rand() on tutorialspoint.. :)
This looks like a worthwhile challenge :)
As i get better i will give this a try!
 

fnelson

Nov 28, 2017
3
Joined
Nov 28, 2017
Messages
3
It was long ago I've programmed in C++, but I remember that I had some problems in context of char/string variables input. More specially, in particular situations some values remain in application buffer and they are "automaticly" assigned to char/string variables.

Unfortunally, I'm not sure how to address that issue (C++ is not the programming language that I'm best at :/ ), but Google is your friend :)
 
Top