std::cin.getline() space and new line setting input
Hey there.
What I'm trying to accomplish is simply. A user enters the input '2 1 3 18' (not including the quotes)
and all 4 numbers are put into an array.
This is my code thus far (note variables are set up properly etc the only issue I'm having is saving the input)
Code:
for (int i = 0; i < m; ++i){
counter = 0;
while (counter < n){
cin.getline(input, 100, ' \n'); //so either a space or a new line is like pressing enter
aMatrix[i][counter] = (int)input; //I wanted to put (long double)input; but that doesn't work - any ideas?
counter++;
}
}
Also running the code line for line, the first time cin.getline() is run I'm unable to enter anything.
Thanks in advance!
Re: std::cin.getline() space and new line setting input
Fundamentally, you can't put both a space and a newline inside a char literal. It's one or the other, not both. In order to pass multiple chars the function would need to accept a const char*, which it doesn't.
What you want is to make a single call to getline() in the outer loop, and then read from the result using an istringstream in the inner loop.
Re: std::cin.getline() space and new line setting input
I'm not exactly sure what you mean by your comments concerning
"space or enter", but you can also read directly into the variable:
Code:
for (int i = 0; i < m; ++i)
{
counter = 0;
while (counter < n)
{
cin >> aMatrix[i][counter];
counter++;
}
}
Re: std::cin.getline() space and new line setting input
One of the requirements is that all the numbers for each equation are on the same line.