![]() |
| | #1 |
| Registered User Join Date: Apr 2005
Posts: 2
| Simple question about pausing program 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);
}
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);
}
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 | |
| | #2 |
| Registered User Join Date: Nov 2004 Location: india
Posts: 493
| |
| PING is offline | |
| | #3 |
| Just Lurking Join Date: Oct 2002
Posts: 4,990
| Code: scanf("%f", &weight);
Code: while ((ch = getchar()) != '\n' && ch != EOF); 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 | |
| | #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 | |
| | #5 |
| +++ OK NO CARRIER 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 | |
| | #6 |
| Registered User Join Date: Jan 2005
Posts: 847
| ...or just run your program from the command prompt. |
| Quantum1024 is offline | |
| | #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 | |
| | #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;
}
![]() Heh never mind I get it now! *smacks head* Last edited by 0rion; 04-01-2005 at 11:50 PM. |
| 0rion is offline | |
| | #9 |
| Registered User Join Date: Mar 2005
Posts: 26
| 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);
}
__________________ any questions any type in programming a ready made answer ----------------------------------------------------------------------------------- FORTUNE FAVOURS THE BOLD! ----------------------------------------------------------------------------------- |
| coolshyam is offline | |
| | #10 |
| Registered User 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 | |
| | #11 |
| +++ OK NO CARRIER 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 | |
| | #12 |
| Registered User 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 | |
| | #13 |
| +++ OK NO CARRIER 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 | |
| | #14 |
| Registered User 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 | |
| | #15 |
| +++ OK NO CARRIER 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |