First question

First of all hello to everyone, I'm new to this forum and also to C++:
I have this question/problem.... I did this program but the problem is that when I input my name as two names (for example Mario Jones), the program just fills the first input with Mario and the second with Jones immediately.
What gets me confused is caused I did a destructor ( } ) and also I specified that I wanted name2 to be cout ......
How can i fix it and more importantly WHY the destructor and the string name2 doesn't work ????
Thanks beforehand

#include <iostream>
#include <string>
using namespace std;

int main ()
{
{ cout << " What is your name ?";
string name;
cin >> name;

cout << "hello, " << name
<< endl << "And what is yours?"; }
{
string name2;
cin >> name2;
cout << "Hello, " << name2
<< "; nice to meet you too!" << endl; }
return 0;
}
operator >> reads one word per time ( it stops reading at the next space )
To get everything the user writes, use getline: getline ( cin, name );
Thanks very much for your reply. Another question please .....
But i taught that when u include a destructor and also a different string name the program should delete the first input and ask for another input.
Or am i mistaken.......? cause this is my main concern not how to have a name and surname as a whole input.
The value of the string doesn't matter, and it would get overwritten anyways. You need to work with the stream ( cin ) not with the string
I'll show an example run of your program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main ()
{
   { 
      cout << " What is your name ?";
      string name;

      cin >> name;



      cout << "hello, " << name
         << endl << "And what is yours?"; 
   }

   {

      string name2;

      cin >> name2;



      cout << "Hello, " << name2
         << "; nice to meet you too!" << endl; 
   }
   return 0;
}


begin scope

declare name

user inputs "John Smith". 
   cin reads up to the next space
   name gets the value "John".
   " Smith" remains on the input stream


end scope, name is destructed

new scope

declared name2

cin looks for input, it finds " Smith"
   it discards the leading spaces
   reads until the next space ( the newline after "Smith" )
   name2 gets the value "Smith"


end scope, name2 is destructed
end of the program
Now i get it ....... Thank you very much for the time consumed to answer my question, much appreciated.
thanks again Bazzy........
Topic archived. No new replies allowed.