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;
}
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:
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