I need my C programs to pause and wait for key press after completion

This is a discussion on I need my C programs to pause and wait for key press after completion within the C Programming forums, part of the General Programming Boards category; I'm searched about this but haven't found anything conclusive. I recently started compiling from the command line and it creates ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    8

    I need my C programs to pause and wait for key press after completion

    I'm searched about this but haven't found anything conclusive. I recently started compiling from the command line and it creates an executable. I'm using a Mac by the way... Anyhow, one of the programs I wrote for school executes, asks for the two integers to be input, but then, it just disappears!

    I need my terminal window to stay open so that I can at least see that my program works correctly!

    Suggestions?


    Code:
    ////  
    //  Program Assignment 1 - Least Common Multiple
    //  Desc: simple program to display LCM of two read integers
    //
    
    
    #include <stdio.h>
    
    
    //func decs
    int findLCM(int, int);
    void getNums(int*, int*);
    
    
    int main()
    {
        int a;
        int b;
        
        getNums(&a, &b);
        printf("The Least Common Mutiple of %d and %d is:\t%d", a, b, findLCM(a, b));
        return 0;
    }   //main
    
    
    void getNums(int* a, int* b)
    {
        printf("This program will accept (2) positive integers and calculate the Least Common Multiple");
        printf("\n\nEnter the first integer -> ");
        scanf("%d", a); //Why does this 'a' not need an address operator '&' preceding it?
        while (*a < 1) //Wanted to combine line 32 and line 33 but couldn't make it work. - Also, why does this 'a' need a '*' to work but the line 29 reference has nothing?
        {
            printf("\n\n*Integers MUST be positive.  Enter the first integer -> ");
            scanf("%d", a);
        }
        
        printf("\nEnter the second integer -> ");
        scanf("%d", b);
        while (*b < 1)
        {
            printf("\n\n*Integers MUST be positive.  Enter the second integer -> ");
            scanf("%d", b);
        }
    
    
            return;
    }   //getNums
    
    
    
    
    int findLCM(int a, int b)
    {
        int i;
        for (i = 1; (i % a != 0) || (i % b != 0); i++)
        {
            
        }
        return i;
    }   //findLCM

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    66
    You can use getchar() at the end of your main function.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    wow, that was a record! 3 minutes and an answer!

    Alas, I just tried that and it doesn't work.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by vertigoat View Post
    wow, that was a record! 3 minutes and an answer!
    Alas, I just tried that and it doesn't work.
    That's because your previous scanf's have left a newline in the buffer. You could try this:
    Code:
    char ch;  // at the top of main
    scanf(" %c", &ch); // before the return 0 in main
    Note that there's a space before the %c. That space will cause the newline (and any other extraneous whitespace) to be skipped.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    If you don't mind, can you explain that? For whatever reason, I just don't comprehend what you are saying. I mean, I can cut and paste the code, but that won't help me understand what is happening.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I honestly don't know what to explain. You've used scanf in your code. It's one more use of that function, the documentation of which I assume you've read.

    What exactly are you asking?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    The last statement in that program is a printf. why can't I wait for a key press after that?

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by vertigoat View Post
    The last statement in that program is a printf. why can't I wait for a key press after that?
    Who said you can't? That's exactly what I meant (read the comments).
    I meant you to do this:
    Code:
    int main() {
        int a;
        int b;
        char ch;
        getNums(&a, &b);
        printf("The Least Common Mutiple of %d and %d is:\t%d", a, b, findLCM(a, b));
        scanf(" %c", &ch);
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wait for a specific key press (c++)
    By RazzTheKid in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2008, 09:21 AM
  2. Pause or wait??
    By verbity in forum C# Programming
    Replies: 5
    Last Post: 11-05-2007, 09:13 AM
  3. Need help: wait for key press
    By Pan in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2006, 01:26 AM
  4. Replies: 2
    Last Post: 07-25-2003, 04:01 AM
  5. Pause or press any key to continue
    By muffin in forum C Programming
    Replies: 3
    Last Post: 09-12-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21