Insert words with space between into char array

When I enter my address
for example:
Headquarters 1120 N Street Sacramento 916-654-5266

Because of the space between words, it also enters the next two variables
city state

How to insert all the words include space into char array?

Than you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
int main()
{
	BookData obj;
	int index = 0;

	for (int i = 0; i < 99; i++)
	{
		char decision = '\0';
		char firstName[99] = "\0";
		char lastName[99] = "\0";
		char address[99] = "\0";
		char city[99] = "\0";
		char state[99] = "\0";

		obj.printMenu();
		cin >> decision;
		while (decision != 'E' && decision != 'e' && decision != 'D' && decision != 'd' && decision != 'Q' && decision != 'q')
		{
			cout << "invalid character, please enter again: ";
			cin >> decision;
		}
		switch (decision)
		{
		case 'E':
		case 'e':
			cout << "Enter your first name: ";
			cin >> firstName;
			obj.setFirstName(firstName, index);

			cout << "Enter your last name: ";
			cin >> lastName;
			obj.setLastName(lastName, index);

			cout << "Enter your address: ";
			cin >> address;
			obj.setAddress(address, index);

			cout << "Enter your city: ";
			cin >> city;
			obj.setCity(city, index);

			cout << "Enter your state: ";
			cin >> state;
			obj.setState(state, index);
			index++;
			break;

		case 'D':
		case 'd':
			obj.displayAllNames();
			break;
		case 'Q':
		case 'q':
			return 0;
			break;
		}
	}
	return 0;
}
Use getline rather than cin

While I'm here, don't use char array. Use string. I really mean it.
Last edited on
Hello rokij6698,

Your use of cin >> decision works fine for variable defined as a single character. But when you try to use it for a string with spaces it will only extract from the input buffer up to the first white space leaving the rest of the string in the input buffer.

For things like "first (space) last" name and an address it is better to use std:getline(std::cin, address);. This will extract everything from the input buffer including the new line character.

Mixing "std::cin" and "std::getline(...)" in a program is fine as long as you use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. after the "std::cin" to clear the input buffer otherwise the "std::getline(...)" will extract the newline character and continue on with the program.

Two other observations:

char firstName[99] = "\0"; can be shortened to char firstName[99]{}; where the empty {}s will do the same thing.

A "std::string" would be a better choice for your string variables instead of the character arrays and they are much more flexible. Unless you are stuck with using character arrays.

Suggestion: include the header file "<cctype>" and you could follow "cin >> decision" decision = std::toupper(decision);. This way you will always be using an upper case letter for the while loop and the switch.

A few blank lines in the program would help also. An example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	obj.printMenu();

		cin >> decision;
                decision = std::toupper(decision);
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

		while (decision != 'E' && decision != 'e' && decision != 'D' && decision != 'd' && decision != 'Q' && decision != 'q')
		{
			cout << "invalid character, please enter again: ";
			cin >> decision;
		}

		switch (decision)
		{
		case 'E':

This makes the code much easier to read when all the lines are not run together.

Hope that helps,

Andy
Topic archived. No new replies allowed.