I have a problem with the getline function

I'm trying to use the getline function, but it skips the first option for some reason, I don't know why. Can anyone help me please? Thanks in advance!

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
  // This is a program that for the two users (student, teacher)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
	ifstream inputFile;
	ofstream outputFile;
	char choice;
	string person,
		   username,
		   password,
		   STUDENT_USERNAME,
		   STUDENT_PASSWORD,
		   TEACHER_USERNAME,
		   TEACHER_PASSWORD,
		   question,
		   answer;
	int option,
		numberOfQuestions,
		counter = 1,
		questionNumber = 1;

	cout << "Are you a teacher or a student?\n";
	cin >> person;

	while (person != "student" && person != "Student" && person != "teacher" && person != "Teacher")
	{
		cout << "Please choose a valid option." << endl;
		cin >> person;
	}

	if (person == "student" || person == "Student")
	{
		cout << "Welcome, student!\n";
		cout << "Please register your username.\n";
		cin >> STUDENT_USERNAME;
		cout << "Please register your password.\n";
		cin >> STUDENT_PASSWORD;
		cout << "\nYour registration is complete! Thank you for registering!" << endl;
	}

	if (person == "teacher" || person == "Teacher")
	{
		cout << "Welcome, teacher!\n";
		cout << "Please register your username.\n";
		cin >> TEACHER_USERNAME;
		cout << "Please register your password.\n";
		cin >> TEACHER_PASSWORD;
		cout << "\nYour registration is complete! Thank you for registering!" << endl;
	}

	cout << "\nPlease log in.\n";
	cout << "Username: ";
	cin >> username;
	cout << "Password: ";
	cin >> password;

	while (!(username == STUDENT_USERNAME && password == STUDENT_PASSWORD) && 
		!(username == TEACHER_USERNAME && password == TEACHER_PASSWORD))
	{
	cout << "\nPlease log in.\n";
	cout << "Username: ";
	cin >> username;
	cout << "Password: ";
	cin >> password;
	}

	if ((username == STUDENT_USERNAME) && (password == STUDENT_PASSWORD))
	{
		cout << "Welcome Student! Would you like to take the test? Choose Y or N\n";
		cin >> choice;
		switch (choice)
		{
			default: while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'n') 
					 {
						cout << "\nPlease choose a valid option." << endl;
						cin >> choice;
						break;
					 }
			case 'y':
			case 'Y': cout << "What is 1+1?" << endl;
				break;
			case 'n':
			case 'N': cout << "" << endl;
				break;
		} 
	}

	if ((username == TEACHER_USERNAME) && (password == TEACHER_PASSWORD))
	{
		cout << "Welcome Teacher! What would you like to do?" << endl;
		cout << "1. Write a test." << endl;
		cout << "2. Check the test scores." << endl;
		cout << "3. Nothing - terminate the program." << endl;
		cin >> option;
		switch (option)
		{
			default: while (option != 1 && option != 2 && option != 3)
					 {
						cout << "Please choose a valid option." << endl;
						cin >> option;
						break;
					 }
			case 1: cout << "So you chose to write a test!" << endl;
					outputFile.open("Test.txt");
					cout << "How many questions are there going to be?" << endl;
					cin >> numberOfQuestions;
					while (numberOfQuestions <= 0)
					{
						cout << "Please enter in a valid number of questions that is not negative or zero." << endl;
						cin >> numberOfQuestions;
					}
					cout << "So you choose to have " << numberOfQuestions << " questions on the test." << endl;
					while (counter <= numberOfQuestions)
					{
						cout << "What is the question for question #" << questionNumber << "?" << endl;
						getline(cin,question);
						cout << "What is the answer to this question?" << endl;
						getline(cin,answer);
						outputFile << question << endl;
						outputFile << answer << endl;
						counter++;
						questionNumber++;
					}
					outputFile.close();
					break;
			case 2: break;
			case 3:
				break;
		} 
	}
	
		


		return 0;
	}


The problem occurs when you type teacher > user > pass > user > pass > 1 > (put in a number greater than 2 for the number of questions). Then here is the problem. For the first round, it skips the option for me to enter in a question and instead skips to letting me enter in the answer. But when it comes to the second question, it didn't skip and it all goes well. Can anyone let me know why? Thanks!
Problem #2

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
// This is a program that for the two users (student, teacher)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
	ifstream inputFile;
	ofstream outputFile;
	char choice;
	string person,
		   username,
		   password,
		   STUDENT_USERNAME,
		   STUDENT_PASSWORD,
		   TEACHER_USERNAME,
		   TEACHER_PASSWORD,
		   question,
		   solution,
		   name;
	int option,
		numberOfQuestions,
		counter = 1,
		questionNumber = 1,
		number,
		answer;

	cout << "Are you a teacher or a student?\n";
	cin >> person;

	while (person != "student" && person != "Student" && person != "teacher" && person != "Teacher")
	{
		cout << "Please choose a valid option." << endl;
		cin >> person;
	}

	if (person == "student" || person == "Student")
	{
		cout << "Welcome, student!\n";
		cout << "Please register your username.\n";
		cin >> STUDENT_USERNAME;
		cout << "Please register your password.\n";
		cin >> STUDENT_PASSWORD;
		cout << "\nYour registration is complete! Thank you for registering!" << endl;
	}

	if (person == "teacher" || person == "Teacher")
	{
		cout << "Welcome, teacher!\n";
		cout << "Please register your username.\n";
		cin >> TEACHER_USERNAME;
		cout << "Please register your password.\n";
		cin >> TEACHER_PASSWORD;
		cout << "\nYour registration is complete! Thank you for registering!" << endl;
	}

	cout << "\nPlease log in.\n";
	cout << "Username: ";
	cin >> username;
	cout << "Password: ";
	cin >> password;

	while (!(username == STUDENT_USERNAME && password == STUDENT_PASSWORD) && 
		!(username == TEACHER_USERNAME && password == TEACHER_PASSWORD))
	{
	cout << "\nPlease log in.\n";
	cout << "Username: ";
	cin >> username;
	cout << "Password: ";
	cin >> password;
	}

	if ((username == STUDENT_USERNAME) && (password == STUDENT_PASSWORD))
	{
		cout << "Welcome Student! Would you like to take the test? Choose Y or N\n";
		cin >> choice;
		switch (choice)
		{
			default: while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'n') 
					 {
						cout << "\nPlease choose a valid option." << endl;
						cin >> choice;
						break;
					 }
			case 'y':
			case 'Y': cout << "So you wish to take the test!" << endl;
					  inputFile.open("Test.txt");
					  inputFile >> name;
					  cout << name << endl;
					  cin >> answer;
					  inputFile >> number;
					  cout << number << endl;
					  outputFile.open("Results.txt");
					  outputFile.close();
					  inputFile.close();
				break;
			case 'n':
			case 'N': cout << "" << endl;
				break;
		} 
	}

	if ((username == TEACHER_USERNAME) && (password == TEACHER_PASSWORD))
	{
		cout << "Welcome Teacher! What would you like to do?" << endl;
		cout << "1. Write a test." << endl;
		cout << "2. Check the test scores." << endl;
		cout << "3. Nothing - terminate the program." << endl;
		cin >> option;
		switch (option)
		{
			default: while (option != 1 && option != 2 && option != 3)
					 {
						cout << "Please choose a valid option." << endl;
						cin >> option;
						break;
					 }
			case 1: cout << "So you chose to write a test!" << endl;
					outputFile.open("Test.txt");
					cout << "How many questions are there going to be?" << endl;
					cin >> numberOfQuestions;
					while (numberOfQuestions <= 0)
					{
						cout << "Please enter in a valid number of questions that is not negative or zero." << endl;
						cin >> numberOfQuestions;
					}
					cout << "So you choose to have " << numberOfQuestions << " questions on the test." << endl;
					while (counter <= numberOfQuestions)
					{
						cout << "What is the question for question #" << questionNumber << "?" << endl;
						getline(cin,question);
						cout << "What is the answer to this question?" << endl;
						getline(cin,solution);
						outputFile << question << endl;
						outputFile << solution << endl;
						counter++;
						questionNumber++;
					}
					outputFile.close();
					cout << "Your questions and answers are saved!" << endl;
					break;
			case 2: cout << "So you wish to check the test scores!" << endl;
					inputFile.open("Results.txt");
					inputFile.close();
					break;
			case 3:
				break;
		} 
	}
	
		

		return 0;
	}

Once I run the program with the teacher role and written the test (above problem not solved yet), how do I get it to show the whole string of words? Let's say I put "What is 1+1?" It only shows "What" at the end.

When writing the test/answer, Referring back to the same question, I put "2" for the answer. I put 2 as the answer, I don't know why they coded the 2 into a huge number as -858993460. How can I fix that? Thanks for all the help in advance!
Sorry for the many questions, but..

Is there any way to loop it so when the students are taking the test, it loops until it is the end of the test? Thanks!

Also, is it possible to double inputFile at the same time? If yes, how can I choose which file to extract the information from? Because I need to alternate from two different files to display. If no, how can I fix it so I am also able to extract the data from both files? Thanks for help in advance!
Can anyone help me please? This is sort of urgent...
Last edited on
Bump
For the first problem, look at this snippet:
1
2
3
4
5
6
7
8
9
					cin >> numberOfQuestions;

...

					cout << "So you choose to have " << numberOfQuestions << " questions on the test." << endl;
					while (counter <= numberOfQuestions)
					{
						cout << "What is the question for question #" << questionNumber << "?" << endl;
						getline(cin,question);


When you asked the user for the numberOfQuestions you left the new line character in the input buffer which getline() takes for the end of input. You'll need to skip this whitespace when ever you switch from the extraction operator and getline. Something like:
getline(cin >> ws, question);

Once I run the program with the teacher role and written the test (above problem not solved yet), how do I get it to show the whole string of words? Let's say I put "What is 1+1?" It only shows "What" at the end.

You need to tell us where (line number) this problem is occurring. I for one don't feel like searching through 150 plus lines of code to find the issue. And remember that the extraction operator stops processing when it encounters an invalid character or whitespace.

Is there any way to loop it so when the students are taking the test, it loops until it is the end of the test?

What? Please be more precise. Remember unless we throughly study your program we won't even know that a student is taking a test. So where does the test start, end?

Also, is it possible to double inputFile at the same time?

Again, what? What do you mean by double inputFile? But yes you can have multiple input files open, using different names for each stream instance.



Sorry, I'm a little confused with what you mean by newline character..

As for the second question, it's in line 90-96 in the second one
But this helped tons! Totally solved the problem I had thanks!
Topic archived. No new replies allowed.