Click here to Skip to main content
15,912,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody , is there anybody to help me with this code below ?
i just need to do a program which ask for the age , and the name , eg;
how old are you , then i get the age, after i ask again and what is your name i get my name .. how can i do it, please help me improve this code ?

#include <iostream>
using namespace std;
int main()
{
int age = 0;
int name =0;
cout << "how old are you ?" << endl;
cin >> age;
cout <<" your name ?" <<endl;
cin="">> name;
cout <<"your name " << name <<"is !" << endl;
cout << "You ! Have " << age << " Years old !" << endl;
system ("pause");
return 0;

}
Posted

When you prompt someone for their name, they are more likely to enter Patrick then they are 6. Your program can't handle that since you made name as an int. Make the type of your name variable something suitable for the purpose. You might use std::string. Of course if you do that, you will also have to include the appropriate header file. If you don't know what that is, you can find it in the documentation.

It is more usual to prompt for the name before the age. It doesn't make a difference to the program, but it would fit better with what more users expect.
 
Share this answer
 
please help get a solution to this error
when i compile i get an error saying that : no operator found which takes a right hand operand of type std::string (or there is no acceptable conversation )
here is my code below i got a suggestion from one of the answer posted on my post
#include <iostream>
using namespace std;
int main()
{
int age = 0;
string name =" ";
cout << "What is your name ?" << endl;
cin >> name;
cout <<" How old are you ?" <<endl;
cin="">> age;
cout <<"your name is: " << name <<" \n " << endl;
cout << "and you are : " << age << " old !" << endl;
system ("pause");
return 0;

}
 
Share this answer
 
do not declare integer if you try to input string toinks!!!!
try this

int main()
{
int age=0;
string name="";
cout << "What is your name ?" <<endl;
cin>> name;
cout << "how old are you ?" <<endl;
cin>> age;
cout << "Your name is:" << name << "\n" <
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900