Thread: Simple question about pausing program

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Simple question about pausing program

    First off, I just started learning how to use C a few days ago by using a 1988 version of C Primer Plus. I think it's a little outdated, and I'm about to burn this book. But here is my question; I'm trying to get the simple programs in the tutorials to stay open so I can see what is happening ( they close immediately). I found this script (don't even know if it is called a script) on this website:
    Code:
    #include <stdio.h> 
    
    int main(void)  /*wait for keypress*/
    {
      int ch;
      printf ("Press [Enter] to continue");
      while ((ch = getchar()) != '\n' && ch != EOF);
      return(0);
    }
    it works when this is all that my program consists of, but when I try to add it to a program in one of the tutorials, such as:
    Code:
    #include <stdio.h> 
    
    int main(void)  /*wait for keypress*/
    {
     
    float weight, value;
    char beep;
    
    beep = '\007';
    printf("are you worth your weight in gold?\n");
    printf("Please enter your weight in pounds, and we'll see.\n");  
    scanf("%f", &weight);
    value = 400.0*weight*14.5833;
    printf("%cYour weight in gold is worth $%2.2f%c.\n", beep,value,beep);
    printf("Your are easily worth that! If gold prices drop, ");
    printf("eat more\nto maintain your value.\n");    
        
    int ch;
    printf ("Press [Enter] to continue");
     while ((ch = getchar()) != '\n' && ch != EOF);
     return(0);
      
     }
    it does not ask for me to hit enter, but just closes after running the commands.

    Did I enter something wrong?

    If anyone helps with this simple problem, I am extremely greatful. I pretty much wasted a night on figuring this out, and I've given up searching for an answer on the web.

    By the way, I'm using Dev-C++

    Help me overcome! thanks

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    the faq at cprogramming.com is really nice..
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    scanf("%f", &weight);
    You type a number followed by the enter key, right? The number is converted and the newline is left in the stdin. Without waiting for you to enter anything new, the newline is then consumed here.
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    See also
    FAQ > How do I... (Level 2) > Flush the input buffer
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    A simple method is to just add a "ch= getchar()" line that removes off the extra '\n' from the scanf() input.

    Code:
    #include <stdio.h> 
    
    int main(void)  /*wait for keypress*/
    {
     
    	float weight, value;
    	char beep;
    	int ch;
    
    	beep = '\007';
    	printf("are you worth your weight in gold?\n");
    	printf("Please enter your weight in pounds, and we'll see.\n");  
    	scanf("%f", &weight);
    	value = 400.0*weight*14.5833;
    	printf("%cYour weight in gold is worth $%2.2f%c.\n", beep,value,beep);
    	printf("Your are easily worth that! If gold prices drop, ");
    	printf("eat more\nto maintain your value.\n");    
    
    	ch=getchar();    
    	printf ("Press [Enter] to continue");
    	while ((ch = getchar()) != '\n' && ch != EOF);
    	return (0);
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your "simple method" breaks if they type anything else other than one character past what you want them to. That's why we tell you to read the FAQ. Or rather, that's why it's been suggested two times already. Well, here's hoping "three times is a charm".

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    ...or just run your program from the command prompt.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    i have a long way to go...but these all get me past my problem, so I can continue with the tutorials.

    Thanks everyone, I appreciate the help

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Truth was I didn't really get how the FAQ code would flush the buffer in his case

    Code from the FAQ:
    Code:
    #include <stdio.h> 
    
    int main(void)
    {
      int   ch;
      char  buf[BUFSIZ];
      
      puts("Flushing input");
      
      while ((ch = getchar()) != '\n' && ch != EOF);
      
      printf ("Enter some text: ");
      
      if (fgets(buf, sizeof(buf), stdin))
      {
        printf ("You entered: %s", buf);
      }
      
      return 0;
    }
    In this case there was nothing in the buffer before the while() loop which meant all it did was discard characters until a new line (or EOF) was reached. Since you stored it in ch, obviously the fgets() won't be effected. Or I'm not seeing something obvious here

    Heh never mind I get it now! *smacks head*
    Last edited by 0rion; 04-01-2005 at 11:50 PM.

  9. #9
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Wink A Simple Solution

    dude.its all done.hope it works on Dev C++
    Code:
    #include <stdio.h> 
    #include<dos.h>
    int main(void)  /*wait for keypress*/
    {
     
    float weight, value;
    char beep;
    
    beep = '\007';
    printf("are you worth your weight in gold?\n");
    printf("Please enter your weight in pounds, and we'll see.\n");  
    scanf("%f", &weight);
    value = 400.0*weight*14.5833;
    printf("%cYour weight in gold is worth $%2.2f%c.\n", beep,value,beep);
    printf("Your are easily worth that! If gold prices drop, ");
    printf("eat more\nto maintain your value.\n");    
        
    int ch;
    shyam:
    printf ("Press [Enter] to continue");
    ch=getchar();
     if ((ch = getchar()) != '\n' && ch != EOF)
     {
    	 goto shyam;
    
     }
     else
     {
    	goto ram;
     }
    ram:
     return(0);
      
     }
    please maintain structuring in your code.it will make our job of debugging easier.

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  10. #10
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    What's wrong with System("pause")?
    I know some commands aren't portables but... either way can pause the programm... or I miss something?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, if you had read the FAQ...

    But then, you already answered your own question. Why use a nonportable method when there are portable versions that work just as well?

    But then, if you had read the FAQ...

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    mmm, I did.. but if the program is ment to run only in my machine... and I can support those non-portable... wouldn't be heavy to use them, right?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do whatever the ........ you want. No one here gives a ......... But when we try to help people, we're not just talking about "my machine". Therefore, we don't post stupid system specific answers when there's a portable option that's just as easy.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    So... quzah are you in flame war to me? Talk about free speaking.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You asked what was wrong with system( "pause" ), I told you. You replied that for you you'd use it anyway, I said I didn't care. End of discussion. If you want to carry it further, don't waste space in this thread, PM me.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 08:59 PM
  2. Possibly simple question
    By PAragonxd in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2008, 02:43 AM
  3. Replies: 16
    Last Post: 09-30-2007, 11:08 PM
  4. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM
  5. question about the loop in case conversion program
    By Elhaz in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2004, 04:06 PM