Thread: A Simple Question (I think)

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    3

    Smile A Simple Question (I think)

    Hey,
    I'm learning C Programming at Uni and was given the following code to compile and try the program...

    Code:
    #include <stdio.h>									
    main()															
    {							
    
    int pets;							
    int cats;	
    int dogs;	
    
    printf("\a How many pets do you have?\n");				
    scanf("%d", &pets);							
    printf("So, you have %d pets! How many of these are cats?\n", pets);	scanf("%d", &cats);	
    printf("and how many dogs?\n", dogs);
    scanf("%d", &dogs);	
    printf("So, you have %d pets of which %d are cats and %d dogs!\n", pets, cats, dogs);
    
    return 0;
    
    }
    Can someone explain why the window is closing before the final line is printed and how I stop this?

    Thank you for your time,
    Scott

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd bet that it does print the final line, but it's so shortly before it closes the window that you don't see it.

    From the FAQ:
    Stop my Windows Console from disappearing everytime I run my program?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    3
    that makes sence, thank you very much for pointing out the article

    so I'm guessing it would just be easiest to repeat
    Code:
    scanf("%d", &dogs);
    after the last print so it thinks its waiting for another input?

    thank you very much for your help, it's appreciated!

    Scott

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by slawrence10 View Post
    that makes sence, thank you very much for pointing out the article

    so I'm guessing it would just be easiest to repeat
    Code:
    scanf("%d", &dogs);
    after the last print so it thinks its waiting for another input?

    thank you very much for your help, it's appreciated!

    Scott
    Yes, that would work - it's a bit excessive, but it'll do the job.

    I'm so old that I'm used to running things from a command window rather than just clicking things in the IDE, so I usually just let the progra finish, but since I'm already in a command window, the text remains - I just get a C:\somedir> at the bottom of the screen.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    3
    ah great, just tried that, so no code changing needed, even better

    thanks again for your help!

    Scott

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    or you can put these at the end of your code right before the return command of the main function:
    Code:
    fflush(stdin);
    getchar();
    so the program waits for an additional ENTER at the end to close

  7. #7
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    In windows, system("PAUSE"); expects any key for the program to continue. This also automatically inserts the text (on a new line): Press any key to continue . . .
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Never use fflush(stdin), its behavior is undefined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM