# Simple loop problem I cant seem to solve

• 03-21-2010
LightYear
Simple loop problem I cant seem to solve
Hey all I'm new here and I really appreciate this resource and all your help.
So here goes,...

I am in a beginners c programming class and I have been presented with the following problem:
"Write a program that finds the largest in a series of numbers enterd by the user. The program must prompt the user to enter numbers one by one. When user enters 0 or a negative number, the program terminates and must display the largest nonnegative number entered."

Okay so here's my code so far:

insert
Code:

``` #include <stdio.h> int main(void) {  float number, highestnumber;  printf("Enter a series of numbers and I will tell you which is the largest.\nEn ter a 0 or negative number when you wish to terminate the program.\n");     scanf("%f", &number);     while  (number != 0 && number > 0)  {     printf("Enter your next number: ");     scanf("%f", &number);     if (number >= highestnumber) {     number =  highestnumber; }   else if (number < highestnumber) {     highestnumber = highestnumber; }                     }      // end of while loop   printf("The highest number is: %2f\n", highestnumber);  return 0; }```

Anyway, the program works as far as it allows you to enter as many numbers as you want, until you hit 0 or a negative, then it terminates.

The problem is that no matter what the highest number was, the program always says that the highest number is 0.

There must be some relation between the highestnumber and number that I am missing, but does anyone have any ideas? This is due tomorrow and the chapter has no analogous problems that will help me.

Thanks a bunch

-P
• 03-21-2010
itCbitC
Without initializing highestnumber, it'll contain garbage so tests like if (number >= highestnumber) will be uncertain.
• 03-21-2010
claudiu
You got the logic right but you are making two mistakes:

1) highestnumber is not initialized to a value. Initialize it to 0.0 at the beginning of the program.

2) when you input a number larger than highestnumber assign highestnumber the value of the number read instead of the other way around.

3) There is no need to have an else. If the number you read is smaller than your current highest don't do anything. You just don't care about it.
• 03-21-2010
LightYear
Thank you so much let me go implement that. :)
• 03-21-2010
LightYear
Okay works better now except that I can't seem to get it to include the number from the first call of scanf in the set of possible highest numbers. It seems to include only those within the loop. Perhaps the loop should end before the final if (statement)?

insert
Code:

``` #include <stdio.h> int main(void) {  float number, highestnumber;  highestnumber = 0.0;  printf("Enter a series of numbers and I will tell you which is the largest.\nEn ter a 0 or negative number when you wish to terminate the program.\n");  scanf("%f", &number);     while  (number != 0 && number > 0)  {     printf("Enter your next number: ");     scanf("%f", &number);     if (number >= highestnumber) {     highestnumber = number; }                     }      // end of while loop   printf("The highest number is: %2f\n", highestnumber);  return 0; }```
• 03-21-2010
LightYear
If I remove the first call of scanf it goes back to giving me 0 as the answer.
• 03-21-2010
tabstop
If you are going to read an extra number outside the loop, just set highestnumber to that read-in number; and then do the loop normally.
• 03-21-2010
LightYear
So I could prompt the user to enter a number n, and initialize highestnumber as highestnumber=n
?
• 03-21-2010
claudiu
Remove the first printf and scanf outside the loop and change the loop from a while() to a do while since you need to priming.

Code:

```do{   printf   scanf }while(number > 0);```