Thread: Very short code tt never work...please help

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    Very short code tt never work...please help

    The code looks like this (got this from dev++ C tutorial):
    Code:
    #include <stdio.h>
    
    main ()
    {
    int num1, num2;
    
    printf("\nEnter first number ");
    scanf("%d",&num1);
    
    printf("\nEnter second number ");
    scanf("%d",&num2);
    
    if (num2 ==0) printf("\n\nCannot devide by zero\n\n");
    else printf("\n\nAnswer is %d\n\n",num1/num2);
    
    }
    The problem is after the prog prompt me for the second no, and after I enter and hit the 'Enter' key, the whole program disappears - failing to print either the statements, "Cannot devide by zero" or "Answer is...".

    Tried different compiler like Borland and also replacing scanf with getchar - and still get the same thing. Wat is wrong???

    Thanks a zillion for your help.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869

  3. #3
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Try this:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    int num1, num2;
    
    printf("\nEnter first number ");
    scanf("%d",&num1);
    
    printf("\nEnter second number ");
    scanf("%d",&num2);
    
    if (num2 ==0) printf("\n\nCannot devide by zero\n\n");
    else printf("\n\nAnswer is %d\n\n",num1/num2);
    
    system("PAUSE");
    return 0;
    }

    [EDIT]


    Dammit, I always forget that the FAQ knows all.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    12

    reply

    try this.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main ()
    {
    int num1, num2;
    
    printf("\nEnter first number ");
    scanf("%d",&num1);
    
    printf("\nEnter second number ");
    scanf("%d",&num2);
    
    if (num2 ==0) printf("\n\nCannot devide by zero\n\n");
    else printf("\n\nAnswer is %d\n\n",num1/num2);
    
    getch();
    }
    I think that your console is not visible because you are not returning nothing to os.

    this code works fine in turbo c++ 4.5 without any problem.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Wow. People, the only time the program pauses really is for input. So PLEASE use a standard function for input like getchar(). Then your program pauses and you don't even need to include an extra library.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Dammit, I always forget that the FAQ knows all.
    The FAQ even knows to include stdlib.h for the system function.

    >this code works fine in turbo c++ 4.5 without any problem.
    Not everyone has Turbo C++ v.old as dirt. In fact, you shouldn't either unless you're working with legacy code that requires it.
    My best code is written with the delete key.

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    You need a buffer flush and then call getchar:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    int num1, num2, ch;
    
    printf("\nEnter first number ");
    scanf("%d",&num1);
    
    printf("\nEnter second number ");
    scanf("%d",&num2);
    
    if (num2 ==0) printf("\n\nCannot devide by zero\n\n");
    else printf("\n\nAnswer is %d\n\n",num1/num2);
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    getchar();
    return 0;
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Quote Originally Posted by Prelude
    >Dammit, I always forget that the FAQ knows all.
    The FAQ even knows to include stdlib.h for the system function.

    Bahahahaha, I was slow last night.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get sample shell code to work...
    By jcafaro10 in forum C Programming
    Replies: 6
    Last Post: 07-07-2010, 08:04 AM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. Replies: 9
    Last Post: 09-09-2007, 08:08 AM
  4. learning to work with SDKs & source code out there...
    By psasidisrcum in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2005, 09:26 PM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM