I need help with a project I am doing for my computer science class. this is my first year programming so this code is meant to be VERY simple.
The project is to create a test consisting of 3 mult. choice questions, 3 matching questions, 3 true/false, and 3 fill in the blank.
I have the code mostly in the way I want it. My problem is in the very end when it is supposed to tally how many questions you got right or wrong. Most of my questions are returning wrong values, meaning if I put an incorrect answer, it counts it as correct.
Here is my code:
Code:#include <iostream> #include <string> using std::cout; using std::cin; using std::endl; using std::string; int main() { //declare variables string name = ""; string answer = ""; string answer4 = ""; string answer5 = ""; string answer6 = ""; int wrong = 0; int right = 0; double percentCorrect = 0.0; //question 1 cout << "Question #1" << endl; cout << "Which of the following symbols is used to represent the condition in a selection structure?" << endl; cout << "a. diamond" << endl; cout << "b. oval" << endl; cout << "c. paralellogram" << endl; cout << "d. rectangle" << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "a" || "A") right = right + 1; else { wrong = wrong + 1; } //question 2 cout << '\n' << "Question #2" << endl; cout << "Which of the following is the equality operator in C++?" << endl; cout << "a. !=" << endl; cout << "b. =" << endl; cout << "c. ==" << endl; cout << "d. ->" << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "c" || "C") right = right + 1; else { wrong = wrong + 1; } //question 3 cout << '\n' << "Question #3" << endl; cout << "Which of the following is the inequality operator in C++?" << endl; cout << "a. !=" << endl; cout << "b. =" << endl; cout << "c. ==" << endl; cout << "d. <>" << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "a" || "A") right = right + 1; else { wrong = wrong + 1; } //question 4-6 cout << '\n' << "Questions #4-6" << endl; cout << "Match the following definitions to the words listed." << endl; cout << "4. Another name for logical operators | a - Relational operators" << endl; cout << "5. Another name for the selection structure | b - Decision structure" << endl; cout << "6. Another name for comparison operators | c - Boolean operators" << endl; cout << "Enter your answer for Question 4: "; cin >> answer4; cout << "Enter your answer for Question 5: "; cin >> answer5; cout << "Enter your answer for Question 6: "; cin >> answer6; if (answer4 == "c" || "C") right = right + 1; else { wrong = wrong + 1; } if (answer5 == "b" || "B") right = right + 1; else { wrong = wrong + 1; } if (answer6 == "a" || "A") right = right + 1; else { wrong = wrong + 1; } //question 7 cout << '\n' << "Question #7 - True or False" << endl; cout << " The expression 35 < (1/2) * (98/3) +4 equates to" << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "a" || "A") right = right + 1; else { wrong = wrong + 1; } //question 8 cout << '\n' << "Question #8 - True or False" << endl; cout << " The expression (4 * 3) < 6 + 7 && 8 < 6 + 9 equates to" << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "a" || "A") right = right + 1; else { wrong = wrong + 1; } //question 9 cout << '\n' << "Question #9 - True or False" << endl; cout << " The expression (4 * 3) < 6 + 7 && 8 < 6 + 9 equates to" << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "a" || "A") right = right + 1; else { wrong = wrong + 1; } //question 10 cout << '\n' << "Question #10 - Fill in the blank" << endl; cout << "___ is used to comment out a line of code" << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "//") right = right + 1; else { wrong = wrong + 1; } //question 11 cout << '\n' << "Question #11 - Fill in the blank" << endl; cout << "#include<_____> is used to enable the use of strings." << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "string") right = right + 1; else { wrong = wrong + 1; } //question 12 cout << '\n' << "Question #12 - Fill in the blank" << endl; cout << "A ________ is used in a selection structure to specify a decision the program needs to make." << endl; cout << "Enter your answer : "; cin >> answer; if (answer == "condition") right = right + 1; else { wrong = wrong + 1; } // total right/wrong cout << '\n' << "The number of right answers is: " << right << endl; cout << "The number of wrong answers is: " << wrong << endl; return 0; } //end of main function
I'm sure the key to my problem is something simple, but I must be overlooking it.
Thanks for the help.
I am in the process of testing the answers. At the time of this post, #1-3 are not working.