Why are some programmers using string as an input and then converting it?

Viewed 76

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 Answers

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.

Related