|
-
January 15th, 2009, 11:02 AM
#1
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!
Last edited by Lang; January 15th, 2009 at 11:09 AM.
-
January 15th, 2009, 11:06 AM
#2
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.
-
January 15th, 2009, 11:13 AM
#3
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++;
}
}
-
January 15th, 2009, 11:21 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|