Thread: Compiler skipping a statement & moving to nxt instruction

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    Compiler skipping a statement & moving to nxt instruction

    Hi Folks,

    I'm facing a strange problem here. In my code the compiler isn't executing an instruction and is skipping to next instruction.

    Below is a program I created for flash card. Here first we feed the program with some words and its meanings. Later vocab is tested by calling one word after another, if user gets it right then the word is deleted and if user gets it wrong the word stays.

    Below program isn't commented so don't bother understanding the whole code cause i'm having trouble only in a small part of it.

    Code:
     #include <iostream>
    #include <string>
    
    class flash_card
    {
        std::string question[15];
        std::string answer[15];
    
        public:
    
        flash_card()
        {
            std::cout<<"constructing class\n";
        }
    
        int feed();
    
        void test(int& total_noofqs);
    
        void right(const int index, int& totalqs);
    
        void wrong();
    
        void learn();
    
        ~flash_card()
        {
            std::cout<<"Deconstructing class\n";
        }
    };
    
    int number_of_questions=0;
    
    int flash_card::feed()
    {
        bool y=true;
        int i=0;
        char return_value;
    
        for(i=0;(y=true);++i)
        {
            std::cout<<"\nEnter the word: ";
            std::getline(std::cin,question[i]);    //not executing, problem lies in this block
            std::cout<<"\nEnter its meaning: "; 
            std::getline(std::cin,answer[i]);
            std::cout<<answer[i];
            std::cout<<"\nPress n to exit or other key to continue: ";
            std::cin>>return_value;
            if(return_value=='n')
            y=false;
        }
        number_of_questions=i+1;
        return(number_of_questions);
    }
    
    void flash_card::test(int& total_noofqs)
    {
        int i=0;
        std::string local_answer;
        char return_value;
        while(i<total_noofqs)
        {
            std::cout<<"meaning for "<<question[i]<<" is?\n";
            std::getline(std::cin, local_answer);
            if(answer[i]==local_answer)
            {
                right(i, total_noofqs);
                continue;
            }
            else
            wrong();
            ++i;
            std::cout<<"\nDo you wanna continue the test: ";
            std::cin>>return_value;
            if(return_value=='n')
            break;
        }
    }
    
    void flash_card::right(const int index, int& totalqs)
    {
        int y=0;
        std::cout<<"\nright";
        for(y=0;(y<(number_of_questions-index));++y)
        {
            question[index]=question[index+1];
            answer[index]=answer[index+1];
            --totalqs;
        }
    }
    
    inline void flash_card::wrong()
    {
        std::cout<<"\nwrong";
    }
    
    inline void flash_card::learn()
    {
        int x=0;
        for(x=0;x<number_of_questions;++x)
        {
            std::cout<<"\n"<<question[x]<<" : "<<answer[x];
        }
    }
    
    int main()
    {
        flash_card list;
        int num;
        std::cout<<"\n1:feed, 2:test, 3:learn 4:quit";
        std::cout<<"\nenter ur choice";
    
        while(true)
        {
        std::cin>>num;
        if(num==4)
        break;
        switch(num)
        {
            case 1:
            list.feed();
            case 2:
            list.test(number_of_questions);
            case 3:
            list.learn();
            default:
            std::cout<<"\nenter a proper choice";
    
        }
        }
        return(0);
    }
    In the function int flash_card::feed()
    line 43 isn't executing
    I get the display to enter the word because std::cout<<"\nEnter the word: "; is executuing
    However, compiler doesn't wait for me to enter the word(std::getline(std::cin,question[i]); isn't executing).
    It continues to execute the next instruction.

    If this line doesn't execute then the whole program is useless.
    I would really appreciate any help for this problem.

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I'm going to guess it's because you are using both getline and std::cin >>.

    The problem is
    std::cin >>
    leaves a newline character in the stream, so when it comes around to getline, it gets an empty line automatically.

    You can do a std::cin.ignore(1, '\n'); after your cin >>.

    Or just use getline for everything, which I think is cleaner.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MarjunC View Post
    However, compiler doesn't wait for me to enter the word(std::getline(std::cin,question[i]); isn't executing).
    It continues to execute the next instruction.

    If this line doesn't execute then the whole program is useless.
    I would really appreciate any help for this problem.

    Thanks
    Most likely there is a "\n" remaining in the input buffer. Try adding this before the getline:
    Code:
    std::cin.ignore(INT_MAX);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To be pedantic, it should be
    std::cin.ignore(std::numeric_limits<int>::max());
    I forget which type cin.ignore takes, though, so may have to change the int part.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56
    No one said it(maybe because its more like a C approach?), but adding a fflush(stdin) before the line thats giving you trouble should work... probably...

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Absolutely not. No one has said it because it's undefined.
    SourceForge.net: Fflush - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by cyberfish View Post
    I'm going to guess it's because you are using both getline and std::cin >>.

    The problem is
    std::cin >>
    leaves a newline character in the stream, so when it comes around to getline, it gets an empty line automatically.

    You can do a std::cin.ignore(1, '\n'); after your cin >>.

    Or just use getline for everything, which I think is cleaner.
    By adding this line seems have solved the problem, Thank you for all the people to have replied. But, can anyone tell me whats wrong with the earlier code, when I give a instruction to receive i/p,it should wait till I give the input.But that wasn't happening.
    If someone gave me some idea about the mistake then I'll have better understanding of the usage of statements like ignore, fflush etc.

    Thanks

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    A stream is like a pipe. You put things in one end by typing them at the console, and they are taken out from the other end when you call getline or cin >>.

    They only wait for input when the pipe is empty. If not, it will just grab whatever's in the pipe, and move on.

    When you type, for example, "5[enter]", 2 characters are entered. a '5', and a newline ('\n').

    cin >> int only grabs an integer, so that leaves a newline in the stream.

    When it gets to getline, it thinks there is already an empty line (terminated by a newline), so it grabs that, and move on.

    cin.ignore(1, '\n'); says grab and discard the first character from the stream, if it's a newline. So when getline comes, the stream is empty, and it waits for more data (your input).

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int flash_card::feed()
    {
        bool y=true;
        int i=0;
        char return_value;
    
        for(i=0;(y=true);++i)
        {
    Typo? Sometimes less is more:
    Code:
    int flash_card::feed()
    {
        bool y=true;
        int i=0;
        char return_value;
    
        for(i=0;y;++i)
        {
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Skipping if statement
    By FallenBlade in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 03:40 PM
  2. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  3. compiler skips one statement
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 08:40 AM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM