Cin and Spaces

I'm working on a project that has been working out so far. The problem i ran into at the moment is that my program needs to be capable of accepting spaces. I did a quick google search, and found cin.getline, but i don't know how to implement it correctly in my program. If you could point me in the right direction, it would be great.

Atraii

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

#include <ctime>
#include <cstdlib>

using namespace std;

struct myChar
{
	char name[15];		//Name of Character
	int age;		//Age of Character
	int hp;			//Health Points
	int c;			//Constitution
	char items[8];
	myChar *nxt;
};

myChar *start_ptr = NULL;
myChar *current;	//Moves list
int option = 0;

int random_number()
{
	int r;
	srand(time(NULL));
	r = rand() % 15 + 3;
	return r;
}


void add_char()
{
	myChar *temp, *temp2; //Pointers used to fill temporary places.
	temp = new myChar;
	cout << "Enter the name of your character: ";
	cin >> temp->name;
	cout << "Enter the age your character will be: ";
	cin >> temp->age;
	temp->hp = random_number();
	temp->c = 3*temp->hp + 10;
		
		
	temp->nxt = NULL;


	if (start_ptr == NULL)
	{
		start_ptr = temp;
		current = start_ptr;
	}
	else
	{
		temp2 = start_ptr;
		while (temp2->nxt != NULL)
		{
			temp2 = temp2->nxt;
		}
		temp2->nxt = temp;
	}
}
void display_all_char()
	{
		myChar *temp;
		temp = start_ptr;
		cout << endl;
		if (temp == NULL)
			cerr << "There are no characters yet, add one first!" << endl;
		else
		{
			while (temp != NULL)
			{
				cout << "Character Name : " << temp->name;
				if (temp == current)
					cout << " <- Current Character Selected" << endl;
				else
					cout << endl;
				cout << "Age : " << temp->age << endl;
				cout << "Health Points : " << temp->hp << endl;
				cout << "Constitution : " << temp->c << endl;
				
				cout << endl << endl << endl;
				temp = temp->nxt;

			}
		cout << "End of Characters!" << endl;
		}
}

void delete_first_char()
{
	myChar *temp;
	temp = start_ptr;
	start_ptr = start_ptr->nxt;
	delete temp;
}

void delete_last_char()
{
	myChar *temp1, *temp2;
	if (start_ptr == NULL)
		cerr << "There are no characters yet, add one first!" << endl;
	else
	{
		temp1 = start_ptr;
		if (temp1->nxt == NULL)
		{
			delete temp1;
			start_ptr = NULL;
		}
		else
		{
			while (temp1->nxt != NULL)
			{
				temp2 = temp1;
				temp1 = temp1->nxt;
			}
			delete temp1;
			temp2->nxt = NULL;
		}
	}
}

void move_selected_on()
{
	if (current->nxt == NULL)
		cerr << "You are at the last character!" << endl;
	else
		current = current->nxt;
}

void move_selected_back()
{
	if (current == start_ptr)
		cerr << "You are at the first character already!" << endl;
	else
	{
		myChar *previous;
		previous = start_ptr;

		while (previous->nxt != current)
		{
			previous = previous->nxt;
		}
		current = previous;
	}
}

int menu()
{
	int sel;
		
	
	cout << endl;
	cout << "Please select an option : " << endl << endl;
		
	cout << "1. Create a character." << endl;
	cout << "2. Delete the first character." << endl;
	cout << "3. Delete the last character." << endl;
	cout << "4. Select the next character." << endl;
    cout << "5. Select the previous character." << endl;
	cout << "6. Display All Characters." << endl;
    cout << "0. Exit." << endl;

	while(1)
	{
		cout << endl << " >> ";

		string option;
		cin >> option;
		istringstream parseInput(option);
		if(!(parseInput >> sel))
		{
			sel = 0;
		}
		
		if(sel >= 0 && sel <= 6) {return sel;}
		cout << "Please enter a valid number between 0 and 6." << endl;
	};
}


int main()
{
	start_ptr = NULL;
	cout << "Character Creater" << endl;
	cout << "Written by ***********" << endl;

	int selection;
	while ((selection = menu()) != 0)
	{
	  switch (selection)
	    {
	      case 1 : add_char(); break;
	      case 2 : delete_first_char(); break;
	      case 3 : delete_last_char(); break;
	      case 4 : move_selected_on(); break;
          case 5 : move_selected_back();
		  case 6 : display_all_char(); break;
	    }
	}
     return 0;
}
Last edited on
Basically, instead of cin >> var, you'd do something like cin.getline(var);. Check this because I may be wrong: http://www.cplusplus.com/reference/iostream/istream/getline/
There are some things to note when using getline over cin that have to do with input buffering. cin leaves a newline in the buffer after every input because you have to press enter after your input. getline still requires that you press enter, but it will discard the newline from the buffer. Whether this has consequences varies but it is usually only relevant when mixing cin and getline, which is a decidedly bad idea.
The getline() that's not a member function is slightly better.
http://www.cplusplus.com/reference/string/getline/
Topic archived. No new replies allowed.