Thread: Need assistance with loop and else if

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    Need assistance with loop and else if

    Hi All,

    I'm trying to figure out how I can test a condition (negative or positive number) and then have it go back into the for loop. What I'm trying to accomplish is if the user enters a negative number the user will get an error and then should be re-prompted for a positive number like this:

    First, enter the number of salaries to process: 4

    Now enter the 4 salaries to be averaged.

    Enter salary #1: 10000
    Enter salary #2: 8000
    Enter salary #3: -20000
    *** Invalid entry. Salary must be positive. ***
    Enter salary #3: 25000
    Enter salary #4: -3333
    *** Invalid entry. Salary must be positive. ***
    Enter salary #4: 52000

    The Average of the 4 salaries entered is: $ 23750.00

    The Total of the 4 salaries entered is: $ 95000.0

    Any help would be very appreciated:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        
          
        int     i, salary;
        int     numb_salaries = 0;
        int     sal_total = 0;
        float   total = 0;
        
        
        printf("Welcome to the  Employee Calculator Program. \n\n");
                
        printf("This program calculates the average and total of as ");
        printf("many employee salaries as you wish to enter. \n\n");
        
        printf("First, enter the number of salaries to process: ");
        scanf ("%i", &numb_salaries);
        
        printf("\nNow enter the salaries to be averaged. \n\n");
        
                /*  Enter for loop  */
                    
                for (i = 1; i <=numb_salaries; ++i)
                {
                    
                    printf("Enter salary #%i: ",i);
                    scanf ("%i", &salary);
                    sal_total = sal_total + salary;
                    
                        if ( salary < 0 )
                            salary = -salary;
                                    
                    printf("*** Invalid entry. Salary must be positive. *** ");        
                    
                                  
                    
                }
        
                printf ("Thanks for using the program.\n\n");
        
                getchar(); /* Pause output */
    
                return (0);
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make a flowchart from your code, then analyze it and you shall easily find your error.
    And you didn't mention what you had problems with.
    And just a FYI,
    sal_total = sal_total + salary;
    can be written as
    sal_total += salary;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok, so within the loop you have:

    Code:
    printf("Enter salary #&#37;i: ",i);
    scanf ("%i", &salary);
    sal_total = sal_total + salary;
                    
    if ( salary < 0 )
      salary = -salary;
                                    
    printf("*** Invalid entry. Salary must be positive. *** ");
    So you are saying:

    Code:
    1. "Enter Salary #(loop iteration)"
    2. [receive a number]
    3. [add the received number to your total]
    
    4. If The_number_you_already_added_to_your_total is negative
         Negate The_number_you_already_added_to_your_total
    
    5. "*** Invalid entry. Salary must be positive. ***"

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Any logic errors you see now?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You don't need to hand out the answers.
    Programmers who has logic errors they cannot fix should learn how to make flowcharts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed