cin.get error - unable to enter input.

Hi, I'm writing a code where ten students input either true, false or blank in an array. Then the compiler will check if the answer is true or not. The actual problem is though, that i have to have the input in a string using cin.get function. However, when i do so, the compiler wont let me enter any inputs at all.

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
#include <iostream>
using namespace std;
int main()
{
	const int Size = 10;
	char AnswerKey[Size] = "TFTTFFTFT";
	int RollNo[Size], Solutions[10], Counter;
	char StudentAnswers[Size];
	int changer, changer2;

	cout << "Welcome!" << endl;
	
	for (int i = 0, j = 1; i < Size; i++, j++)
	{
		cout << "-------------------------------------------" << endl;
		cout << "Student No. " << j << "!" << endl;
		cout << "Please enter your Roll No." << endl;
		cout << "Enter here: ";
		cin >> RollNo[i];

		for (int j = 0; j < Size; j++)
		{
			while (RollNo[i] == RollNo[j] && i != j)
			{
				cout << "Oops! Your entered Roll Number has already been used. Please try again." << endl;
				cout << "Enter here: ";
				cin >> RollNo[i];
			}
		}

		cout << "Great! Now. Please enter all of your answers." << endl;
		cout << "(T = true, F = false, BLANK = No answer.)" << endl;
		cout << "Enter All Values in the same line." << endl;
		cout << "Enter here: ";
		cin.get(StudentAnswers, Size);

		for (int counter = 0; counter < Size; counter++)
		{
			if (StudentAnswers[counter] == AnswerKey[counter])
			{
				Counter += 2;
			}
			else if (StudentAnswers[counter] != AnswerKey[counter] && StudentAnswers[counter] != ' ')
			{
				counter--;
			}
		}

		Solutions[i] = Counter;
		Counter = 0;

	}

	for (int i = 0; i < Size; i++)
	{
		for (int j = 0; j < Size; j++)
		{
			if (Solutions[i] < Solutions[j])
			{
				changer = Solutions[i];
				Solutions[i] = Solutions[j];
				Solutions[j] = changer;
				changer2 = RollNo[i];
				RollNo[i] = RollNo[j];
				RollNo[j] = changer2;
			}
		}
	}

	cout << "Winner: " << endl;
	cout << "Roll Number: " << RollNo[0] << "   " << "Marks: " << Solutions[0] << endl;
	cout << endl;
	cout << endl;
	cout << "Runner-Up:" << endl;
	cout << "Roll Number: " << RollNo[1] << "   " << "Marks: " << Solutions[1] << endl;

	return 0;
}
As a first refactor consider. Note no error detection/handling for invalid input (except duplicate roll no):

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
#include <iostream>
#include <utility>
#include <cctype>

using namespace std;

int main() {
	constexpr size_t NoStuds {3};
	constexpr char AnswerKey[] {"TFTTFFTFT"};
	constexpr size_t NoAns {sizeof(AnswerKey)};

	size_t RollNo[NoStuds] {};
	int Solutions[NoStuds] {};

	cout << "Welcome!\n";

	for (size_t stud {}; stud < NoStuds; ++stud) {
		bool dup {};
		char StudentAnswers[NoAns] {};

		cout << "-------------------------------------------\n";
		cout << "Student No. " << stud + 1 << "!\n";

		do {
			dup = false;

			cout << "Please enter your Roll No: ";
			cin >> RollNo[stud];
			cin.ignore();

			for (size_t k {}; k < NoStuds; ++k)
				if (RollNo[stud] == RollNo[k] && k != stud) {
					cout << "Oops! Your entered Roll Number has already been used. Please try again.\n";
					dup = true;
					break;
				}
		} while (dup);

		cout << "Great! Now. Please enter all of your " << NoAns - 1 << " answers.\n";
		cout << "(T = true, F = false, BLANK = No answer.)\n";
		cout << "Enter All Values in the same line: ";
		cin.getline(StudentAnswers, NoAns);
		cin.clear();

		for (size_t counter {}, got {strlen(StudentAnswers)}; counter < got; ++counter) {
			const char upper {static_cast<char>(toupper(static_cast<unsigned char>(StudentAnswers[counter])))};

			if (upper == AnswerKey[counter])
				Solutions[stud] += 2;
			else if (upper != AnswerKey[counter] && StudentAnswers[counter] != ' ')
				--Solutions[stud];
		}
	}

	for (size_t i {}; i < NoStuds - 1; ++i)
		for (size_t j {}; j < NoStuds - i - 1; ++j)
			if (Solutions[j] < Solutions[j + 1]) {
				std::swap(Solutions[j], Solutions[j + 1]);
				std::swap(RollNo[j], RollNo[j + 1]);
			}

	cout << "\nWinner:\n";
	cout << "Roll Number: " << RollNo[0] << "   " << "Marks: " << Solutions[0] << "\n\n\n";
	cout << "Runner-Up:\n";
	cout << "Roll Number: " << RollNo[1] << "   " << "Marks: " << Solutions[1] << '\n';
}

Last edited on
Topic archived. No new replies allowed.