Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Simple C Program driving me crazy, Any help?

  • 16-01-2012 5:03pm
    #1
    Registered Users, Registered Users 2 Posts: 400 ✭✭


    Heya, so i have to write out a C program, but i cannot for the life of me wrap my head around a certain part. I'll put out what i have, and see can anyone help.
    Write a program that:
    -Takes in an emplyees name,
    -Take in the ID Number
    -Take in their Wage
    -Take in their Tax Code (S or C)
        -S = Self emplyed
        -C = Company-Employed
         (if anything other than S or C pressed, ask again)
    
            Self Emplyed;
            - >27k, pay 25% tax
            - between 27k and 20k, tax = 23%
            - <20k, tax = 20%
    
            Company;
            - >29k, Tax =24%
            - Between 29k and 18k, tax = 22%
            - <18k, tax = 10%
    
    */
    

    What would be the best way of getting the S and C input? i tried a do while, but must have been messing it up bigtime as i couldn't get it to work.

    cheers :D


Comments

  • Registered Users, Registered Users 2 Posts: 7,530 ✭✭✭jmcc


    Only two possible right values. You could use a switch statement but it might be overkill.

    Regards...jmcc


  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    truedoom wrote: »
    Heya, so i have to write out a C program, but i cannot for the life of me wrap my head around a certain part. I'll put out what i have, and see can anyone help.


    What would be the best way of getting the S and C input? i tried a do while, but must have been messing it up bigtime as i couldn't get it to work.

    cheers :D

    scanf is yer man

     
    [COLOR=#339933][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]#include[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"stdio.h"[/COLOR][/SIZE][/FONT]
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2]
    [/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] main([/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000]) [/COLOR][/SIZE][/FONT]
    [SIZE=2][FONT=Consolas]{[/FONT][/SIZE]
    [/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] c;[/SIZE][/FONT]
    [/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#000000] s;[/COLOR][/SIZE][/FONT]
     
    [SIZE=2][FONT=Consolas]printf([/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"Enter a: "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]);[/SIZE][/FONT]
    [SIZE=2][FONT=Consolas]scanf([/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"%d"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], &c);[/SIZE][/FONT]
    [SIZE=2][FONT=Consolas]printf([/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"Enter s: "[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]);[/SIZE][/FONT]
    [SIZE=2][FONT=Consolas]scanf([/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"%d"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], &s);[/SIZE][/FONT]
    [SIZE=2][FONT=Consolas]printf([/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"You entered: %d %d\n"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], c,s);[/SIZE][/FONT]
     
    [/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] 0;[/SIZE][/FONT]
    [SIZE=2][FONT=Consolas]}[/FONT][/SIZE]
    [/SIZE][/FONT][/COLOR]
     
     
    


    above may not work but similar required


  • Registered Users, Registered Users 2 Posts: 58 ✭✭adigilani


    how about....

    double wagesEarned; //this will have the taxable pay, whereever you are getting it from

    char userChoice='';
    double taxRate=0.00;

    do
    {
    printf("c) if company employeed\n");
    printf("s) if self employeed\n\n");
    printf("Please choose: ");
    userChoice = getchar();
    if(userChoice=='c' || userChoice=='C')
    {
    taxRate = companyEmployeedFunction();//do your calculation on and when the tax rate will change, loop will end
    }
    else if(userChoice=='s' || userChoice=='S')
    {
    taxRate = selfEmployeedFunction(); //do your calculation on and when the tax rate will change, loop will end
    }
    else
    {
    taxRate=0.00;
    printf("not a valid input.\ntry again\n\n");
    }
    }while(taxRate==0.00);


  • Registered Users, Registered Users 2 Posts: 13 AnnCCFE


    truedoom wrote: »
    Heya, so i have to write out a C program, but i cannot for the life of me wrap my head around a certain part. I'll put out what i have, and see can anyone help.
    Write a program that:
    -Takes in an emplyees name,
    -Take in the ID Number
    -Take in their Wage
    -Take in their Tax Code (S or C)
        -S = Self emplyed
        -C = Company-Employed
         (if anything other than S or C pressed, ask again)
    
            Self Emplyed;
            - >27k, pay 25% tax
            - between 27k and 20k, tax = 23%
            - <20k, tax = 20%
    
            Company;
            - >29k, Tax =24%
            - Between 29k and 18k, tax = 22%
            - <18k, tax = 10%
    
    */
    

    What would be the best way of getting the S and C input? i tried a do while, but must have been messing it up bigtime as i couldn't get it to work.

    cheers :D

    christopher, this is a form of cheating. If the post is not taken down in the next 48 hours you will get a immediate fail.

    Thanks,
    Ann.


  • Registered Users, Registered Users 2 Posts: 58 ✭✭adigilani


    did you try

    char userChoice='';

    do
    {
    printf("c) if company employeed\n");
    printf("s) if self employeed\n\n");
    printf("Please choose: ");
    userChoice = getchar();
    if(userChoice=='c' || userChoice=='C')
    {
    break;
    }
    else if(userChoice=='s' || userChoice=='S')
    {
    break;
    }
    else
    {
    userChoice='';
    printf("not a valid input.\ntry again\n\n");
    }
    }while(userChoice=='');

    the above loop will ask for an input and check if it is c or C or s or S and if not then keep repeating the loop... and if a valid input is entered then will quit...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 400 ✭✭truedoom


    AnnCCFE wrote: »
    christopher, this is a form of cheating. If the post is not taken down in the next 48 hours you will get a immediate fail.

    Thanks,
    Ann.

    you did say we could use the internet to find the answer :P


  • Registered Users, Registered Users 2 Posts: 400 ✭✭truedoom


    adigilani wrote: »
    how about....

    double wagesEarned; //this will have the taxable pay, whereever you are getting it from

    char userChoice='';
    double taxRate=0.00;

    do
    {
    printf("c) if company employeed\n");
    printf("s) if self employeed\n\n");
    printf("Please choose: ");
    userChoice = getchar();
    if(userChoice=='c' || userChoice=='C')
    {
    taxRate = companyEmployeedFunction();//do your calculation on and when the tax rate will change, loop will end
    }
    else if(userChoice=='s' || userChoice=='S')
    {
    taxRate = selfEmployeedFunction(); //do your calculation on and when the tax rate will change, loop will end
    }
    else
    {
    taxRate=0.00;
    printf("not a valid input.\ntry again\n\n");
    }
    }while(taxRate==0.00);

    Thats a very good version of it, thanks a million :)


  • Registered Users, Registered Users 2 Posts: 400 ✭✭truedoom


    adigilani wrote: »
    did you try

    char userChoice='';

    do
    {
    printf("c) if company employeed\n");
    printf("s) if self employeed\n\n");
    printf("Please choose: ");
    userChoice = getchar();
    if(userChoice=='c' || userChoice=='C')
    {
    break;
    }
    else if(userChoice=='s' || userChoice=='S')
    {
    break;
    }
    else
    {
    userChoice='';
    printf("not a valid input.\ntry again\n\n");
    }
    }while(userChoice=='');

    the above loop will ask for an input and check if it is c or C or s or S and if not then keep repeating the loop... and if a valid input is entered then will quit...

    i like this one very much, may utilise parts of it :) thank you :D


  • Closed Accounts Posts: 32 EasyGame


    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
    
    	double userWage=0;
    	int taxRate=0;
    	char taxCode;
    	int eval = 0;
    
    	while(eval==0)
    	{
    		printf("Enter S if Self-Employed\n");
    		printf("Enter C if Company-Employed\n");
    		printf("Tax Code: ");
    		scanf("%c", &taxCode);
    
    		taxCode = tolower(taxCode);
    		switch(taxCode){
    			case 's':
    				if(userWage>=27000){
    					taxRate = 25;}
    				else if(userWage >= 20,000 && userWage <27000){
    					taxRate = 23;}
    				else{taxRate = 20;}
    				eval = 1;
    				break;
    			case 'c': 
    				if(userWage >= 29000){
    					taxRate = 24;}
    				else if(userWage >=18,000 && userWage <29000){
    					taxRate = 22;}
    				else{taxRate = 10;}
    				eval = 1;
    				break;
    			default:
    				printf("Incorrect Character Input, Please Try Again\n");
    				break;
    			}
    	}
    	printf("\nTax Code is : %s", &taxCode);
    	return 0;
    }
    
    


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Thread locked due to truedom breaking our rules and apparently breaking the rules of his course.

    E.P.


  • Advertisement
This discussion has been closed.
Advertisement