Thread: 'If' Statement inside the Switch statement being ignored.

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    33

    'If' Statement inside the Switch statement being ignored.

    I created a history review program in C language.

    In case 10, there are problems with the 2 if statements enclosed.

    1. Question 1: The U.S. Constitution prohibits the establishment of a state religion. Regardless of whether I enter TRUE or FALSE, it always displays "Incorrect".

    2. Question 2: Identify the name of this specific provision. Regardless of what I enter, it always displays "Incorrect".

    __________________________________________________ __________
    Code:
    #include <stdio.h>
    
    int main ()
    {
    int x;
    char a;
    char b[200];
    char s[10];
    char TRUE;
        do
        {
           printf("WELCOME TO THE AMERICAN HISTORY INTERACTIVE QUIZ PROGRAM!!!\n");
           printf("THIS SERIES IS ON THE U.S. CONSTITUTION.\n");
           printf("THERE ARE 20 QUESTIONS IN TOTAL.\n");
           printf("PLEASE ENTER A NUMBER IN THE RANGE 1-20,INCLUSIVE:\n");
           printf("ENTER A NUMBER OUTSIDE THIS RANGE TO QUIT THIS PROGRAM.\n");
           scanf("%d",&x);
       
           switch (x)
           {       
               case 1:
               printf("What is the significance of Madison vs. Marbury?\n");
               printf("a. The U.S. Supreme Court asserted its right to judicial review and transformed the power relationships between the executive, legislative, and the judicial branches.\n");
               printf("b. The Judiciary Act of 1789 was found to be partially unconstitutional.\n");
               printf("c. Marbury did not receive his political appointment in time.\n");
               printf("d. Marbury never became a Justice of the Peace in the District of Columbia.\n");
               scanf(" %c", &a);
                    if (a=='a')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
               
               case 2:
               printf("Which U.S. Constitutional amendment was ratified to ensure American women's voting rights?\n");
               printf("a. 17th\n");
               printf("b. 18th\n");
               printf("c. 19th\n");
               printf("d. 22nd\n");
               scanf(" %c", &a);
                    if (a=='c')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
       
               case 3:
               printf("Which U.S. Constitutional amendment was ratified to mandate the direct election of U.S. Senators?\n");
               printf("a. 16th\n");
               printf("b. 17th\n");
               printf("c. 18th\n");
               printf("d. 19th\n");
               scanf(" %c", &a);
                    if (a=='b')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
               
               case 4:
               printf("Which U.S. Constitutional amendment was ratified to ensure American women's voting rights?\n");
               printf("a. 17th\n");
               printf("b. 18th\n");
               printf("c. 19th\n");
               printf("d. 22nd\n");
               scanf(" %c", &a);
                    if (a=='c')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
       
               case 5:
               printf("Who were the Anti-Federalists?\n");
               printf("a. They opposed demands for a strong central government and instead supported strong states' rights and a federation of states.\n");
               printf("b. They opposed the Federalists.\n");
               printf("c. 19th\n");
               printf("d. 22nd\n");
               scanf(" %c", &a);
                    if (a=='c')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
       
               case 6:
               printf("What were some of the major weaknesses of the Articles of Confederation?\n");
               printf("a. The U.S. Congress could not lay and collect taxes.\n");
               printf("b. There was no national executive branch to enforce enacted legislation.\n");
               printf("c. The U.S. Congress could not regulate interstate commerce.\n");
               printf("d. All of the above.\n");
               scanf(" %c", &a);
                    if (a=='d')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
       
               case 7:
               printf("Which amendment is the only one that has been repealed in American history?\n");
               printf("a. 15th\n");
               printf("b. 16th\n");
               printf("c. 17th\n");
               printf("d. 18th\n");
               scanf(" %c", &a);
                    if (a=='d')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
       
               case 8:
               printf("What are the provisions of the Fifth Amendment?\n");
               printf("I. Due process; freedom from self-incrimination; trials must be preceded by grand jury indictments;\n");
               printf("II. Eminent domain\n");
               printf("III. Double jeopardy\n");
               printf("a. I only \n");
               printf("b. II only \n");
               printf("c. III only\n");
               printf("d. I, II\n");
               printf("e. I, II, III\n");
               scanf(" %c", &a);
                    if (a=='e')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
       
               case 9:
               printf("What are the provisions of the Fifth Amendment?\n");
               printf("I. Due process; freedom from self-incrimination; trials must be preceded by grand jury indictments;\n");
               printf("II. Eminent domain\n");
               printf("III. Double jeopardy\n");
               printf("a. I only \n");
               printf("b. II only \n");
               printf("c. III only\n");
               printf("d. I, II\n");
               printf("e. I, II, III\n");
               scanf(" %c", &a);
                    if (a=='e')
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
       
               case 10: 
               printf("TRUE or FALSE.\n");
               printf("The U.S. Constitution prohibits the establishment of a state religion.\n");
               printf("Enter the answer:");
               scanf("%s",&s);
                    if (s=="TRUE")
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               printf("Identify the name of this specific provision.\n");
               printf("Enter the answer:");
               scanf("%s", &b);
                    if (b=="Establishment Clause" || b=="Establishment")
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
               break;
           }
       } while (x>0 && x<21);
    
       return 0;   
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (s=="TRUE")
    > if (b=="Establishment Clause" || b=="Establishment")
    This isn't how you compare strings.

    Use strcmp()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Location
    BE
    Posts
    66
    It displays incorrect because that is what it is. The value of s being something like "TRUE\000\000\000\000<.

    You can use Enter a number for TRUE or another number for FALSE, or a function as Salem suggested - if possible for you.

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    33
    Thanks for your help. How would I compare strings with spaces?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use strcmp or strncmp from <string.h>. Take note of the return value though, i.e., strcmp does not compare only for equality, but rather you would compare the result with 0 to do the comparison that you want, e.g., strcmp(x, y) == 0 to compare x and y for equality.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with a switch statement inside of a loop.
    By chumpp in forum C Programming
    Replies: 3
    Last Post: 06-30-2012, 11:01 PM
  2. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  3. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 05:46 PM
  4. switch statement inside a do while
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:33 PM
  5. cin.get() inside switch statement
    By timberwolf5480 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 01:26 AM