- #1
MinusTheBear
- 22
- 0
Homework Statement
Compare two strings which represent test scores for a 20 question exam. If the student scores 15 or higher, they pass. Less than that they fail. Indicate whether a student passes or fails. Display how many answers they got right and wrong. Display which number they got wrong on the exam.
I simplified the problem at hand (it's actually a lot longer) and removed a lot of my code except the portion that is relevant. I made sure this portion of the code runs. The problem is the output for the answers they got wrong are weird symbols. I made bold the lines that are probably causing an issue. The code runs as expected with every test trial the professor gave EXCEPT for printing out the numbers they missed.
Homework Equations
n/a
The Attempt at a Solution
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- const int NUM_QUESTIONS = 20;
- int main()
- {
- int pass = 15;
- int correct = 0;
- int wrong = 0;
- string missed;
- string key = "BDAACABACDBCDADCCBDA";
- string student = "BDAACABACDBCDADCCBDD"; // Only 1 answer is wrong, answer 20
- for (int answer = 0; answer < NUM_QUESTIONS; answer++){
- if (key[answer] == student[answer]){
- correct++;
- }
- else{
- missed[wrong] = static_cast<char>((answer+1));
- wrong++;
- }
- }
- if (correct >= pass){
- cout << "Congratulations. You passed the exam.\n";
- }
- else{
- cout << "Sorry. You failed the exam.\n";
- }
- cout << "You got " << correct << " questions correct.\n";
- cout << "You missed the following" << wrong << " question(s): ";
- for (int element = 0; element < wrong; element++){
- cout << missed[element] << " ";
- }
- return 0;
- }
Congratulations. You passed the exam.
You got 19 questions correct.
You missed the following 1 question(s):