Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

while loop problem

38 views
Skip to first unread message

ritchie31

unread,
Mar 28, 2013, 3:43:35 AM3/28/13
to
I am using some examples of some known book


#include <stdio.h>

/* Function main begins program execution */
int main()
{
int counter; /* number of grade to be entered next */
int grade; /* grade value */
int total; /* sum of grades input by user */
float average; /* average of grades */

/* initialization phase */
total = 0; /* initialize total */
counter = 0; /* initialize loop counter */

/* processing phase */
/* get the first grade from user */
printf("Enter grade, -1 to end: "); /* prompt for input */
scanf("%d",&grade); /* read grade from user */

/* loop while sentinel value not yet read from user */
while (grade != -1){
total = total + grade; /* add grade to total */
counter = counter + 1; /* increment counter*/

/* get next grade from the user */
printf("Enter grade, -1 to end: ");
scanf("%d",&grade);
} /* end while */

/* termination phase */
/* if user entered at least one grade */
if (counter != 0){
/* Calculate average of all grades entered */
average = (float) total / counter; /* avoid truncation */

/* Display average with digits of precision */
printf("Class average is %.2f\n", average);
} /* end if */
else { /* if no grades were entered, output message */
printf("No grades were entered\n");
} /* end else */

return 0; /* indicate that program ended succesfully */
} /* end function main */

when I run it it continues accepting values of grade = -1.
Where is the problem in this program?
Thanks in advance

Dag-Erling Smørgrav

unread,
Mar 28, 2013, 8:00:14 AM3/28/13
to
ritchie31 <medi...@gmail.com> writes:
> I am using some examples of some known book

Out of curiosity, which one?

> when I run it it continues accepting values of grade = -1.
> Where is the problem in this program?

The code looks correct (except for the use of an unsafe library function
and complete lack of error checking and reliance on the implementation
to flush stdout before reading from stdin) and works for me. What
happens when you run it?

DES
--
Dag-Erling Smørgrav - d...@des.no

Paul

unread,
Mar 28, 2013, 9:01:29 AM3/28/13
to
To help debug your problem, consider the code example
at the end of this article.

http://linux.die.net/man/3/scanf

"Return Value

These functions return the number of input items successfully
matched and assigned, which can be fewer than provided for,
or even zero in the event of an early matching failure."

The author of the code segment at the end of the article,
checks the returned value from scanf. If scanf didn't find
a valid input, then there's no reason to store a new value
in "grade". The old value might still be stored in there.

You might want to analyze what is coming back from scanf,
to understand what might be happening.

Defensive programming might seem like a chore, but
it pays off dividends later. If scanf returns a value,
and there is a way for scanf to screw up, you should
be checking for that condition.

In fact, as a rank amateur at C programming myself, if
I don't know how a function works, I may test it
first. I write a separate test program, to test my
understanding of how it works.

/* insert usual program constructs as necessary... */
/* this is a snippet */

int n;
int grade = 79;
n = scanf("%d",&grade);
printf("I got %d back from scanf, and %d is my grade\n", n, grade);

Then, I try some test values, 23, -1, invalid_string,
and so on, and for each test program run, see what happens
with respect to input. If I see a value of 79 printed for grade,
then I suspect no assignment to grade had been made.

Input is tricky, and you can easily spend more time making
the program robust against command line arguments, or input
formatting, than in the actual program logic.

You might also end up testing for differences, between
feeding the program from a file, rather than interactive
input from stdin.

grade_program < list_of_grades_file.txt > output_of_my_program.txt

I have no idea why your program is broken, but seeing
as you're not checking the returned value of scanf,
that's where I'd start to make changes.

HTH,
Paul

Keith Thompson

unread,
Mar 28, 2013, 10:58:06 AM3/28/13
to
ritchie31 <medi...@gmail.com> writes:
> I am using some examples of some known book
>
>
> #include <stdio.h>
>
> /* Function main begins program execution */
> int main()
[snip]
> return 0; /* indicate that program ended succesfully */
> } /* end function main */
>
> when I run it it continues accepting values of grade = -1.
> Where is the problem in this program?

I suggest posting your question to comp.lang.c rather than
comp.std.c. comp.std.c discusses the C standard document(s);
comp.lang.c discusses C, the language described by that standard.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

ritchie31

unread,
Mar 29, 2013, 3:30:10 AM3/29/13
to
I think the error was that I copied the executable from a different machine.
When I runned last night it was not working correctly.
So I recompiled it and it works fine now.
Thanks for your time.
0 new messages