2

I've recently came across this code in C++:

int main(){                                                                                                                  
    std::string a='0';  //I missed the quotes here anyways that wasn't 
                        //really necessary for the question                                                                                                       
    std::cout<<"Enter your number: ";                                                                                        
    getline(std::cin,a);                                                                                                     
    int ia=std::stoi(a);                                                                                                     
                                                                                                                             
    return 0;                                                                                                                
}                                                                                                                                 

So why would someone write code like that, when you can write it like this:

int main(){                                                                                                                  
    int a=0;                                                                                                                 
    cout<<"Enter a number: ";                                                                                                
    cin>>a;                                                                                                                  
                                                                                                                              
    return 0;                                                                                                                
}                                                                                                                            

(I'm using using namespace std; by the way) Why write longer code like in the first example? Is it more efficient considering time and memory?

  • 1
    `std::string a=0;`? Where did you see this code exactly? If you can provide a link that would be helpful. – cigien Jan 20 '21 at 22:24
  • 2
    https://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it – πάντα ῥεῖ Jan 20 '21 at 22:24
  • @cigien this derek banes guy yt video https://www.youtube.com/watch?v=tT8ICXAO_-4&t=626s –  Jan 20 '21 at 22:41
  • Thank you, but as always, add all relevant information to the question, not as a comment. – cigien Jan 20 '21 at 22:42
  • 1
    Although the code appears similar but not the same to the video you missed the quotes around the 0. – drescherjm Jan 20 '21 at 22:42
  • @drescherjm thanks but it doesn't really answer my question –  Jan 20 '21 at 22:43
  • 1
    No but it makes your code invalid / Undefined Behavior causing people to ask where you got this. It's illegal to pass a null pointer to the constructor of a std::string – drescherjm Jan 20 '21 at 22:43
  • @drescherjm my bad, fixed it –  Jan 20 '21 at 22:47
  • 2
    There are caveats when mixing in streaming operator and getline, and often is a source of problems when those nuances are not accounted for. Often it is best (as in easiest) to stick with one or the other idiom. – Eljay Jan 20 '21 at 22:48
  • 2
    Let's say you expect a number and the person enters "abc". `cin >> x` fails to read it and puts the stream into a fail state. To tell the user that they typed something wrong you need to know why it failed - but you don't know why it failed because you don't know what they typed. If you read it as a string and then convert it you know which of the two things failed and your error message to the user is meaningful. – Jerry Jeremiah Jan 20 '21 at 22:51

1 Answers1

4

There's more than one way to take this kind of input, and believe me, there are a multitude of weird, bizarre, if not just plain wrong ways to do it.

Reading into a std::string means you can do your own validation and surface errors where the conversion failed, or if the input was unexpectedly long or short.

If you read into an int you will get an int but it can obscure errors on input meaning you have fewer opportunities to provide feedback about conversion issues.

For example, with the std::cin >> a method you can input "lol" and you get back 0 which glosses over the fact that what you put in is total trash.

This is why it's often advantageous to read in a whole line into a std::string and do your own parsing, like checking with a regular expression that the format is correct before converting, otherwise surfacing errors.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 4
    Also, `std::cin >> a >> b;` for two ints, `a` will get set to 0 for the `lol`, but `b` will be the old value or still uninitialized, if it hadn't been initialized yet. That one bit me years back. – Eljay Jan 20 '21 at 23:08
  • @Eljay: with an input of `lol` neither a nor b will be modified -- a is 0 in this case (only) because it was previously initialized to 0. – Chris Dodd Jan 21 '21 at 00:10
  • 1
    @ChrisDodd • `a` will be guaranteed to be set to `0`. `b` (because the stream is in a bad state) will not be (re)set to anything. – Eljay Jan 21 '21 at 00:36