Char & string problem after space..

I try both char and string to store 2 words, for example "my dog".

The program after display only "my" and the rest after the space are gone.

Whats wrong ?

1
2
3
4
5
6
7
8
char testMsg[30];
or
string testMsg;

cout<<"enter two words ";
cin>>testMsg;

cout<<testMsg;


Last edited on
>> just reads until the next whitespace.
Use getline:

1
2
3
string testMsg;
cout<<"enter two words ";
getline(cin,testMsg);
I still have a problem...
Thanks Athar, i tested this code to a new project and work perfect but...

1
2
3
4
5
6
7
8
9
10
11
12
13
        string w1;
        string w2;

        cout<<"1st msg: Enter 2 words: ";
        getline(cin,w1);
        cout<<endl;

        cout<<"2nd msg: Enter 2 words: ";
        getline(cin,w2);
        cout<<endl;

        cout<<endl<<"w1: "<<w1<<endl;
        cout<<"w2: "<<w2<<endl<<endl;

But when i run it is some functions(except main and some others) of my program get other output.

1st msg: Enter 2 words:
2nd msg: Enter 2 words: word1 word2


w1:
w2: word1 word2

My variables are again local, any ideas whats the problem ?
Last edited on
I don't see a problem...could you explain what the problem is?
1
2
3
4
const int SIZE = 81;
char adress[SIZE];
cout << "Enter Address : ";
cin.getline(address,SIZE)
cin.getline(name of array, size)
I found the solution.

I just put
cin.ignore();
before the getline

Thanks for your time Athar,L B and LittleQuick!
Last edited on
Topic archived. No new replies allowed.