Thread: Need help (Basic C++ program)

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    Need help (Basic C++ program)

    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.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    if (answer == "a" || "A")
    This will always evaluate to true since "A" by itself is true. What you want is:
    Code:
    if (answer == "a" || answer == "A")
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Editing that now.
    And i just noticed the true/false where i copied the if statement

    EDIT:
    Thank you sir/ma'am(?)
    That was definitely my problem.
    I have had trouble with that exact part of my if statements in class.

    Thanks for finding my problem. It works perfectly now.
    Last edited by bamgolfing; 11-16-2010 at 08:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed (problem with basic C program)
    By EpicYuzer in forum C Programming
    Replies: 15
    Last Post: 11-11-2010, 05:38 PM
  2. Command Line argument help, basic program
    By Rollo in forum C Programming
    Replies: 3
    Last Post: 10-31-2010, 12:51 PM
  3. Replies: 29
    Last Post: 10-22-2009, 11:01 AM
  4. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM