What happens when one assigns a string value to an integer variable in c++?
c++, variables
Solution
A stream has a internal state. If an input fails the state is set to indicate the error and all further inputs will fail, unless that state is cleared.
In your case you should initialize foo and bar with zero.
Test the stream state:
if( ! (cin >> foo)) {
// Error
}
Same with bar
If you have resolved the input failure you can use `cin.clear()` to clear the error state.
Problem
I just started learning c++ (so forgive me for my noobish query). Here's some code I wrote as an exercise: ``` #include<iostream> int main() { using namespace std; int foo; cin >> foo; int bar; cin >> bar; cout << "foo plus bar is " << foo+bar<< endl; return 0; } ``` Now this code works perfectly fine when both the inputs are numbers. But when I enter a string for the first input (just to see what happens) the program does not ask me for the second input and `cout`s the result as `foo plus bar is 0`. What I'd like to know is why is the program skipping my second `cin` when I assign a string to an integer variable. Thanking you for your help.