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?
#include <iostream>
usingnamespace std;
int main()
{
constint correct = 20;
constint 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.
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*
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?
#include <iostream>
usingnamespace std;
int main()
{
constint correct = 20;
constint 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.
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];
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.
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.
}
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.
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
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
@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.