I'm told that I should, unless beyond necessary, only use either of the two in my code, cin or getline.
Cin I am told creates unusual errors and such (Not checking type of input, etc )
I understand getline() is much more flexible with strings and characters, but how about numeric types? I mean, I can parse a getline string to a numeric value, but isn't there a safer route? Question being, which method should I take for both string AND numerical input?
int inputReturn(int x);
int defaultValue = 0;
int main()
{
//Getting numerical input w/getline()
string input = "";
while(true)
{
cout << "Enter a value: ";
getline(cin, input);
stringstream myStream(input);
system("CLS");
if(myStream >> defaultValue)
{
system("CLS");
break;
}
cout << "Invalid input. Try again!" << endl << endl;
}
inputReturn(input); //Converted to numeric value, but still type of string (Error)
system("CLS");
return 0;
}
int inputReturn(int x)
{
return x*2;
}
In this example, I parse string input to a numeric value then use the value as a parameter of int and get an error of string to int.
So, question - what should I use for these data types or can I use both?
I hope you guys understand my questioning.