please help

Pages: 12
Hello I'm a student in an intro to C++ class. I have a homework assignment on arrays and am seriously stumped. I've googled and searched through this site and cannot find the answer to my question without using a library function I haven't learned yet. "strcomp" The only libraries I've used are iostream, iomanip, and string, but I'm most familiar with iostream.

Anyway, I need to write a program that grades a student's test and just tells them whether or not they passed. I have to store the correct answers in one array and the users answers (cin answers) in another. My book tells me how to compare arrays but only if the two arrays are EXACTLY the same, which not what I need to do. I've thought of how compare the FIRST 11 elements of each array, but not a linear comparison ANY 11 elements. I need to do this because the user only needs 11 answers to be correct. How do I tell the compiler that it doesn't matter WHICH 11 elements are the same as long as there ARE 11 elements which are the same in both arrays?

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
	const int correct = 20;
	const int user = 20;

	char answers[correct] = { 'B' || "b", 'D' || "d", 'A' || 'a', 'A' || 'a',
		'C' || 'c' , 'A' || 'a', 'B' || 'b', 
		'A' || 'a', 'C' || 'c', 'D' || 'd', 'B' || 'b', 'C' || 'c', 'D' || 'd', 
		'A' || 'a', 'D' || 'd', 'C' || 'c', 'C' || 'c', 'B' || 'b', 'D' || 'd', 'A' || 'a'};
char answers[user];
int count;
cout << "what were the student's answers\n";
cout << "type the answers with a space in between\n";
for (count = 0; count < correct; count++)
	cin >> answers[user];
if what do I put here?
cout << "the student passed\n";
else
cout << "the student failed\n";
return 0;
}


So that's how I'm trying to write it. By the way, where I said "What do I put here" I know that doesn't mean anything in code. I'm purposely asking you guys what I should put there as my if statement. Of course if there's absolutely no way I can do it like how I have so far, please let me know what I should do.

Thanks so much.
Oh, dear, oh dear. First things first.

I'm afraid you can't use ||s like you are in the array starting on line 9. Furthermore, you're mixing types, as "" denotes a constant char*, while '' denotes a constant character.

You can easily uppercase or lowercase letters with toupper() or tolower(), which both take one character and return the uppercase or lowercase equivalent.

From there, you have several options, but one of the simpler ones would be to have a for loop to compare each of the elements of answers from line 13 with each of the elements of answers from line 9, and count the number of items that match. *wink*

-Albatross
I understand you are saying about the ||s but I don't understand what you're saying I should. Can you please copy my code and edit it to show me in another post?

Thanks.
char answers[correct] = { 'B' , 'D' , 'A' , etc etc};

Convert the user input to upper case (using toupper, for example) before comparing it.
You mean like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
	const int correct = 20;
	const int user = 20;

	char answers[correct] = { tolower('B'), tolower('D'), tolower('A'), tolower('A'); ect.
		char answers[user];
int count;
cout << "what were the student's answers\n";
cout << "type the answers with a space in between\n";
for (count = 0; count < correct; count++)
	cin >> answers[user];
if what do I put here?
cout << "the student passed\n";
else
cout << "the student failed\n";
return 0;
}


also, how does using a for loop solve the comparison problem?
No, I do not mean like that. Let's take a look at this line:

char answers[correct] = { tolower('B'), tolower('D'), tolower('A'), tolower('A') ...
Here, you are making an array of the answers. What do you think the result of tolower is? Effectively, you've found a longhand way of writing this:

char answers[correct] = { 'b', 'd', 'a', 'a' ...
If that's what you want, you can just write it like that.


The point is that the student could enter the letter 'A' or 'a', for example, and if the answer is 'a' BOTH of those possible answers must be seen as correct. How can we do this? How can we alter the student's input answer so that it doesn't matter if he enters 'A' or 'a'? Using tolower:

Situation: Student enters 'A'.
Result: tolower(studentsAnswer) gives us 'a'.

Situation: Student enters 'a'.
Result: tolower(studentsAnswer) gives us 'a'.

So no matter whether the student enters the answer in uppercase or lowercase, is we apply tolower to his answer, we'll get his answer in lowercase. If all the characters in the answers array are lowercase, we can now compare them.

As an aside, you should pick better names for your variables. You have a variable that is the number of questions, but you have called that variable 'correct'. This makes no sense. If you give that variable a name like 'numberOfQuestions', the code will make much more sense when you read it.
@science man

As the program stands now, every answer typed in, will be out of bounds, since you are using cin >> answers[user];, and user equals 20. The answer[] array goes from 0 to 19. Change cin >> answers[user]; to cin >> answers[count];
@Moschops

What you're saying I effectively did makes sense to me, but I still don't understand what I should do instead. (at least not completely if at all) Are saying I should do this?
char tolower (answers[user]) or cin >> tolower (answers[user])

I've never used the tolower or toupper library functions before so I'm just trying to understand how that solves this issue to wanting the compiler to understand both uppercase and lowercase form of a letter.

Also I picked those variable names to represent what their array is holding. Should I have done it like this ? user[answers] and correct[answers]

@whitenite1


As the program stands now, every answer typed in, will be out of bounds, since you are using cin >> answers[user];, and user equals 20.

oh ya thanks.

Change cin >> answers[user]; to cin >> answers[count];

Oh I see why ok thanks. I think I'd perfer to the for loop instead.
I've never used the tolower or toupper library functions before so I'm just trying to understand how that solves this issue to wanting the compiler to understand both uppercase and lowercase form of a letter.


The user can enter their answers in uppercase or lowercase. If you compare their input 'a' with the correct answer 'A', they will be marked as incorrect, even though they clearly are correct. Pushing ALL letter involved to upper or lower solves this problem.

You need to stop coding and start thinking about how to solve the problem. The algorithm you're looking for is something like:

1
2
3
4
5
6
7
8
9
set score to zero
for each of the twenty questions:
{
  get user answer
  compare it to the correct answer
  if they match, increment score by one
}
if score is greater than or equal to passmark, student passed
if score is less than passmark, student failed


THIS is programming. Understanding, thinking about and solving a problem in a way that you can apply to code. The rest is syntax, which can wait until you've actually solved the problem.


I have thought about how to solve the problem, but don't believe I know enogh code to do so. (hence the library functions tolower and toupper) I probably should've said that in my starting post.
Anyway, as far as the code path you wrote, let me replace that with actual code to try to give a clearer understanding of where and how I'm stuck. Actually, I need to understand how to get the complier to understand how to compare each element as explained before in order to translate any of your code path into actual code. Which is what I don't understand. how exactly do I write the code to do that comparison?
I get the feeling you don't want to just show me because you think I just want you to do my homework for me. If so, please understand that I'm not that kind of person on top of the fact that computer science is my major so it would be completely ignorant of me to think that way.
Here is how to compare two char values:

1
2
3
4
5
6
7
8
char x;
char y;
...
...
if ( x == y)
{ // they are the same}
else
{ // they are not} 
@science man

First, the answer list
1
2
char answers[quiz_questions] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
	char quiz_answers[quiz_questions]; // Array to hold users answers 

You don't really need the variable, user.
Then, in the count loop, where you get the users input, add, quiz_answers[count] = toupper(quiz_answers[count]); That sets all the answers to uppercase.

Then, you need a loop to check if(quiz_answers[x]==answers[x]). If yes, increase correct variable, if not, increase wrong variable. If the variable correct, is high enough, student passes, if not, they fail.
To build off Moschops's post, here is a way to compare two characters without considering the case.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cctype> //Needed for tolower() and toupper().
...
...
char x;
char y;
...
...
if (tolower(x) == tolower(y)) {
    //Case aside, they are the same.
}
else {
    //They are not.
}


-Albatross

If yes, increase correct variable, if not, increase wrong variable. If the variable correct, is high enough, student passes, if not, they fail.

I'm a little unclear on how to do that. I've made the corrections you told me but don't know how to get it to account for the number of wrong and right answers. Should I set a variable for right answers and another for worng? If so, how?

Here's the code with the corrections I've understood so far.
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
#include <iostream>
using namespace std;

int main()
{
	const int answers = 19;
	const int test_answers = 19;

	char answers[test_answers] = { 'B' || "b", 'D' || "d", 'A' || 'a', 'A' || 'a',
		'C', 'A', 'B', 
		'A', 'C', 'D', 'B', 'C', 'D', 
		'A', 'D', 'C', 'C', 'B', 'D', 'A'};
char test_answers[test_questions];
int count;
cout << "what were the student's answers\n";
cout << "type the answers with a space in between\n";
test_answers[count} = toupper(test_answers[count])
	cin >> test_answers[test_questions];

for (count = 0; test_answers[count] == answers[count]; count++)
I don't know what do tell it to do here. 
cout << "the student passed\n";
else
cout << "the student failed\n";
return 0;
} 
Should I set a variable for right answers and another for worng?


You could do, but why would you care about knowing how many she got wrong AND how many she got right? You only need to know about one of them.

Here is how to set a variable to zero at the beginning:

int numberOfCorrectAnswers = 0;

and here are some ways to increase it by one. Each of these has the same effect. You should, at this stage, pick the one that is most obvious to you when you read it.

1
2
3
4
numberOfCorrectAnswers++;
++numberOfCorrectAnswers;
numberOfCorrectAnswers += 1;
numberOfCorrectAnswers = numberOfCorrectAnswers + 1; // I SUGGEST YOU USE THIS ONE 

Last edited on
so my for loop would go like this?
1
2
3
4
5
for (count = 0; test_answers[count] == answers[count]; count++)
number_of_correct_answers = number_of_correct_answers + 1
if 
number_of_correct_answers = 11
cout << "the student passed"

aussme I initalized the variable as int
Last edited on
for (count = 0; count < numberOfQuestions; count++)
might work better.

Time to read up on what each of those statements in the for loop header is for. The middle one is the condition to keep on going.
The if statement should look like if (number_of_correct_answers == 11); and you would need a semi-colon after number_of_correct_answers = number_of_correct_answers + 1
Last edited on
@Moschops
I don't how that properly follows my next statement. When I read what I wrote as the for statement I find it easier to read because I see how the following statement follows it.

@whitenite1
I know thanks. I wrote it in a rush.

@all

Thanks so much guys! I'll post again if I still have any compile errors or running problems.
Last edited on
Still having trouble my compiler is giving me a bunch of errors.
Here's my code
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
#include <iostream>
using namespace std;

int main()
{
	const int test_questions = 19;
	const int test_answers = 19;
	char answers;

	char answers[test_answers] = { 'B', 'D', 'A', 'A',
		'C', 'A', 'B', 
		'A', 'C', 'D', 'B', 'C', 'D', 
		'A', 'D', 'C', 'C', 'B', 'D', 'A'};
char test_answers[test_questions];
int count;
cout << "what were the student's answers\n";
cout << "type the answers with a space in between\n";
test_answers[count} = toupper(test_answers[count])
	cin >> test_answers[test_questions];

for (count = 0; test_answers[count] == answers[count]; count++)
number_of_correct_answers = number_of_correct_answers + 1;
if (number_of_correct_answers = 11);
cout << "the student passed\n";
else
cout << "the student failed\n";
return 0;
} 


here are the errors
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
1
1>  array program.cpp
1>l:\comp sci stuff\array program\array program.cpp(10): error C2040: 'answers' : 'char [19]' differs in levels of indirection from 'char'
1>l:\comp sci stuff\array program\array program.cpp(13): error C2078: too many initializers
1>l:\comp sci stuff\array program\array program.cpp(14): error C2373: 'test_answers' : redefinition; different type modifiers
1>          l:\comp sci stuff\array program\array program.cpp(7) : see declaration of 'test_answers'
1>l:\comp sci stuff\array program\array program.cpp(18): error C2143: syntax error : missing ')' before '}'
1>l:\comp sci stuff\array program\array program.cpp(18): error C2143: syntax error : missing ']' before ')'
1>l:\comp sci stuff\array program\array program.cpp(18): error C2109: subscript requires array or pointer type
1>l:\comp sci stuff\array program\array program.cpp(18): error C2513: 'int' : no variable declared before '='
1>l:\comp sci stuff\array program\array program.cpp(18): error C2065: 'test_answers' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(18): error C2065: 'count' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(19): error C2146: syntax error : missing ';' before identifier 'cin'
1>l:\comp sci stuff\array program\array program.cpp(19): error C2143: syntax error : missing ';' before '>>'
1>l:\comp sci stuff\array program\array program.cpp(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>l:\comp sci stuff\array program\array program.cpp(21): error C2059: syntax error : 'for'
1>l:\comp sci stuff\array program\array program.cpp(21): error C2143: syntax error : missing ')' before ';'
1>l:\comp sci stuff\array program\array program.cpp(21): error C2065: 'count' : undeclared identifier
1>l:\comp sci stuff\array program\array program.cpp(21): error C2143: syntax error : missing ';' before '=='
1>l:\comp sci stuff\array program\array program.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>l:\comp sci stuff\array program\array program.cpp(21): error C2143: syntax error : missing ';' before '++'
1>l:\comp sci stuff\array program\array program.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>l:\comp sci stuff\array program\array program.cpp(21): error C2059: syntax error : ')'
1>l:\comp sci stuff\array program\array program.cpp(23): error C2059: syntax error : 'if'
1>l:\comp sci stuff\array program\array program.cpp(24): error C2143: syntax error : missing ';' before '<<'
1>l:\comp sci stuff\array program\array program.cpp(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>l:\comp sci stuff\array program\array program.cpp(25): error C2059: syntax error : 'else'
1>l:\comp sci stuff\array program\array program.cpp(27): error C2059: syntax error : 'return'
1>l:\comp sci stuff\array program\array program.cpp(28): error C2059: syntax error : '}'
1>l:\comp sci stuff\array program\array program.cpp(28): error C2143: syntax error : missing ';' before '}'
1>l:\comp sci stuff\array program\array program.cpp(28): error C2059: syntax error : '}'
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Can you guys make sense out of these errors? I don't see why they're occurring.
Pages: 12