Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   New to C (http://www.programmingforums.org/showthread.php?t=2430)

fusionfreek Feb 23rd, 2005 9:26 PM

New to C
 
Hi all, I am a student in my first programming class. We are learning C, and so far so good, except for a problem i keep having with scanf. Basically, if I am lookinig f an integer value and someone enters a character, I want the program to alert the error, and loop back. If the user enters an invalid integer it works, but the minute the user enters a character the program ends. any help on overcoming this would be appreciated. Here is a sample problem I am working on out of c primer plus.

:

/* exersise 8 --
 * Write a program that requests the hours worked in a week and then prints the gross pay, the taxes, and the net pay. Assume the following:

Basic pay rate = user enters choice

Overtime (in excess of 40 hours) = time and a half

Tax rate


15% of the first $300



20% of the next $150



25% of the rest



 *
 *
*/

#include <stdio.h>
#include <ctype.h>
int main(void)
{
        int hrs = 0;                                // the number of hours worked
        int othrs = 0;                                // the number of hrs over 40
        float grosspay = 0.0;                        // gross pay
        float netpay = 0.0;                        // net pay
        float txrate1 = 0.15;                        // rate for first 300
        float txrate2 = 0.20;                        // rate for next 150
        float txrate3 = 0.25;                        // rate for the rest
        float taxes        = 0.0;                        // taxes
        float basepay        = 0.0;                        // pay rate
        int choice        =0;                        // menu choice
        // get pay rate
        printf("****************************************************************\n");
        printf("Enter the number the corresponds with your current rate of pay: \n");
        printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
        printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
        printf("5) quit\n");
        printf("****************************************************************\n");
        scanf(" %d",&choice);
       
        // validate choice
        if ((choice > 5))
        {
                do
                {
                        printf("You did not enter a valid choice, please try again!\n\n");
                        printf("****************************************************************\n");
                        printf("Enter the number the corresponds with your current rate of pay: \n");
                        printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
                        printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
                        printf("5) quit\n");
                        printf("****************************************************************\n");
                }while((scanf(" %d",&choice) != 1) || (choice > 5));
        }


                switch(choice)
                {
                case 1:
                        basepay = 8.75;
                        break;
                case 2:
                        basepay = 9.33;
                        break;
                case 3:
                        basepay = 10.00;
                        break;
                case 4:
                        basepay = 11.20;
                        break;
                default:
                        printf("Quitting the program. Good bye!");
                        exit(0);
                }

       

        // get the hours worked
        printf("\nIt's time to get paid. ");
        printf("Please enter the number of hours you worked this week: ");
        scanf(" %d",&hrs);

       
        // calculate normal and ot hours
       
        if(hrs < 40)
        {
                othrs = 0;
        }
        else
        {
                othrs = hrs - 40;
                hrs = 40;
        }
       
        // calculate the gross pay
       
        grosspay = (hrs * basepay) + (othrs * basepay * 1.5);
       
        // calculate taxes
       
        if (grosspay <= 300.00)
        {
                taxes =        grosspay * txrate1;
                netpay = grosspay - taxes;
        }       
        else if (grosspay <= 450.00)
        {        taxes = ((grosspay - 300)*txrate2) + (300.00*txrate1);
                netpay = grosspay - taxes;
        }
        else
        {       
                taxes = ((grosspay - 450.00)*txrate3) + (150.00*txrate2) + (300.00*txrate1);
                netpay = grosspay - taxes;
        }       
        // display the results
       
        printf("Gross Pay:\t\t\t%.2f\n",grosspay);
        printf("Taxes    :\t\t\t%.2f\n",taxes);
        printf("Net Pay  :\t\t\t%.2f\n",netpay);
       
        return 0;        // end program
}


tempest Feb 23rd, 2005 9:55 PM

Use a method like this...

:

#include <stdio.h>

int getInt(char *msg);

int main(int argc, char *argv[]) {
    int num = getInt("Please enter a number: ");
    printf("You entered %d, thank you!", num);
    return 0;
}

int getInt(char *msg) {
    int num;

    try {
        printf(msg);
        scanf("%d", &num);
    } catch(char * errStr) getInt(msg);

    return num;
}



All times are GMT -5. The time now is 4:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC