To store a single char value

I'm having an issue with my code if someone enters y it works fine but I want to stop multiple entries ie: if someone puts yes or a series of yyyy it should run once and not four times. So I've tried lots of different solutions without success

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Emulates two dice throw
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    srand((unsigned)time(0));
    int sum, dice1, dice2, doubleRoll, numberRolls;
    char answer;
    bool endGame;

    endGame = false;
    doubleRoll = 0;
    numberRolls = 0;

    while (endGame==false)
    {
        dice1 = rand() % 6 + 1;   //random number
        dice2 = rand() % 6 + 1;   //random number
        sum = dice1 + dice2 + doubleRoll;

        cout << "\nYou threw " << dice1 << " and " << dice2 << " move " << sum << " spaces!" << endl;
        cout << "\nRoll again (Y)es or (N)o?";
        cin >> answer;

        if (answer == 'Y' || answer == 'y') // to end the game
            endGame = false;
        else
            endGame = true;

    }

    cout << "\nThank you for playing!" << endl;

    return 0;
}
> I want to stop multiple entries
> if someone puts yes or a series of yyyy it should run once and not four times.

Why would you want to prevent that?
My take on this: http://www.cplusplus.com/forum/general/170262/#msg850052

If you must subscribe to the keyboard-with-free-cat-included school of programming, extract and discard buffered input before attempting the next input.
1
2
        std::cin >> answer; // line 28
        std::cin.ignore( 1000, '\n' ) ; // **** added **** 



basically i am just using the first character entered and ignoring the rest.
you can enter as many y as you like and it will only run once.
any other letter entered the program will quit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Emulates two dice throw
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
    srand((unsigned)time(0));
    int sum, dice1, dice2, doubleRoll, numberRolls;
    char answer[1] = {'Y'};
    
    bool endGame;

    endGame = false;
    doubleRoll = 0;
    numberRolls = 0;

    while (endGame==false)
    {   
        if (answer[0] == 'Y' || answer[0] == 'y')      
        {
            dice1 = rand() % 6 + 1;   //random number
            dice2 = rand() % 6 + 1;   //random number
            sum = dice1 + dice2 + doubleRoll;            

            cout << "\nYou threw " << dice1 << " and " << dice2 << " move " << sum << " spaces!" << endl;
            cout << "\nRoll again (Y)es or (N)o?";
                        
            cin >> answer;          
            
        }    
        else
        {
             // to end the game  
            endGame = true;
        }                  
        
    }

    cout << "\nThank you for playing!" << endl;

    return 0;
}
Its to stop invalid entries as i build up the code its just the beginning,
Im very new to programming.


If you must subscribe to the keyboard-with-free-cat-included school of programming, extract and discard buffered input before attempting the next input.


I don't understand what that means
Thanks I got the answer I needed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <ctime>
#include <cstdlib>

int main()
{
    std::srand( std::time(0) );
    int doubleRoll = 0;
    int numberRolls = 0;

    char answer ;
    do
    {
        ++numberRolls ;

        const int dice1 = rand() % 6 + 1;   //random number
        const int dice2 = rand() % 6 + 1;   //random number

        if( dice1 == dice2 ) doubleRoll = 6 ;
        else doubleRoll = 0 ;

        const int sum = dice1 + dice2 + doubleRoll;

        std::cout << "\nYou threw " << dice1 << " and " << dice2 << " move " << sum << " spaces!\n"
                  << "\nRoll again (Y)es or (N)o?";

        std::cin >> answer ;

        // *** extract and discard any buffered input *** //
        // http://www.cplusplus.com/reference/istream/istream/ignore/
        std::cin.ignore( 1000, '\n' ) ; // throw away remaining characters

    } while( answer == 'y' || answer == 'Y' ) ;

    std::cout << "\nThank you for playing!" << '\n' ;
}
Why would you want to prevent that?

Because ambiguous designs like that don't scale very well? If the user wanted to run the program 100 times for example you wouldn't have them type in 'y' that many times. You are far better off prompting them separately and having an embedded loop in which case you certainly would filter the input.

@ OP: Can I propose a slightly different design? How about something closer to how 'ping' works on POSIX systems (the same as "ping -t..." on Windows). Instead of prompting the user and asking if they want to play again, just assume that they do and continue to execute until the app is terminated with 'ctrl+c' which is a standard control combination in shell environments. This eliminates the potentially annoying prompt to the user and marginally simplifies your design loop. If you wanted to be cute about it you could even have the "Thank you for playing" in the destructor of a custom class.
Last edited on
> Instead of prompting the user and asking if they want to play again,
> just assume that they do and continue to execute until the app is terminated with 'ctrl+c'

Using ctrl-c for normal termination of programs is daft to the point of being laughable.

ping (which, by the way, does not have anything to do with POSIX) is a really terrible model for anything other than a purely diagnostic utility, solely used for testing and trouble-shooting.
This program is intended for use in network testing, measurement and management. Because of the load it can impose on the network, it is unwise to use ping during normal operations or from automated scripts.
https://www.freebsd.org/cgi/man.cgi?query=ping&manpath=FreeBSD+10.1-RELEASE
I don't think it's laughable though, I honestly think that ctrl-c is highly underrated. It's an uncomplicated gesture that is easily remembered and performed, "portable" and it allows for the application to exit normally as opposed to a forced termination. I'll admit that it's outside of convention, but there is no technical reason to discourage it other then the possibility that a simple majority of people may not know about it; but that's what text prompts are for.

Regarding the ping thing (heh that rhymed), MacOS and Linux both continue to run ping until the user tells the application to stop. They do this because the POSIX standard tells them to operate the command line interface in a standardized fashion. Windows does not do this, and someone who is only familiar with Windows and not the other two may assume that the normal operating mode of ping is to send five evenly spaced signals to the target host in which case my example would have been completely lost on them. So I'll grant you that ping in and of itself has nothing to do with POSIX, however I was referring to its behavior on a given platform not the application itself.

I know that you're just quoting the documentation, but for the benefit of anyone else that happens to read this; point-to-point ping floods aren't really an issue on modern hardware specs anymore, not to mention the myriad of settings that are in place by default to negate the attack outright. DoS these days requires more finesse than just screaming at the client.
Topic archived. No new replies allowed.