Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 23rd, 2005, 9:26 PM   #1
fusionfreek
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 fusionfreek is on a distinguished road
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
}
fusionfreek is offline   Reply With Quote
Old Feb 23rd, 2005, 9:55 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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;
}
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:21 PM.

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