Thread: Cannot input printf function without an error

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

    Cannot input printf function without an error

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
    int x;
    int attempt=0;
    char a;
    char b[7];
    char c[]="TRUE";
    char d[15];
    char e[]="Establishment";
        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:
               if (attempt>0)
               break;    
               else 
               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");
               ++attempt;
               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",&b);
                    if (strcmp("TRUE",c) == 0 || strcmp("true",c)==0 || strcmp("True",c)==0) 
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
                    //Compare strings by setting a string with a pre-set value and the other string based on user input. 
                    //Return 0 means that they are interpreted as equal.
               printf("Identify the name of this specific clause.\n");
               printf("Enter the answer:");
               scanf("%s", &d);
                    if (strcmp("ESTABLISHMENT",e) == 0 || strcmp("Establishment",e)==0 || strcmp("establishment",e)==0)
                    printf("CORRECT!\n");
                    else
                    printf("INCORRECT!\n");
                    //Compare strings by setting a string with a pre-set value and the other string based on user input. 
                    //Return 0 means that they are interpreted as equal.
               break;
           }
       } while (x>0 && x<21);
    
       return 0;   
    }
    This is fine until I insert printf("This question has been selected."); after if (attempt>0). Once I insert this line, I get an error stating 'else without a previous if'.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you always use braces for if statements and loops, even when unnecessary. Your mistake is likely related to a failure to do this.
    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

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You need to put code after each if/else into blocks.

    Code:
    if( /* something */ )
    {
        // all
        // if
        // code
        // here
    }
    else
    {
        // all
        // else
        // code
        // here
    }
    Note the use of braces to enclose multiple statements into a single block.

    [edit] Upon closer examination, you don't even need the "else" there. If the "if" is true, you break out of the case and nothing else there will execute. Otherwise, the rest of the code for that case will execute.
    Last edited by Matticus; 04-17-2015 at 01:14 PM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you also better start checking the return value of scanf. Otherwise - as soon as somebody at the menu stage enters a instead of 1-20 - your program is stuck

    FAQ > Flush the input buffer - Cprogramming.com
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Your History also needs checking
    1920 was in the 20th century!

    Code:
    #ifdef ALWAYS_BREAK
        if (attempt>0)
               printf("will always break out of switch\n");
               break;   
    #else 
        if (attempt>0)
        {          
             printf("will conditionally  break out of switch\n");
             break;  
        }
    #endif
    Last edited by sparkomemphis; 04-17-2015 at 01:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: syntax error before printf
    By Ronnie11 in forum C Programming
    Replies: 6
    Last Post: 07-12-2014, 08:39 AM
  2. Hex scanf input and printf output.
    By et3ruiz in forum C Programming
    Replies: 3
    Last Post: 11-23-2011, 12:54 AM
  3. Weird read input or bad printf output?
    By ChaoticMachiner in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 03:40 PM
  4. printf() error
    By Wipe in forum C Programming
    Replies: 2
    Last Post: 01-15-2005, 12:45 PM
  5. basic input and output with printf and fgets
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 08-01-2002, 11:02 PM