having problems with std::cin reading white spaces

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

Last edited on
Last edited on
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>
using namespace 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;
}
Last edited on
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;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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;
}
thanks yanson, std::cin.ignore(); did not work i re wrote it so it asked for the number and street name separate which worked

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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;
}
Try it like this. You should be able to enter first and last names, and a street name of more than one word:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
}
Last edited on
Topic archived. No new replies allowed.