Loop Problem

W

Walter Roberson

My program should ask for repeating a calculation or not. I want to
solve it with a do..while, but the weird thing is that the program
totally ignores the scanf line where I want to receive the user input.
It starts the loop again by itself without receiving any input.
char quit;

do {
get_vars(pointer_on_m, results);
calculate(pointer_on_m, results);

printf("\n\rAnother calculation? (n for quit): ");
scanf("%c", &quit);
printf("\n\r");
}
while (quit != 'n');

Where is your EOF handling?

What -exactly- is your input? Is it *really* just the character 'n',
or is the user entering an 'n' followed by a return/enter? Where is
the input stream positioned after you read the first character?
 
W

Walter Roberson

my End of File handling? What exactly do you mean?

What will happen with your code if the user enters the
end-of-file sequence instead of further y/n inputs, or if the
input is redirected from a source that runs out of data?
What value will be stored into the character variable by the
scanf() if there is no more data?
 
W

Walter Roberson

Markus Pitha said:
Walter Roberson schrieb:
I cannot even enter anything. The program shows the printf ...(n to
quit) line at the end of the first program cycle, but the first function
(of these 2) already started again and I was not even able to enter
anything.

Let me guess: you have at least one input occuring before this point.
Read the scanf() documentation with regard to "trailing white space".
 
J

Jonathan Bartlett

Markus said:
Yes, in these two functions, I have some inputs, but only numbers, no
chars. I still wonder, why my problem works with numbers, but not with
chars. For C it must be no difference, isn't it?

It's picking up the white space (probably the return character) after
the numbers.

If you type 23456, and then hit the return key, the return key is STILL
waiting in the queue to be picked up by scanf. Now, if the next read is
for a number, then it will skip over whitespace (including your return).
But if it's just looking for a character, your return that you already
typed in but is still waiting to be read will do just fine.

Jon
 
C

Chris Torek

... the weird thing is that the program
totally ignores the scanf line ...

Never, ever use scanf().

(For advanced C programmers: rarely use scanf(). :) )

See the comp.lang.c FAQ, questions 12.17 and 12.18a.
 
M

Markus Pitha

Hi,
My program should ask for repeating a calculation or not. I want to
solve it with a do..while, but the weird thing is that the program
totally ignores the scanf line where I want to receive the user input.
It starts the loop again by itself without receiving any input.
The funny thing is that it works with numbers. When I change the input
to receiving numbers, the program waits on this part, but why not when I
want to give a char as input?
Here is this part:

char quit;

do {
get_vars(pointer_on_m, results);
calculate(pointer_on_m, results);

printf("\n\rAnother calculation? (n for quit): ");
scanf("%c", &quit);
printf("\n\r");
}
while (quit != 'n');

It would work like this, but why?:

int quit;

do {
get_vars(pointer_on_m, results);
calculate(pointer_on_m, results);

printf("\n\rAnother calculation? (0 for quit): ");
scanf("%d", &quit);
printf("\n\r");
}
while (quit != 0);
 
W

Walter Roberson

:> Walter Roberson schrieb:

:>>Let me guess: you have at least one input occuring before this point.
:>>Read the scanf() documentation with regard to "trailing white space".

:> Yes, in these two functions, I have some inputs, but only numbers, no
:> chars. I still wonder, why my problem works with numbers, but not with
:> chars. For C it must be no difference, isn't it?

:It's picking up the white space (probably the return character) after
:the numbers.

:If you type 23456, and then hit the return key, the return key is STILL
:waiting in the queue to be picked up by scanf.

Exactly. Which the scanf() documentation makes clear. I was giving
some pretty specific hints about exactly which sections of the FM that
the OP should RT, but I was hoping to "teach to fish" rather than
just giving the complete answer.
 
M

Markus Pitha

Hello,

Walter said:
Where is your EOF handling?

my End of File handling? What exactly do you mean?
What -exactly- is your input? Is it *really* just the character 'n',
or is the user entering an 'n' followed by a return/enter? Where is
the input stream positioned after you read the first character?

