C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-01-2005, 01:31 AM   #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
Noid is offline   Reply With Quote
Old 04-01-2005, 01:53 AM   #2
Registered User
 
PING's Avatar
 
Join Date: Nov 2004
Location: india
Posts: 493
http://faq.cprogramming.com/cgi-bin/...&id=1043284385

the faq at cprogramming.com is really nice..
__________________
Go Petr !!
My Blog
PING is offline   Reply With Quote
Old 04-01-2005, 08:53 AM   #3
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 4,990
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.*
Dave_Sinkula is offline   Reply With Quote
Old 04-01-2005, 11:31 AM   #4
Registered User
 
Join Date: Apr 2004
Posts: 171
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);
}
0rion is offline   Reply With Quote
Old 04-01-2005, 01:07 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,259
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-01-2005, 01:38 PM   #6
Registered User
 
Join Date: Jan 2005
Posts: 847
...or just run your program from the command prompt.
Quantum1024 is offline   Reply With Quote
Old 04-01-2005, 05:42 PM   #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
Noid is offline   Reply With Quote
Old 04-01-2005, 11:48 PM   #8
Registered User
 
Join Date: Apr 2004
Posts: 171
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.
0rion is offline   Reply With Quote
Old 04-02-2005, 08:46 AM   #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!
-----------------------------------------------------------------------------------
coolshyam is offline   Reply With Quote
Old 04-02-2005, 09:02 AM   #10
Registered User
 
Joelito's Avatar
 
Join Date: Mar 2005
Location: Tijuana, BC, México
Posts: 282
What's wrong with System("pause")?
I know some commands aren't portables but... either way can pause the programm... or I miss something?
Joelito is offline   Reply With Quote
Old 04-02-2005, 09:05 AM   #11
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,259
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-02-2005, 09:10 AM   #12
Registered User
 
Joelito's Avatar
 
Join Date: Mar 2005
Location: Tijuana, BC, México
Posts: 282
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?
Joelito is offline   Reply With Quote
Old 04-02-2005, 09:11 AM   #13
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,259
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-02-2005, 09:38 AM   #14
Registered User
 
Joelito's Avatar
 
Join Date: Mar 2005
Location: Tijuana, BC, México
Posts: 282
So... quzah are you in flame war to me? Talk about free speaking.
Joelito is offline   Reply With Quote
Old 04-02-2005, 09:46 AM   #15
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,259
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple question about the C programming tutorial #1 elsheepo C Programming 13 01-19-2009 08:59 PM
Possibly simple question PAragonxd C++ Programming 1 09-14-2008 02:43 AM
Can someone look at my simple program and tell me what is wrong with it? xMEGANx C++ Programming 16 09-30-2007 11:08 PM
Mystery bug in simple program? - I am beginner archie123 C++ Programming 7 04-08-2005 07:23 AM
question about the loop in case conversion program Elhaz C++ Programming 8 09-20-2004 04:06 PM


All times are GMT -6. The time now is 01:19 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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