Posted by: JavaBeans June 23, 2006
Help on c program
Login in to Rate this Post:     0       ?        
Ah...programming is such an art... You can easily convert this Java code into C if you wish; I'm just not sure why your lecturer is wanting you to do this in C in the first place. C is good for anything 'networking'; for high level stuff like this well...you're looking at a high level language...

package com.sajha.hr.payroll;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;

/**
 * @author JavaBeans
 *
 */
public class CalcPay {
	private static BufferedReader keyboard;
	
	public static void main( String args[] ) throws IOException {		
		keyboard =
			new BufferedReader(
					new InputStreamReader( System.in ));
		System.out.println("Enter Employee ID (a number):");

		boolean exit=false;
		do {
			try {
				switch( checkValidNum() ) {
					case 1:
						calcMgr(); break;
					case 2:
						calcHourly(); break;
					case 3:
						calcCom(); break;
					case 4:
						outsrcWork(); break;				
					default: throw new PayrlException();
				}
				exit=true;
			} catch ( PayrlException pe) {
			System.err.println("ID's range must " +
					"be within 1-4. Try again.");
			}
		} while( !exit );
	}
	
	static void calcMgr() throws IOException {
		System.out.println("Enter the Manager's " +
				 "fixed weekly salary (a number):");
		System.out.println(
						"The managers's gross weekly " +
						"income is $" + checkValidNum() + ". Bye!");
	}
	
	static void calcHourly() throws IOException {
		System.out.println("Enter the hourly worker's " +
		 "fixed hourly wage (a number):");
		int wage = checkValidNum();
				
		System.out.println("Now enter the number of hours " +
								"they've worked for the week:");
		int hours = checkValidNum();
				
		double hrlyPay=0;
		if( hours > 40 ) {
			hrlyPay=(wage*40) + ((wage*1.5)*(hours-40));
		}else{
			hrlyPay=wage*hours;
		}
		System.out.println(
				"The hourly worker's gross weekly " +
				 "income is $" + hrlyPay + ". Bye!");
	}
	
	static void calcCom() throws IOException {
		System.out.println("Enter the commission worker's " +
		 "weekly sales (a number):");
		System.out.println(
				"The commission worker's gross weekly " +
				 "income is $" + (250+(.057*checkValidNum())) + ". Bye!");
	}

	static void outsrcWork() throws IOException {
		System.out.println("Enter the item no that was " +
		 "outsourced or contracted:");
		int num=0;
		
		//simple storage of values
		Hashtable item =
			new Hashtable();
		item.put("1", "1000");
		item.put("2", "2000");
		item.put("3", "3000");
		item.put("4", "4000");
		//etc..
		boolean exit=false;
		do{
			try {		
				num = checkValidNum();
				if( num>4 ) {
					throw new PayrlException();
				} else {
					exit=true;
				}
			} catch( PayrlException pe ) {
				System.err.println("Item range must " +
						"be within 1-4. Try again.");
			}

		} while( !exit );	
		System.out.println(
			"The contractor's gross weekly " +
				"income is $" + item.get(String.valueOf(
						 num )) + ". Bye!");
	}

	static int checkValidNum() throws IOException {
		int num=-1;
		boolean exit=false;
		do {
			try {
				num= Integer.parseInt( keyboard.readLine());
				exit=true;
			} catch( NumberFormatException nfe ) {
				System.err.println("I said a number! Try again: ");				
			}
		}while(!exit);
		return num;
	}
}

class PayrlException extends Exception {
	public PayrlException() {
		super();
	}
}

Cheers!
Read Full Discussion Thread for this article