tags:

views:

565

answers:

6

Hello there,

I'm trying to collect user's input in a string variable that accepts whitespaces for a specified amount of time.

Since the usual cin >> str doesn't accept whitespaces, so I'd go with std::getline from <string>

Here is my code:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        string local;
        getline(cin, local); // This simply does not work. Just skipped without a reason.
        //............................
    }

    //............................
    return 0;
}

Any idea?

+1  A: 

Are you hitting enter? If not get line will return nothing, as it is waiting for end of line...

hhafez
A: 
  • Is n properly initialized from input?
  • You don't appear to be doing anything with getline. Is this what you want?
  • getline returns an istream reference. Does the fact that you're dropping it on the ground matter?
John at CashCommons
Yes. I tried to print it, and `n` is initialized properly. --- I cut off the codes after getline.
djzmo
+1  A: 

My guess is that you're not reading n correctly, so it's converting as zero. Since 0 is not less that 0, the loop never executes.

I'd add a bit of instrumentation:

int n;
cin >> n;
std::cerr << "n was read as: " << n << "\n"; // <- added instrumentation
for // ...
Jerry Coffin
No. The loop do execute. I'm using VC++ 2008, so I'd be able to debug by tracing the code line by line.
djzmo
A: 

On which compiler did you try this? I tried on VC2008 and worked fine. If I compiled the same code on g++ (GCC) 3.4.2. It did not work properly. Below is the versions worked in both compilers. I dont't have the latest g++ compiler in my environment.

int n;
cin >> n;
string local;
getline(cin, local); // don't need this on VC2008. But need it on g++ 3.4.2. 
for (int i = 0; i < n; i++)
{
    getline(cin, local);
    cout << local;
}
Jagannath
+6  A: 

You can see why this is failing if you output what you stored in local (which is a poor variable name, by the way :P):

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        string local;
        getline(cin, local);
        std::cout << "> " << local << std::endl;
    }

    //............................
    return 0;
}

You will see it prints a newline after > immediately after inputting your number. It then moves on to inputting the rest.

This is because getline is giving you the empty line left over from inputting your number. (It reads the number, but apparently doesn't remove the \n, so you're left with a blank line.) You need to get rid of any remaining whitespace first:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    cin >> ws; // stream out any whitespace
    for(int i = 0; i < n; i++)
    {
        string local;
        getline(cin, local);
        std::cout << "> " << local << std::endl;
    }

    //............................
    return 0;
}

This the works as expected.

Off topic, perhaps it was only for the snippet at hand, but code tends to be more readable if you don't have using namespace std;. It defeats the purpose of namespaces. I suspect it was only for posting here, though.

GMan
I tend to explicitly use the std:: namespace prefix myself, but there is nothing wrong with a using declaration in a cpp (not h) file.
Jon Reid
Perhaps in such a simple example. There's *a lot* of stuff in the namespace `std`, though. Function-level is the most global I'll go with `using` directives.
GMan
A: 

The important question is "what are you doing with the string that gives you the idea that the input was skipped?" Or, more accurately, "why do you think the input was skipped?"

If you're stepping through the debugger, did you compile with optimization (which is allowed to reorder instructions)? I don't think this is your problem, but it is a possibility.

I think it's more likely that the string is populated but it's not being handled correctly. For instance, if you want to pass the input to old C functions (eg., atoi()), you will need to extract the C style string (local.c_str()).

Max Lybbert