Mixing input methods is always a disaster waiting to happen, especially when you mix those which attempt to parse the input with those which just read the input. The different ways in which white-space and newlines get treated will catch you out.
Read a whole line into a string (preferably a std::string) then extract information from the string in memory.
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main ( ) {
string input;
if ( getline(cin,input) ) {
istringstream convert(input);
int result;
if ( convert >> result ) {
cout << "Success = " << result << endl;
} else {
cout << "Not an integer" << endl;
}
} else {
cout << "No input" << endl;
}
return 0;
}
It might look like a lot of code compared to your 1-liner, but if you were to make your input bomb-proof, you'd be looking at a similar amount of code.
If your program does a lot of interactive I/O, then you'd create a number of wrapper functions to take care of some of the repetitiveness.
ill try that later im going to try and modify it to search for strings (i hope to use a text file to hold information for a simple text based game im making)
Okay heresa question, using the code above, how could i get it to print each string on its own line, automatically enter them at the end and display them all
I know i use a loop but its the I/o im confused about