Reputation: 23
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.
Upvotes: 1
Views: 630
Reputation: 3
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).
Upvotes: -1
Reputation: 113
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
.
Upvotes: 0
Reputation: 45
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;
}
Upvotes: 0
Reputation: 37
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;
}
Upvotes: 0
Reputation: 100
#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;
}
Upvotes: 1
Reputation: 9888
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?
Upvotes: 0
Reputation: 3181
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 int
s to string
Upvotes: 0
Reputation: 361605
#include <string>
...
string firstname;
string lastname;
int
values hold numbers. To store names, use string
s.
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.
Upvotes: 9