Problem with getline

I have a problem with the getline. Here's a picture of the problem

https://41.media.tumblr.com/865c79247a91e094a36697bb75c43320/tumblr_nx1rz3JQG21qdp21xo1_1280.png

When I ask the user to enter their name by typing their name next to the "Student: " it just skips to asking for the username without letting the user to type in their full name. I checked the "getline(cin, name1s);" and thought that it was right...but apparently it's not. Is there a way to fix this?


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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// This program writes user input to a file.
#include <iostream>
#include <fstream>        // Needed to use files
#include <string>
#include <cstring>
#include <cmath>
using namespace std;

int main()
{
	int type;
	cout << "Welcome to the CalState Fullerton!\n\n" << "Enter 1 if you are a student, or Enter 2 if you are a teacher.\n\n ";
	cin >> type;
	switch (type)
	{
	case 1:
	{
		ofstream outputFile;
		string name1s;
		// Open the output file
		outputFile.open("File#1.txt");
		// Get the name of student
		cout << "Enter the name of the user.\n";
		cout << "Student: ";
		getline(cin, name1s);
		// Write the names to the file
		outputFile << name1s << endl;
		// Close the file
		outputFile.close();
		cout << "The name was saved to a file.\n";


		ofstream outputFile1;
		string username1s, password1s;
		// Open the output file
		outputFile1.open("File#2.txt");
		// Get username and password
		cout << "Enter username: ";
		cin >> username1s;
		cout << "Enter password: ";
		cin >> password1s;
		// Write the names to the file
		outputFile1 << username1s << endl;
		outputFile1 << password1s << endl;
		// Close the file
		outputFile1.close();
		cout << "The username and password were saved to a file.\n";


		string usernames,
			passwords;
		char option;

		do {

			cout << "Please enter your username." << endl;
			cin >> usernames;
			cout << "Please enter your password. \n";
			cin >> passwords;

			if (usernames == username1s && passwords == password1s)
			{
				"\n";
				cout << "Hello and good luck taking this quiz." << endl;
				"\n";
				int choice, score = 0;
				string name;
				double score1;


				cout << "Please enter your full name." << endl;
				getline(cin, name);

				cout << "Which of the following is a datatype?" << endl;
				cout << "1. int " << endl;
				cout << "2. doubletriple" << endl;
				cout << "3. setdecimal" << endl;
				cout << "4. Valuemore" << endl;
				cout << "\n" << endl;
				cin >> choice;

				if (choice != 1)
				{
					cout << "Sorry, you are incorrect." << endl;
				}
				else
				{
					cout << "Way to go!" << endl;
					score++;
				}
				cout << "\n";
				cout << "What datatype do you need to use when you're dealing with numbers that has decimals?" << endl;
				cout << "1. bool" << endl;
				cout << "2. void" << endl;
				cout << "3. double" << endl;
				cout << "4. char" << endl;
				cout << "\n" << endl;
				cin >> choice;

				if (choice != 3)
				{
					cout << "Sorry, you are incorrect." << endl;
				}
				else
				{
					cout << "Way to go!";
					score++;
				}
				cout << "\n";
				cout << "Which of the following is true about char?" << endl;
				cout << "1. It performs integer division if both operands are integers." << endl;
				cout << "2. It is also called a constant." << endl;
				cout << "3. It is a preprocessor directive" << endl;
				cout << "4. It is used to hold single characters or very small integer values" << endl;
				cout << "\n" << endl;
				cin >> choice;

				if (choice != 4)
				{
					cout << "Sorry, you are incorrect." << endl;
				}
				else
				{
					cout << "Way to go!";
					score++;
				}
				cout << "\n";
				cout << "What is the definition of bool?" << endl;
				cout << "1. Bool represents values that are true or false." << endl;
				cout << "2. It has values that are stored as integers." << endl;
				cout << "3. In bool, false is 0 and true is 1." << endl;
				cout << "4. All of the above." << endl;
				cout << "\n" << endl;
				cin >> choice;

				if (choice != 4)
				{
					cout << "Sorry, you are incorrect." << endl;
				}
				else
				{
					cout << "Way to go!";
					score++;
				}
				cout << "\n";
				cout << "What do you need to put on the header in order to utilize string?" << endl;
				cout << "1. #include (stream)" << endl;
				cout << "2. #include <string>" << endl;
				cout << "3. #include string" << endl;
				cout << "4. #include <iomanip>" << endl;
				cout << "\n" << endl;
				cin >> choice;

				if (choice != 2)
				{
					cout << "Sorry, you are incorrect." << endl;
				}
				else
				{
					cout << "Way to go!";
					score++;
				}

				cout << "\n";
				score1 = score * 100 / 5;
				cout << "The quiz is now over." << endl;
				cout << "Student's name: " << name << endl;
				cout << "Overall quiz score: " << score << " out of 5" << endl;
				cout << "Your percentage on the quiz: " << score1 << "%" << endl;
				cout << "Thank you for taking this quiz. It will be added in your grade." << endl;

				ofstream outputFile3;
				// Open the output file
				outputFile3.open("File#4.txt");
				outputFile3 << "Student name: " << name << endl;
				outputFile3 << "Corrected answers: " << score << " out of 5" << endl;
				outputFile3 << "Grade:  " << score1 << "%" << endl;


				// Close the file
				outputFile3.close();

				cout << "The datas were saved to a file.\n";


				ofstream outputFile4;
				// Open the output file
				outputFile4.open("File#5.txt");
				outputFile4 << "Student name: " << name << endl;
				outputFile4 << "Corrected answers: " << score << " out of 5" << endl;
				outputFile4 << "Grade:  " << score1 << "%" << endl;


				// Close the file
				outputFile4.close();

				cout << "The datas were saved to a file.\n";


			}
			else
			{
				cout << "The user name or password is invalid.  Press Y or y to try again! \n\n";
				cin >> option;

			}

		} while (option == 'y' || option == 'Y');
		break;
	}


	case 2:
	{
		cout << "\n";
		cout << "Welcome Professor. I hope you are doing well." << endl;
		cout << "\n";

		ofstream outputFile;
		string name11t;

		// Open the output file
		outputFile.open("File#1.txt");

		// Get the names of three friends
		cout << "Enter your name please.\n";
		cout << "Name: ";
		getline(cin, name11t);
		// Write the names to the file
		outputFile << name11t << endl;


		// Close the file
		outputFile.close();

		cout << "The names were saved to a file.\n";



		ofstream outputFile1;
		string username1t;
		string password1t;


		// Open the output file
		outputFile1.open("File#2.txt");

		// Get the names of three friends

		cout << "Enter username: ";
		cin >> username1t;
		cout << "Enter password: ";
		cin >> password1t;


		// Write the names to the file

		outputFile1 << username1t << endl;
		outputFile1 << password1t << endl;

		// Close the file
		outputFile1.close();

		cout << "The usernames and passwords were saved to a file.\n";


		string usernamet,
			passwordt;
		char option1;

		do {

			cout << "Please enter your username." << endl;
			cin >> usernamet;
			cout << "Please enter your password. \n";
			cin >> passwordt;

			if (usernamet == username1t && passwordt == password1t)
			{
				"\n";
				cout << "Hello Professor. Do you want to edit your quiz or do you want to view your student's scores on it.\n\n";


			}

			else
			{
				cout << "The user name or password is invalid.  Press Y or y to try again! \n\n";
				cin >> option1;

			}

		} while (option1 == 'y' || option1 == 'Y');
		break;
	}
	default: cout << "Invalid Selection!\n";
	}

	return 0;
Topic archived. No new replies allowed.