Help me in getline function..

When using vc++ console i got the wrong output. But in Visual studio i got the right output. Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
#include <string>

using namespace std;

void main()
{

    while(1)
    {
        string address;
        cout<<"Enter your address:";
        getline(cin,address);
        cout<<address<<"\n";
     }
}


the result is

Enter your address: Sample1
//it needs 2 enter to show the sample1 and since im in infinite loop, if you enter another value the Enter your address will show first before your latest inputted value.. can anyone help me to read getline using one enter only. Thanks in advance
Last edited on
I don't know but try using std::endl instead of "\n".
 
cout << address << endl;

The difference is that this will also flush the stream.

EDIT: I forgot, you are more likely to need the flush after outputting the first message.
 
cout << "Enter your address:" << flush;
Last edited on
Please show a small sample of your actual output, and what you actually would like your output to be. I get the following output, which is what I would expect when looking at your code.
Enter your address:234 West South Street
234 West South Street
Enter your address:431 South West Street
431 South West Street
Enter your address:


Also note that you shouldn't need to flush() the output stream, the call to getline() should force a flush of the output stream all by it's self unless you're using a non-standard conforming compiler, or you have messed with the stream interfaces. Since you are using void main() it is possible that your compiler doesn't conform to the standard in this situation as well.

If you only want to enter one address, why do you have the code inside an infinite loop?
1
2
3
4
5
6
7
8
Enter your address:Philippines

Philippines//needs 2 enter
Enter your address:America
Enter your address://after i enter america, this is the next output
//and if i input another value, for example Australia, after i enter this is the output
America
Enter your address:


Note: i am using visual c++ editor not visual studio framework.
Last edited on
Topic archived. No new replies allowed.