1

I am a c++ beginner and I am curious to why this does not work:

#include <iostream>

using namespace std;
int main ()
{
    int firstname;
    int lastname;

    cout << "My name is " << firstname << lastname;
    cin >> firstname >> lastname;
    cout << endl;

    return 0;

}

I want the output to simply be where the user inputs their first name and the last name and it turns out to be as follows:

Example: My name is John Doe.

Nicu Stiurca
  • 8,747
  • 8
  • 40
  • 48
  • 5
    You'd have to do your input BEFORE you do the output. Right now you're saying "My name is..." before you ever asked what the user's name is. – Marc B Feb 18 '14 at 21:41
  • 2
    Because you need to read in the values, then output them. You have that backwards. – OldProgrammer Feb 18 '14 at 21:41
  • 1
    Order in are statements sequential executed. – Kerrek SB Feb 18 '14 at 21:49
  • @downvoter, why? The poster made it extremely clear what his problem is (even included an SSCCE, which far too many questions here lack), specified his expected input / output. Yes, the problem is trivial for anyone with any experience, but that doesn't invalidate his question given his status as a beginner. – Nicu Stiurca Feb 18 '14 at 21:53

8 Answers8

9
#include <string>

...

string firstname;
string lastname;

int values hold numbers. To store names, use strings.

cin >> firstname >> lastname;
cout << "My name is " << firstname << " " << lastname;

Then make sure to read the names before you print them. The cin and cout should be swapped. I've also added a space (" ") in the printout between the two variables.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1
    #include <iostream>
    #include <string>  // so you can use string

    using namespace std;

    int main() {
   string first;
   string last;
   cin >> first;
   cin >> last; // getting input from using and storing it in last
   cout << "My name is " << first << last << endl; // printing out "My name is and " and what you wrote for first and last
return 0;
}
user3742860
  • 100
  • 2
  • 13
0
cout << "My name is ";
cin >> firstname >> lastname;
cout << firstname << " " << lastname;

This should output a single line of:

My name is John Doe

Plus, strings of characters are stored in string types, not int types

So you'd have to include <string>, and change the ints to string

Alex
  • 3,111
  • 6
  • 27
  • 43
0

Name can be of int type change it to std::string

Here is the modified code will produce output as you want.

#include <iostream>

int main ()
{
    std::string firstname;
    std::string lastname;

    std::cin >> firstname >> lastname;
    std::cout << "My name is " << firstname<<" " << lastname<<"\n";

    return 0;

}

Note that the you will have to add " " while printing if you want a white space between your first name and last name.

Try to take input using 'getline(cin,str); if your string contain white space too.

I would suggest you to not to use standard namespace i.e. using namespace std; while writing the code. For more detail please have a look of link provided below

Why is "using namespace std" considered bad practice?

Community
  • 1
  • 1
Shravan40
  • 8,922
  • 6
  • 28
  • 48
  • 1
    Interesting read regarding `using namespace std`, but completely irrelevant to short `hello world` programs like this. – Nicu Stiurca Feb 18 '14 at 23:18
  • I though it too, since he/she is beginner he/she should know these thing too. That's why i though worth to mention it. – Shravan40 Feb 18 '14 at 23:25
0

First I would try to prompt the user to enter their first and last name. Or else how would they know what to enter? And using int type does not help at all because the user would be entering a string and not an integer. Try this ...

#include <iostream>
using namespace std;

int main() {

cout << "Pleas enter your first and last name." << endl;
string name;
cin >> name;

cout << "Hello " << name << endl;

return 0;
}
John
  • 37
  • 5
0

Make sure that the user knows what to input (first and last name) otherwise they will not know what to input. This is the output code you can use:

cout << "Please enter your first and last name." << endl;

So the full code should look something like this:

#include <iostream>
#include <string>

using namespace std;
int main ()
{
    string firstname;
    string lastname;

    cout << "Please enter your first and last name: " << endl;
    cin >> firstname >> lastname;
    cout << "My name is " << firstname << " " << lastname;
    cout << endl;

    return 0;
}
amykp
  • 45
  • 12
0

You are doing

int firstname;
int lastname;

meaning that you want to get an integer value, however you want a string. So, replace the int with std::string or string in your case. Also, remember to #include <string> to get the string functionality. After doing this, you should be able to input and return letters. :D

#include <iostream>
#include <string>

using namespace std;
int main ()
{
    string firstname;
    string lastname;

    cout << "My name is " << firstname << lastname;
    cin >> firstname >> lastname;
    cout << endl;

    return 0;

}

Might I add that you generally should not use using namespace std; as it is considered bad practice, it also is not really necessary, you could just type std::.... using namespace std is used if you do not want to type the namespace name every time, but it's generally better to distinguish between which type of functions you want to use with the same names but in different namspaces. and using '\n' for a new line as well instead of endl. This is because endl takes more time to complete than \n.

Legat
  • 93
  • 1
  • 8
-1

I recommend to also include the namespace in your code if you are a beginner. In simple cases like printing strings its readable but a better practice if you're learning to include std::string, std::cin, and std::cout. In this case the :: just means to grab the keyword(right value) from its namespace(left value).