I cannot even enter anything. The program shows the printf ...(n to
quit) line at the end of the first program cycle, but the first function
(of these 2) already started again and I was not even able to enter
anything.
 
W

Walter Roberson

:> Never, ever use scanf().

:Ok, so fgets would be the right solution, wouldn't it?

Note, with fgets() you have to deal with the issue that there might
not be a newline within the number of size of the buffer you provided.
 
M

Markus Pitha

Walter said:
Let me guess: you have at least one input occuring before this point.
Read the scanf() documentation with regard to "trailing white space".

Yes, in these two functions, I have some inputs, but only numbers, no
chars. I still wonder, why my problem works with numbers, but not with
chars. For C it must be no difference, isn't it?
 
M

Markus Pitha

Hello,

Chris said:
Never, ever use scanf().

(For advanced C programmers: rarely use scanf(). :) )

See the comp.lang.c FAQ, questions 12.17 and 12.18a.

Ok, so fgets would be the right solution, wouldn't it?
 
C

CBFalconer

Chris said:
Never, ever use scanf().

(For advanced C programmers: rarely use scanf(). :) )

See the comp.lang.c FAQ, questions 12.17 and 12.18a.

The phrase "if (1 == scanf("%d", &integer))" is quite useful for
getting numbers out of a stream. I prefer to encapsulate it in a
function, together with a routine to do line flushing. But once
you can't check performance with "1 == scanf" I agree with you.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
P

pete

Markus said:
Hello,



Ok, so fgets would be the right solution, wouldn't it?

scanf is OK for string input, but not so good for anything else.
Strings, fortunately can be easily parsed any way you like.

new.c shows a more less bulletproof way of getting string input.
The drawback is that it doesn't let you know if you've
entered a line that's longer than your array.
However, in situations such as this one, you don't care.

C:\Program Files\DevStudio\SharedIDE\bin\Debug>new
Another calculation? (n for quit): Yes please

Another calculation? (n for quit): No thank you


C:\Program Files\DevStudio\SharedIDE\bin\Debug>


/* BEGIN new.c */

#include <stdio.h>

#define STRING_LENGTH 1
#define str(s) # s
#define xstr(s) str(s)

int main(void)
{
char string[STRING_LENGTH + 1];
int rc;

do {
printf("Another calculation? (n for quit): ");
fflush(stdout);
rc = scanf("%" xstr(STRING_LENGTH) "[^\n]%*[^\n]", string);
if (!feof(stdin)) {
getchar();
}
putchar('\n');
} while (rc == 1 && *string != 'n' && *string != 'N');
return 0;
}

/* END new.c */

If rc is one then you have a string in the array.
If rc isn't, then you don't.
If rc is zero, that means that only a newline character was input.
If rc is EOF, then you have an end of file condition.
 
C

Chico

Markus said:
Hi,
My program should ask for repeating a calculation or not. I want to
solve it with a do..while, but the weird thing is that the program
totally ignores the scanf line where I want to receive the user input.
It starts the loop again by itself without receiving any input.
The funny thing is that it works with numbers. When I change the input
to receiving numbers, the program waits on this part, but why not when I
want to give a char as input?
Here is this part:

char quit;

do {
get_vars(pointer_on_m, results);
calculate(pointer_on_m, results);

printf("\n\rAnother calculation? (n for quit): ");
scanf("%c", &quit);
printf("\n\r");
}
while (quit != 'n');

It would work like this, but why?:

int quit;

do {
get_vars(pointer_on_m, results);
calculate(pointer_on_m, results);

printf("\n\rAnother calculation? (0 for quit): ");
scanf("%d", &quit);
printf("\n\r");
}
while (quit != 0);


I had the same problem; I "fixed" it by writing:

scanf("%s", &input);

Although a lot of people here would eat me alive because that says to
look for a string not a character.
 
G

Grumble

Chico said:
scanf("%s", &input);

Although a lot of people here would eat me alive because that says to
look for a string not a character.

What type did you use for the variable 'input'?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top