0

So I have this code but I don't know why two lines in the code are spiking read. This two lines are marked by comments below:

   #include <iostream>
   #include<stdio.h>

using namespace std;

class publication{
     char title[20];
     int price;

     public:

     void getdata(){
        cout<< "Enter the price of the book";
        cin>>price;

        cout<< "Enter the title";  #this line 1st
        gets(title);               #this is not running
     }

      void putdata(){
       cout<<price;
       puts(title);
       }

};

class Tape:public publication{
       float play;

       public:

       void getdata(){
         cout<< "Enter play"; #this line 2nd 
         cin>>play;
       }

       void putdata(){
           cout<<endl<<play;
       }
};

int main()
{
    publication p;
    p.getdata();
    p.putdata();

    Tape t;
    t.getdata();
    t.putdata();

    book b;
    b.getdata();
    b.putdata();

}

Now I am unable to understand why line 16 and 47 i skipped. I have checked the syntax it's all good. There are no errors in this program. I use codeblocks and gnu gcc compiler for c++. This is the image

In this image as you can see the two lines have auto-compiled without taking the input for the title. Some lines of code are removed which were not relevant to the problem.

Joseph D.
  • 11,804
  • 3
  • 34
  • 67
  • 1
    Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Sander De Dycker Jun 23 '18 at 04:59
  • this is not a compilation (I've fixed title and text). Compilation is a process of transferring human readable code (in C++ for example) into machine code. – Marek R Jun 23 '18 at 05:37
  • 1
    @Steve314 sadly this is not duplicate. He didn't provide an input at all so this is different issue. – Marek R Jun 23 '18 at 05:40
  • @SanjayRajpoot did you try run program without debugger? As I remember there are some problem with gdb (Debugger used by Code Block) and standard input, so if you run this outside of CB it may work. Check also what value returns `cin.rdstate()`), most probably stream is in error state from the beginning. – Marek R Jun 23 '18 at 05:48
  • 1
    Do not use gets(), it's inherently flawed and a security risk. – Martin James Jun 23 '18 at 07:40

1 Answers1

1

This is neither a problem with the IDE or the compiler. Don't blame the poor guy CodeBlocks :(

I don't know what prompted you to use gets and puts in your program. If optimization is your concern try using std::ios::sync_with_stdio(false); with cin

Also do not use gets as it can lead to buffer over flows if you are not careful. Use fgets instead. See cplusplus and stackoverflow

Now, coming to the issue, the real reason why it looks like lines 16 and 47 are omitted is because of improper print statements and messages in your code. The only line that seems to be skipped is gets(title);. In fact, everything is executed.

Why is gets(title) skipped?

So when you enter price as 12 it is actually stored in the input buffer as 12\n. Now after reading the 12, we still have \n in the buffer, because cin doesn't read new line unlike cin.getlineand gets sees that new line as the terminating character. See gets

So clear the buffer using cin.ignore(numeric_limits<streamsize>::max(), '\n');

I have reformatted your code a bit. see below. Note that I have used fgets which treats new line as a valid character and \n is included in the title. See fgets

using namespace std;

class publication {
    char title[20];
    int price;

public:

    void getdata() {
        cout << "Enter the price of the book"<<endl;
        cin >> price;

        cout << "Enter the title" << endl;

        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        fgets(title, 20, stdin);        
    }

    void putdata() {
        cout << "Price is " << price << endl;
        cout << "Title is "<< title;
    }

};

class Tape :public publication {
    float play;

public:

    void getdata() {
        cout << "Enter play"<<endl; 
        cin >> play;
    }

   void putdata() {
        cout << "Play is " << play << endl;
    }
};

int main()
{
    publication p;
    p.getdata();
    p.putdata();

    Tape t;
    t.getdata();
    t.putdata();
}
HariUserX
  • 1,341
  • 1
  • 9
  • 17