Thread: My getline function is skipped whenver I use case 10.

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

    My getline function is skipped whenver I use case 10.

    My getline function is skipped whenver I select case 10. I am not given a chance to enter input. The program automatically skips and enters the sentence "The correct answer is the Connecticut Compromise." I have checked with code::blocks and there were no compiler warnings. I ran that portion of the code separately in another file and everything worked out fine. Why does it not function properly when it is combined with the rest of the code?

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main ()
    {
           char a;
           char fill_in_the_blank[50];
           char correct_answer_for_Q10[]="Connecticut Compromise";
           char a_correct_answer_for_Q10[]="connecticut compromise";
           char b_correct_answer_for_Q10[]="Connecticut compromise";
           int x;
           int attempt1=0;
           int attempt2=0;
           int attempt3=0;
           int attempt4=0;
           int attempt5=0;
           int attempt6=0;
           int attempt7=0;
           int attempt8=0;
           int attempt9=0;
           int attempt10=0;
           int score=0;
    
           cout<<"\nWELCOME TO THE AMERICAN HISTORY INTERACTIVE QUIZ PROGRAM!!!\n\n";
           cout<<"\nTHIS SERIES IS ON THE U.S. CONSTITUTION.\n\n";
           cout<<"\nTHERE ARE 20 QUESTIONS IN TOTAL.\n\n";
    
           do{
           cout<<"\nPLEASE ENTER A NUMBER IN THE RANGE 1-20,INCLUSIVE.\n\n";
           cout<<"\nPLEASE ENTER A NUMBER OUTSIDE THIS RANGE TO QUIT THIS PROGRAM.\n\n";
           cin>>x;
    
           switch (x)
           {
           case 1:
           if (attempt1>0)
                cout<<"\nThis question has been selected. Please choose another question other than Q1.\n";
           if (attempt1>0)
                break;
           else
                cout<<"\nQ1.What is the significance of Madison vs. Marbury?\n";
                cout<<"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";
                cout<<"b. The Judiciary Act of 1789 was found to be partially unconstitutional.\n";
                cout<<"c. Marbury did not receive his political appointment in time.\n";
                cout<<"d. Marbury never became a Justice of the Peace in the District of Columbia.\n";
                cout<<"Answer:"<<endl;
                cin>>a;
                    if (a=='a')
                    cout<<"\nCORRECT!\n\n";
                    if (a=='a')
                    ++score;
                    if (a=='a')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is a."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt1;
               break;
    
           case 2:
           if (attempt2>0)
                cout<<"\nThis question has been selected. Please choose another question aside from Q1.\n";
           if (attempt2>0)
                break;
           else
               cout<<"\nQ2.Which U.S. Constitutional amendment was ratified to ensure American women's voting rights?\n";
               cout<<"a. 17th\n";
               cout<<"b. 18th\n";
               cout<<"c. 19th\n";
               cout<<"d. 22nd\n";
               cin>>a;
                    if (a=='c')
                    cout<<"CORRECT!\n\n";
                    if (a=='c')
                    ++score;
                    if (a=='c')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is c."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt2;
               break;
    
           case 3:
           if (attempt3>0)
                cout<<"This question has been selected.\n";
           if (attempt3>0)
                break;
           else
               cout<<"\nQ3.Which U.S. Constitutional amendment was ratified to mandate the direct election of U.S. Senators?\n";
               cout<<"a. 16th\n";
               cout<<"b. 17th\n";
               cout<<"c. 18th\n";
               cout<<"d. 19th\n";
               cin>>a;
                    if (a=='b')
                    cout<<"CORRECT!\n\n";
                    if (a=='b')
                    ++score;
                    if (a=='b')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is b."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt3;
    
           case 4:
           if (attempt4>0)
               cout<<"This question has been selected.\n";
           if (attempt4>0)
               break;
               else
               cout<<"\nQ4.Which U.S. Constitutional amendment was ratified to ensure American women's voting rights?\n";
               cout<<"a. 17th\n";
               cout<<"b. 18th\n";
               cout<<"c. 19th\n";
               cout<<"d. 22nd\n";
               cin>>a;
                    if (a=='c')
                    cout<<"CORRECT!\n\n";
                    if (a=='c')
                    ++score;
                    if (a=='c')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is c."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt4;
               break;
    
           case 5:
           if (attempt5>0)
               cout<<"This question has been selected.\n";
           if (attempt5>0)
               break;
               else
               cout<<"\nQ5.Who were the Anti-Federalists?\n";
               cout<<"a. They opposed demands for a strong central government and instead supported strong states' rights and a federation of states.\n";
               cout<<"b. The Anti-Federalists conditioned approval of the drafted U.S. Constitution on the presence of a bill of rights.\n";
               cout<<"c. a and b.\n";
               cout<<"d. None of the above.\n";
               cin>>a;
                    if (a=='c')
                    cout<<"CORRECT!\n\n";
                    if (a=='c')
                    ++score;
                    if (a=='c')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is c."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt5;
               break;
    
           case 6:
           if (attempt6>0)
               cout<<"This question has been selected.\n";
           if (attempt6>0)
               break;
           else
               cout<<"\nQ6.What were some of the major weaknesses of the Articles of Confederation?\n";
               cout<<"a. The U.S. Congress could not lay and collect taxes.\n";
               cout<<"b. There was no national executive branch to enforce enacted legislation.\n";
               cout<<"c. The U.S. Congress could not regulate interstate commerce.\n";
               cout<<"d. All of the above.\n";
               cin>>a;
                    if (a=='d')
                    cout<<"CORRECT!\n\n";
                    if (a=='d')
                    ++score;
                    if (a=='d')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is d."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt6;
               break;
    
           case 7:
           if (attempt7>0)
               cout<<"This question has been selected.\n";
           if (attempt7>0)
               break;
           else
               cout<<"\nQ7.Which amendment is the only one that has been repealed in American history?\n";
               cout<<"a. 15th\n";
               cout<<"b. 16th\n";
               cout<<"c. 17th\n";
               cout<<"d. 18th\n";
               cin>>a;
                    if (a=='d')
                    cout<<"CORRECT!\n\n";
                    if (a=='d')
                    ++score;
                    if (a=='d')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is d."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt7;
               break;
    
           case 8:
           if (attempt8>0)
               cout<<"This question has been selected.\n";
           if (attempt8>0)
               break;
           else
               cout<<"\nQ8.What are the provisions of the Fifth Amendment?\n";
               cout<<"I. Due process; freedom from self-incrimination; trials must be preceded by grand jury indictments;\n";
               cout<<"II. Eminent domain\n";
               cout<<"III. Double jeopardy\n";
               cout<<"a. I only \n";
               cout<<"b. II only \n";
               cout<<"c. III only\n";
               cout<<"d. I, II\n";
               cout<<"e. I, II, III\n";
               cin>>a;
                    if (a=='e')
                    cout<<"CORRECT!\n\n";
                    if (a=='e')
                    ++score;
                    if (a=='e')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is e."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt8;
               break;
    
           case 9:
           if (attempt9>0)
               cout<<"This question has been selected.\n";
           if (attempt9>0)
               break;
               cout<<"\nQ9.A constitutional amendment needs the approval _______ of the state legislatures or _______ of the state ratifying conventions in order to ensure passage.\n";
               cout<<"a. 2/3;4/5\n";
               cout<<"b. 4/5;3/4\n";
               cout<<"c. 3/4;3/4\n";
               cout<<"d. none of the above\n";
               cin>>a;
                    if (a=='e')
                    cout<<"CORRECT!\n\n";
                    if (a=='e')
                    ++score;
                    if (a=='e')
                    cout<<"Your score is now: "<<score<<"\n";
                    else
                    cout<<"The correct answer is e."<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
               ++attempt9;
    
           case 10:
           if (attempt10>0)
           cout<<"This question has been selected.\n";
           if (attempt10>0)
           break;
    
           cout<<"\nQ10.What was the plan that created a bicameral Congress with the House of Representatives based on proportional representation and the Senate based on equality?.\n";
           cout<<"Enter the answer:";
           cin.getline(fill_in_the_blank,50);
    
           if (strcmp(fill_in_the_blank,correct_answer_for_Q10)==0 || strcmp(fill_in_the_blank,a_correct_answer_for_Q10)==0 || strcmp(fill_in_the_blank,b_correct_answer_for_Q10)==0)
           {
               cout<<"CORRECT!"<<endl;++score;
               cout<<"Your score is now: "<<score<<endl;
           }
           else
           {
                cout<<"The correct answer is the Connecticut Compromise."<<endl<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
           }
           ++attempt10;
            break;
            }
    
            } while (x>0 && x<21);
    
            cout<<"\nThanks for playing!\n\n"<<"Your total score is "<<score<<" out of 10."<<endl;
    
            return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    Indentation is important, but it's not the only thing you need. Your else statements don't have appropriate curly brackets to indicate code blocks. As a result, only one line after them is included.
    Devoted my life to programming...

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    you should use autoformating tool like astyle
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main ()
    {
        char a;
        char fill_in_the_blank[50];
        char correct_answer_for_Q10[]="Connecticut Compromise";
        char a_correct_answer_for_Q10[]="connecticut compromise";
        char b_correct_answer_for_Q10[]="Connecticut compromise";
        int x;
        int attempt1=0;
        int attempt2=0;
        int attempt3=0;
        int attempt4=0;
        int attempt5=0;
        int attempt6=0;
        int attempt7=0;
        int attempt8=0;
        int attempt9=0;
        int attempt10=0;
        int score=0;
    
        cout<<"\nWELCOME TO THE AMERICAN HISTORY INTERACTIVE QUIZ PROGRAM!!!\n\n";
        cout<<"\nTHIS SERIES IS ON THE U.S. CONSTITUTION.\n\n";
        cout<<"\nTHERE ARE 20 QUESTIONS IN TOTAL.\n\n";
    
        do {
            cout<<"\nPLEASE ENTER A NUMBER IN THE RANGE 1-20,INCLUSIVE.\n\n";
            cout<<"\nPLEASE ENTER A NUMBER OUTSIDE THIS RANGE TO QUIT THIS PROGRAM.\n\n";
            cin>>x;
    
            switch (x)
            {
            case 1:
                if (attempt1>0)
                    cout<<"\nThis question has been selected. Please choose another question other than Q1.\n";
                if (attempt1>0)
                    break;
                else
                    cout<<"\nQ1.What is the significance of Madison vs. Marbury?\n";
                cout<<"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";
                cout<<"b. The Judiciary Act of 1789 was found to be partially unconstitutional.\n";
                cout<<"c. Marbury did not receive his political appointment in time.\n";
                cout<<"d. Marbury never became a Justice of the Peace in the District of Columbia.\n";
                cout<<"Answer:"<<endl;
                cin>>a;
                if (a=='a')
                    cout<<"\nCORRECT!\n\n";
                if (a=='a')
                    ++score;
                if (a=='a')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is a."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt1;
                break;
    
            case 2:
                if (attempt2>0)
                    cout<<"\nThis question has been selected. Please choose another question aside from Q1.\n";
                if (attempt2>0)
                    break;
                else
                    cout<<"\nQ2.Which U.S. Constitutional amendment was ratified to ensure American women's voting rights?\n";
                cout<<"a. 17th\n";
                cout<<"b. 18th\n";
                cout<<"c. 19th\n";
                cout<<"d. 22nd\n";
                cin>>a;
                if (a=='c')
                    cout<<"CORRECT!\n\n";
                if (a=='c')
                    ++score;
                if (a=='c')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is c."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt2;
                break;
    
            case 3:
                if (attempt3>0)
                    cout<<"This question has been selected.\n";
                if (attempt3>0)
                    break;
                else
                    cout<<"\nQ3.Which U.S. Constitutional amendment was ratified to mandate the direct election of U.S. Senators?\n";
                cout<<"a. 16th\n";
                cout<<"b. 17th\n";
                cout<<"c. 18th\n";
                cout<<"d. 19th\n";
                cin>>a;
                if (a=='b')
                    cout<<"CORRECT!\n\n";
                if (a=='b')
                    ++score;
                if (a=='b')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is b."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt3;
    
            case 4:
                if (attempt4>0)
                    cout<<"This question has been selected.\n";
                if (attempt4>0)
                    break;
                else
                    cout<<"\nQ4.Which U.S. Constitutional amendment was ratified to ensure American women's voting rights?\n";
                cout<<"a. 17th\n";
                cout<<"b. 18th\n";
                cout<<"c. 19th\n";
                cout<<"d. 22nd\n";
                cin>>a;
                if (a=='c')
                    cout<<"CORRECT!\n\n";
                if (a=='c')
                    ++score;
                if (a=='c')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is c."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt4;
                break;
    
            case 5:
                if (attempt5>0)
                    cout<<"This question has been selected.\n";
                if (attempt5>0)
                    break;
                else
                    cout<<"\nQ5.Who were the Anti-Federalists?\n";
                cout<<"a. They opposed demands for a strong central government and instead supported strong states' rights and a federation of states.\n";
                cout<<"b. The Anti-Federalists conditioned approval of the drafted U.S. Constitution on the presence of a bill of rights.\n";
                cout<<"c. a and b.\n";
                cout<<"d. None of the above.\n";
                cin>>a;
                if (a=='c')
                    cout<<"CORRECT!\n\n";
                if (a=='c')
                    ++score;
                if (a=='c')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is c."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt5;
                break;
    
            case 6:
                if (attempt6>0)
                    cout<<"This question has been selected.\n";
                if (attempt6>0)
                    break;
                else
                    cout<<"\nQ6.What were some of the major weaknesses of the Articles of Confederation?\n";
                cout<<"a. The U.S. Congress could not lay and collect taxes.\n";
                cout<<"b. There was no national executive branch to enforce enacted legislation.\n";
                cout<<"c. The U.S. Congress could not regulate interstate commerce.\n";
                cout<<"d. All of the above.\n";
                cin>>a;
                if (a=='d')
                    cout<<"CORRECT!\n\n";
                if (a=='d')
                    ++score;
                if (a=='d')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is d."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt6;
                break;
    
            case 7:
                if (attempt7>0)
                    cout<<"This question has been selected.\n";
                if (attempt7>0)
                    break;
                else
                    cout<<"\nQ7.Which amendment is the only one that has been repealed in American history?\n";
                cout<<"a. 15th\n";
                cout<<"b. 16th\n";
                cout<<"c. 17th\n";
                cout<<"d. 18th\n";
                cin>>a;
                if (a=='d')
                    cout<<"CORRECT!\n\n";
                if (a=='d')
                    ++score;
                if (a=='d')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is d."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt7;
                break;
    
            case 8:
                if (attempt8>0)
                    cout<<"This question has been selected.\n";
                if (attempt8>0)
                    break;
                else
                    cout<<"\nQ8.What are the provisions of the Fifth Amendment?\n";
                cout<<"I. Due process; freedom from self-incrimination; trials must be preceded by grand jury indictments;\n";
                cout<<"II. Eminent domain\n";
                cout<<"III. Double jeopardy\n";
                cout<<"a. I only \n";
                cout<<"b. II only \n";
                cout<<"c. III only\n";
                cout<<"d. I, II\n";
                cout<<"e. I, II, III\n";
                cin>>a;
                if (a=='e')
                    cout<<"CORRECT!\n\n";
                if (a=='e')
                    ++score;
                if (a=='e')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is e."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt8;
                break;
    
            case 9:
                if (attempt9>0)
                    cout<<"This question has been selected.\n";
                if (attempt9>0)
                    break;
                cout<<"\nQ9.A constitutional amendment needs the approval _______ of the state legislatures or _______ of the state ratifying conventions in order to ensure passage.\n";
                cout<<"a. 2/3;4/5\n";
                cout<<"b. 4/5;3/4\n";
                cout<<"c. 3/4;3/4\n";
                cout<<"d. none of the above\n";
                cin>>a;
                if (a=='e')
                    cout<<"CORRECT!\n\n";
                if (a=='e')
                    ++score;
                if (a=='e')
                    cout<<"Your score is now: "<<score<<"\n";
                else
                    cout<<"The correct answer is e."<<endl;
                cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                ++attempt9;
    
            case 10:
                if (attempt10>0)
                    cout<<"This question has been selected.\n";
                if (attempt10>0)
                    break;
    
                cout<<"\nQ10.What was the plan that created a bicameral Congress with the House of Representatives based on proportional representation and the Senate based on equality?.\n";
                cout<<"Enter the answer:";
                cin.getline(fill_in_the_blank,50);
    
                if (strcmp(fill_in_the_blank,correct_answer_for_Q10)==0 || strcmp(fill_in_the_blank,a_correct_answer_for_Q10)==0 || strcmp(fill_in_the_blank,b_correct_answer_for_Q10)==0)
                {
                    cout<<"CORRECT!"<<endl;
                    ++score;
                    cout<<"Your score is now: "<<score<<endl;
                }
                else
                {
                    cout<<"The correct answer is the Connecticut Compromise."<<endl<<endl;
                    cout<<"INCORRECT!\n\n"<<"Your score is now: "<<score<<"\n";
                }
                ++attempt10;
                break;
            }
    
        } while (x>0 && x<21);
    
        cout<<"\nThanks for playing!\n\n"<<"Your total score is "<<score<<" out of 10."<<endl;
    
        return 0;
    }
    and splitting long function in smaller onces could also help to analyze the code
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My setup() function is skipped.
    By howardbc14 in forum C Programming
    Replies: 3
    Last Post: 05-14-2015, 11:29 PM
  2. gets function skipped 1st time
    By ambrown782 in forum C Programming
    Replies: 6
    Last Post: 07-26-2014, 12:30 AM
  3. Need help with getline function
    By chickenlittle in forum C++ Programming
    Replies: 7
    Last Post: 10-13-2010, 01:04 PM
  4. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  5. getline function
    By JamMan in forum C++ Programming
    Replies: 11
    Last Post: 11-25-2001, 05:29 PM