3

The issue that I'm having is that if you input any string the cin will assign the int to 0. An interesting finding is that if you later take cin to a string you get the entire string you put in for the int. cin.fail() always returns true for some reason, even with cin.ignore(), etc and if( cin >> startingPosition ) also always returns true. So, how do I get it to catch an even recognize that it's a string and not an int? As in, how do I have it loop again if it is a string?

int getUserPosition(bool volatileCall = false) {
    cout << "Which slot do you want to drop the chip in (0-8)? " << endl;
    int startingPosition;
    cin >> startingPosition;
    while (startingPosition >= WIDTH || startingPosition < 0) {
    cout << "Invalid slot." << endl << endl;

        if (volatileCall) {
            return -1;
        }
        cout << "Which slot do you want to drop the chip in (0-8)? " << endl;
        cin >> startingPosition;
        cout << startingPosition << endl;

    }
    return startingPosition;
}
  • 2
    Please read [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 22 '18 at 08:36
  • 2
    And please elaborate on your question, or rather *ask* us a question. – Some programmer dude Oct 22 '18 at 08:37
  • *"The issue that I'm having is that if you input any string the cin will assign the int to 0."* I don't see how that's an issue... what else should it do with the invalid input? – Blaze Oct 22 '18 at 08:38
  • @Blaze An alternative could be leaving the int to the previous value, or reporting error. – VLL Oct 22 '18 at 08:40
  • That's right. As for the error, `>>` returns `false` on error. There's a nice answer on validating streaming integers here: https://stackoverflow.com/a/2076144/10411602 – Blaze Oct 22 '18 at 08:41
  • 1
    A small terminology issue: assignment goes in the other direction, from value to object; it assigns 0 to the int. – molbdnilo Oct 22 '18 at 08:49

1 Answers1

0

you must save result from cin

isNumber = (cin >> startPosition);

the whole code will look like

int getUserPosition(bool volatileCall = false) {
    cout << "Which slot do you want to drop the chip in (0-8)? " << endl;
    int startingPosition;
    bool isNumber = (cin >> startingPosition);
    while(isNumber && (startingPosition >= WIDTH || startingPosition < 0)) {
        cout << "Invalid slot." << endl << endl;
        if (volatileCall) {
            return -1;
        }
        cout << "Which slot do you want to drop the chip in (0-8)? " << endl;
        isNumber = (cin >> startingPosition);
        cout << startingPosition << endl;
    }
    return startingPosition;
}
Axbor Axrorov
  • 2,720
  • 2
  • 17
  • 35