1

My simple code for getting info from user & print the same works fine with Windows OS (on Dev C++) but But its not asking for string when run on Ubuntu (tried Terminal/Dev C++ via WINE/Code Blocks)

   #include<stdio.h>
   void display();
   struct book
  {
         int isbn;
         char name[25];
  }b[5];

   void PUSH()
   {
        int top;
        for(top=0;top<5;top++)
        {
        printf("Enter the ISBN no:");
        scanf("%d",&(b[top].isbn));
        printf("Enter the name:");
        fflush(stdin);
        gets(b[top].name);
        }
   }
  main()
  {
             PUSH();
             display();
  }
  void display()
  {
      int i;
      for(i=0;i<5;i++)
      {

      printf("----------------\nISBN no: %d",(b[i].isbn));
      printf("\nBook Name: %s \n",(b[i].name));
      }
      printf("----------------\n");
  }
  void POP()
  {
       int i;
      for(i=0;i<5;i++)
      {

      printf("----------------\nISBN no: %d",(b[i].isbn));
      printf("\nBook Name: %s \n",(b[i].name));
      }
      printf("----------------\n");

  }

Output on Ubuntu:

   Enter the ISBN no:23
   Enter the name:Enter the ISBN no:

After i input 23 it has to ask for the name of the book but its skipping that part & again asks for next isbn no.

After some blind retries I found that when gets(b[top].name); gets(b[top].name); was used twice like this it worked fine....

My question is why is it so.? How to make ubuntu compile & run programs that run fine on windows?

2 Answers 2

6

This is really because your C code is broken, its not the fault of Ubuntu or anything else.

Here's a few tips on how to fix it to work though, and how to compile it.

  1. system("pause"); don't do it. Avoid system calls, as they hard-code a requirement of the end system (which doesn't exist on Linux)

  2. gets(b[top].name); should be replaced by scanf("%s", &b[top].name); which is a more reliable way of taking string input.

  3. main() is invalid you should replace main() with int main() and replace system("pause"); with a simple return 0; to make your C standards-compliant.

How to compile

Compiling in Linux is much more simple than windows (for small programs like this).

  • Open a command line.
  • cd ~/your/code/
  • Compile with this command: gcc -Wall --pedantic -o my_program my_program.c
    • If this command fails, you need to run sudo apt-get install build-essential

And lastly some important advice: Dev C++ died out in 2005 - On windows, switch to Visual C++ 2010 Express, on Linux use Eclipse or a text editor and the gcc command.


Hope this helps

2
  • Great information! This also helps me.
    – nanofarad
    Aug 7, 2012 at 17:27
  • it helped... thanx..
    – Ravi Ojha
    Aug 7, 2012 at 18:27
3

gcc describing fflush(stream), says:

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

fflush is not defined for output streams. It is defined for output streams and for bidirectional streams on which the last operation was output. stdin is an input stream and fflush(stdin) has no meaning.

So when you input 23 and press enter. After that fflush doesn't do anything. And gets() function just took a \n. Replace with it scanf.

There are lot improvement can be done in the code. Look through it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .