Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cin input with a + causes next input to be a string

Tags:

c++

cin

so I'm pretty new to C++ and I'm doing an assignment for my class. I ran into a problem when trying to check if an input is a string or a double/int. So I made a basic program to test it

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
    string hi;
    double hello;

    cin >> hello;

    if (!cin)
    {
        //Strings go here

        cin.clear();
        cin >> hi;
        cout << hi << endl;
    }
    else
    {
        cout << hello << endl;
    }

    cout << "Done!" << endl;
}

So it works for basically when inputting a letter (such as "j" or "a") or a number but when inputting "+" or "-" it waits for the next input then forces it through the string block, even if it is a number. However "*" or "/" are read as strings and don't cause that issue (I'm assuming since they aren't explicitly operators) I assume I am probably missing something. Thank you


Edit: I am testing with single types at a time (such as 123, 1 , d, +) without mixing types, so there won't be any inputs that have a double and a string

As per user4581301's suggestion, I'll put in some examples input and outputs

Inputs:Outputs

"Hello":"Hello"

123:123

"/":"/"

"+" (Input after: 2):"2"

like image 318
Shivum Kumar Avatar asked Dec 10 '25 22:12

Shivum Kumar


1 Answers

The problem

Your programme does not work exactly as intended, because it doesn't take into consideration potentially consumed but lost characters.

Here are different cases that work as expected:

  • abc: the first char read is not numeric, so it's not consumed and cin fails fast. The second reading reads every chars present as a string.
  • 123abc456: the first 123 is read. When a is encountered, it is not consumed since it's not valid numeric. Further reading is stopped, but a numeric value could be read.
  • /123: the first char read is not numeric, so it's not consumed and cin fails. the second reading reads every char present as string.
  • -123 or +123: the first char is considered as a valid numeric char and is read, and then the remainder is read, still as a numeric value. Works as expected if you consider that a double ora an int can be signed. Depending on output formatting, the + might not appear with your output.

Here are the cases that do not work: if the first char is + or - but it is not followed by a valid numeric char. In this case the first char is consumed, but the next char makes the numeric input fail. The remaining chars are then read as string (except the first sign that was already consumed and is lost). Examples:

  • ++123
  • + 123 (the space ends the axtraction of the double that fails, the remainder is read as a string).

Online demo

The solution

The easiest solution is to read the input as a string, then try to convert the string.

For example:

size_t processed;
string hi;
double hello;
cin >> hi;

try {
    hello = stod(hi,&processed); 
    cout<<"number:" <<hello; 
    if (processed<hi.size()) 
        cout << " (followed by something)";
    cout <<endl; 
}
catch (...)   // should be more precise in catching, but it's for the proof of concept 
{
    cout <<"string: "<< hi << endl;
}

Online demo

If you want to consider + and - as alphanumeric, it'd be easy to check if hi is non empty and hi[0] a digit before trying to do the conversion.

Alternatively, you could also do a regex check of the string to see if it matches a numeric format.

like image 73
Christophe Avatar answered Dec 16 '25 10:12

Christophe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!