hello all, i am new to programming just learning the ropes. i am having a little problem with address part i have put into bold,
its not picking up anything after the first space, i have tried std::cin.get and std::get() but i get errors for them
i cant seem to figure out what i am doing wrong
thank you for your time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your name:" << std::endl;
std::string name;
std::cin >> name;
std::cout << "Enter your age:" << std::endl;
int age = 0;
std::cin >> age;
std::cout << "Enter your address:" << std::endl;
std::string address;
std::cin >> address;
std::cout << "your name is " << name << " you are " << age << " and you live here " << address << std::endl;
system("pause");
return 0;
}
I don't know the answer to your problem, but I would suggest using std library instead of writing std:: everywhere. It would look much cleaner. This is what it would look like, and it has the same result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
usingnamespace std ;//you have to add this, and remove std from everywhere else
int main()
{
cout << "Enter your name:" << endl;
string name;
cin >> name;
cout << "Enter your age:" << endl;
int age = 0;
cin >> age;
cout << "Enter your address:" << endl;
string address;
cin >> address;
cout << "your name is " << name << " you are " << age << " and you live here " << address <<endl;
system("pause");
return 0;
}
thanks Yanson, this is what i have now its working but for some reason it is ignoring numbers at the beginning of the address the user inputs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your name:" << std::endl;
std::string name;
std::cin >> name;
std::cout << "Enter your age:" << std::endl;
int age = 0;
std::cin >> age;
std::cout << "Enter your address:" << std::endl;
std::string address;
std::cin >> address;
std::getline (std::cin,address);
std::cout << "your name is " << name << " you are " << age << " and you live here " << address << std::endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your name:" << std::endl;
std::string name;
std::cin >> name;
std::cin.ignore();
std::cout << "Enter your age:" << std::endl;
int age = 0;
std::cin >> age;
std::cin.ignore();
std::cout << "Enter your address:" << std::endl;
std::string address;
std::getline (std::cin,address);
std::cout << "your name is " << name << " you are " << age << " and you live here " << address << std::endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your name:" << std::endl;
std::string name;
std::cin >> name;
std::cout << "Enter your age:" << std::endl;
int age = 0;
std::cin >> age;
std::cout << "enter your house number:" << std::endl;
int number = 0;
std::cin >> number;
std::cout << "Enter your street/road name:" << std::endl;
std::string address;
std::cin >> address;
std::cout << "your name is " << name << " you are " << age << " and you live here " << number << " " << address << std::endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter your name:" << std::endl;
std::string name;
getline(std::cin, name);
std::cout << "Enter your age:" << std::endl;
int age = 0;
std::cin >> age;
std::cout << "enter your house number:" << std::endl;
int number = 0;
std::cin >> number;
std::cin.ignore(1000, '\n'); // remove trailing newline character
std::cout << "Enter your street/road name:" << std::endl;
std::string address;
getline(std::cin, address);
std::cout << "your name is " << name << " you are " << age
<< " and you live here " << number << " " << address << std::endl;
system("pause");
return 0;
}