Thread: Re: [Dev-C++] Basic C question: scanf
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Nurhidayat <dh...@ma...> - 2001-07-12 06:06:12
|
At 20:47 11/07/2001 -0700, you wrote:
>When the program is run, the prompt "Enter salaries:" is printed. After
>that, I enter a series of numbers, then press the Enter key. The printf in
>the loop is executed for each number input, but then the program hangs.
No, it doesn't hang.
After the printf in the loop is executed, scanf still waiting legal input.
while (scanf("%lf", &salary) == 1) {
...
}
So to finish scanf, you have to give illegal input or non numeric
character, like 'x' (or another alphabet character you like) then press
[Enter].
Try to give input like this:
Enter salaries: 100 200 150 600 x [Enter]
If you write loop like this
while (scanf("%lf", &salary) != EOF) {
...
}
You can use <ctrl>Z to finish input.
___Nurhidayat___
|
|
From: Eric A. <eri...@ho...> - 2001-07-12 17:08:31
|
Or you coul do
//Add the following lines
#include <ctype.h>
#define Done 0;
#define True !done;
While(!done)
then somewhere in the loop ask if they was to continue
printf("Would you like to enter more values? Y/N/x20");
ch=toupper(gets(ch));
If(ch == 'Y') Done = True;
then end the loop.
>----- Original Message -----
>From: "Mary Sipple" <ms...@in...>
>To: <dev...@li...>
>Sent: Thursday, July 12, 2001 4:47 AM
>Subject: [Dev-C++] Basic C question: scanf
>
>
> > I've written a short program using Dev-C++ 4, but it does not execute as
>I
> > would expect. The code is as follows:
> >
> > #include <stdio.h>
> >
> > int main(int argc, char *argv[])
> > {
> > double salary, fed_withhold, state_withhold;
> > double salary_sum = 0, fed_sum = 0, state_sum = 0;
> >
> > printf("Enter salaries: ");
> > while (scanf("%lf", &salary) == 1){
> > fed_withhold = salary * .17;
> > state_withhold = salary * .03;
> > printf("salary: %.2f fed tax: %.2f state tax: %.2f\n",
> > salary, fed_withhold, state_withhold);
> > salary_sum += salary;
> > fed_sum += fed_withhold;
> > state_sum += state_withhold;
> > }
> > printf("\nSalary total: %.2f Fed tax total: %.2f State tax total:
>%.2f\n",
> > salary_sum, fed_sum, state_sum);
> >
> > system("PAUSE");
> > return 0;
> > }
> >
> > When the program is run, the prompt "Enter salaries:" is printed. After
> > that, I enter a series of numbers, then press the Enter key. The printf
>in
> > the loop is executed for each number input, but then the program hangs.
>I
> > have to either interrupt the program (<cntrl>-z worked for this) or type
>in
> > alpha characters and hit Enter. At that point the printf after the while
> > loop is executed.
> >
> > What have I got wrong or what is it that I don't understand?
> >
> > Thanks,
> >
> > Mary
> >
> >
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com
|
|
From: Mary S. <ms...@in...> - 2001-07-13 15:28:04
|
Thanks to everyone who replied. I had expected the <cr> to be a legal input.
Thanks for the info.
Mary
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of
Nurhidayat
Sent: Wednesday, July 11, 2001 11:03 PM
To: dev...@li...
Subject: Re: [Dev-C++] Basic C question: scanf
At 20:47 11/07/2001 -0700, you wrote:
>When the program is run, the prompt "Enter salaries:" is printed. After
>that, I enter a series of numbers, then press the Enter key. The printf in
>the loop is executed for each number input, but then the program hangs.
No, it doesn't hang.
After the printf in the loop is executed, scanf still waiting legal input.
while (scanf("%lf", &salary) == 1) {
...
}
So to finish scanf, you have to give illegal input or non numeric
character, like 'x' (or another alphabet character you like) then press
[Enter].
Try to give input like this:
Enter salaries: 100 200 150 600 x [Enter]
If you write loop like this
while (scanf("%lf", &salary) != EOF) {
...
}
You can use <ctrl>Z to finish input.
___Nurhidayat___
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Luigi S. <gi...@li...> - 2001-07-13 22:41:09
|
In my opinion you should use the solution suggested by Nurhidayat (I mean
compare the value returned by scanf with EOF, see below), in fact this
allows your code be used like a "filter".
Just in case you are not familiar with redirection and pipes, here is what
I mean.
Suppose you wish to input these values: 100 200 300
You could feed the values via console (as you usually do) or by the
following two steps: (I assume that you are in a DOS box, and your
executable is named salary.exe)
- write the values in a text file (say that you name the file: salary.dat)
- launch the command: salary < salary.dat.
try also: salary < salary.dat > out.dat
and : salary < salary.dat >> out.dat
The above is redirection.
Better yet: if the values to be fed to salary.exe are the output of another
executable (say it: compute.exe), assuming that it writes to stdout, you
could tie the output of "compute" to the input of "salary" by the following
command:
compute | salary
The above is an example of a "pipe".
Hope this was of some help.
At 22.43 12/07/01 -0700, you wrote:
>Thanks to everyone who replied. I had expected the <cr> to be a legal input.
>Thanks for the info.
>
>Mary
>
>
>
>If you write loop like this
>
> while (scanf("%lf", &salary) != EOF) {
> ...
> }
>
>You can use <ctrl>Z to finish input.
>
>
>
>___Nurhidayat___
>
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
---
gisan
|