Thread: Print Maximum number

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    Print Maximum number

    Hey guys-

    I'm having an issue with this program I wrote. It is supposed to have a user enter values and then output the maximum value of those values entered, using very basic C Programming. If only the sentinel value is entered, it will print out an error message. I have that working properly, but when I enter values, it outputs the maxmium, but when I enter the sentinel value (999), it outputs that as the maximum, which it shouldn't do. Any ideas? I'm probably just overlooking something very simple. I appreciate the help! Thanks!!

    -----------------
    Code:
    ...
    while (temp != 999)
    {               
       printf("Enter another value [enter 999 to stop]:\n");
       scanf("%f",&temp);
       counter == counter++; 
    
    	if ( temp > max) 
    	{
    	max = temp;
    	}
    	else if ( temp < max)
    	{
    	temp = temp;
    	}
    }
    
    if ( counter != 1 )
       {
       printf("The maximum is: %f\n", max);
       }
    else
       {
       printf("No valid entires:\n");
       }
    }
    Last edited by rmathus; 10-07-2003 at 08:28 AM.

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    If the input is 999 then don't update the max value and don't output it.
    Code:
    while (temp != 999)
    {               
       printf("Enter another temperature [enter 999 to stop]:\n");
       scanf("%f",&temp);
       if(temp!=999)
       {
          counter == counter++; 
    
    	if ( temp > max) 
    	{
    	max = temp;
    	}
    	else if ( temp < max)
    	{
    	temp = temp;
    	}
          printf("Temperature = %f\n", temp);
          printf("Counter = %d\n", counter);
          printf("Maximum = %f\n", max);
       }
    }

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    Thanks! I figured it was just a stupid mistake. Appreciate it!

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Can I suggest a number of changes?

    Code:
    #include <stdio.h>
    
    int main() {
        float temp; 
        int counter = 0;
        float max;	
    
        printf("Enter a temperature [enter 999 to stop]:\n");
        scanf("%f", &temp);
        
        while (temp != 999) {
            counter++; 
            
            if (temp > max)  {
                max = temp;
            }
            
            printf("Temperature = %f\n", temp);
            printf("Counter = %d\n", counter);
            printf("Maximum = %f\n", max);
            
            printf("Enter a temperature [enter 999 to stop]:\n");
            scanf("%f", &temp);
        }
    
        if (counter > 1) {
            printf("The maximum temperature is: %f\n", max);
        } else {
            printf("No valid temperatures were entered\n");
        }
        return 0;
    }
    Edit: Formatting
    Last edited by Zainny; 10-07-2003 at 06:37 AM.
    Beware the fury of a patient man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shortest path algorithm with a maximum number of edges limit
    By blackslither in forum C Programming
    Replies: 4
    Last Post: 12-28-2008, 04:49 PM
  2. how to print a increasing number of words?
    By gershonj in forum C Programming
    Replies: 4
    Last Post: 07-11-2008, 07:36 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  4. can't print all the permutations of a number
    By whizkid in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2005, 10:41 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM