Help me with this program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • compsci
    New Member
    • Jan 2007
    • 32

    Help me with this program

    Write a program that uses functions to calculate a person’s paycheck. The program should ask for a person’s first and last name, social security number (use fakes), phone number, address, hours worked (in the week), and hourly wage. The program should also include overtime (time and a half) if they have worked more than 40 hours. The program should also take out federal taxes (8%). The program should display the results to the screen. The program should run until the user chooses to quit. Have functions to do the following.
    • Compute overtime for a person (for the given week).
    • Compute federal tax on a person’s pay.
    • Compute a person’s after-tax pay
    All input and output should be done in main. The output should include the person’s first and last name, social security number, phone number, address, federal tax, and after-tax pay.
    Turn in a design for this program at the beginning of the next lab.
    • For each function, state what its parameters are, what it returns, and how it computes the returned value from the parameter values passed to it.
    • List the steps in main, including function calls. Identify what is in the while loop. Indicate the condition for the while loop.
    You will later modify this program so that, besides displaying the results to the screen, it will also write the information to a file called paycheck.dat.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    No one is going to do your homework for you. Help us to help you by telling us what you have already done to solve this problem. Additionally if you are having problems writing your code, post a snippit of code in which you are having a problem. State what you think the problem might be and what ways you may be able to solve it. If there is a specific problem with your code that you cannot overcome, the nice folks on thescripts will help you.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Originally posted by RedSon
      No one is going to do your homework for you. Help us to help you by telling us what you have already done to solve this problem. Additionally if you are having problems writing your code, post a snippit of code in which you are having a problem. State what you think the problem might be and what ways you may be able to solve it. If there is a specific problem with your code that you cannot overcome, the nice folks on thescripts will help you.
      At least this one was clear, concise, and well formatted with proper english.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by Motoma
        At least this one was clear, concise, and well formatted with proper english.
        That is the only reason I replied in the first place. Otherwise I would have just ignored the entire message. Ugh...

        Comment

        • cyberking
          New Member
          • Jan 2007
          • 84

          #5
          There could be errors with the code.. Couldnt compile an check it!!
          Long an irritating code.. There were some ambiguities in your post, so couldnt render the entire code..

          Code:
          #include<stdio.h>
          #include<conio.h>
          #define F_TAX 0.08
          
          int overtime(int);
          int federal(int);
          
          void main()
          {
           char f_name[20],ssn[20],addr[100],ch;
           int phone_no,hours,hr_wage;
           int overtime = 40,extra;
          
           printf("Enter your:\n");
           printf("First name");
           gets(f_name);
           printf("\nsocial security no:");
           gets(ssn);
           printf("\naddress");
           scanf("%s",addr);
           printf("\nPhone no:");
           scanf("%d",phone_no);
           printf("\nHOurs u have worked:");
           scanf("\nhours");
          
           printf("Press a key to continue:") ;
           ch = getch();
          
           while(ch)
          {
            clrscr();
            break;
          }
          
           printf("The details u have entered are:");
           printf("%s \t %s \t %s \t %d \t %d",f_name,ssn,addr,phone_no,hours);
           // This is gettin to be a long program.. So i ve used tabs!! 
           // Change the code the way u want your output to be displayed
          
           etra= overtime(hours) ;
           printf("\nOvertime = %d"extra);
           
          getch();
          }
          
          int overtime(hours)
           {
            int extra;
            if(hours>40) 
              extra = hours - 40;
              return extra; 
           else
             printf("You have not worked for extra hours!!");
            }
          
          int federal(pay)
           {
            // Calculations up to u.. I am not sure how tax is accounted for!
            // Return values accordingly. The second and the third functions u have
            // specified are more or less the same.. they can be implemented right her in
            // this function itself!
           } }
          Last edited by horace1; Jan 27 '07, 11:29 AM. Reason: added code tags

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            now compiles, see if it does what you require
            Code:
            #include<stdio.h>
            #include<conio.h>
            #define F_TAX 0.08
            
            int overtime(int);
            int federal(int);
            
            int main()
            {
             char f_name[20],ssn[20],addr[100],ch;
             int phone_no,hours,hr_wage;
             int overtime = 40,extra;
            
             printf("Enter your:\n");
             printf("First name");
             gets(f_name);
             printf("\nsocial security no:");
             gets(ssn);
             printf("\naddress");
             scanf("%s",addr);
             printf("\nPhone no:");
             scanf("%d",phone_no);
             printf("\nHOurs u have worked:");
             scanf("\nhours");
            
             printf("Press a key to continue:") ;
             ch = getch();
            
             while(ch)
            {
              clrscr();
              break;
            }
            
             printf("The details u have entered are:");
             printf("%s \t %s \t %s \t %d \t %d",f_name,ssn,addr,phone_no,hours);
             // This is gettin to be a long program.. So i ve used tabs!! 
             // Change the code the way u want your output to be displayed
            
             extra= overtime *hours ;  // ** changed expression
             printf("\nOvertime = %d", extra); // ** added ,
             
            getch();
            }
            
            int overtime(hours)
             {
              int extra;
              if(hours>40) 
                {       // ** added {
                extra = hours - 40;
                return extra; 
            }  // ** added
             else
               printf("You have not worked for extra hours!!");
              }
            
            int federal(pay)
             {
              // Calculations up to u.. I am not sure how tax is accounted for!
              // Return values accordingly. The second and the third functions u have
              // specified are more or less the same.. they can be implemented right her in
              // this function itself!
             }

            Comment

            • cyberking
              New Member
              • Jan 2007
              • 84

              #7
              Thanks buddy for doin the corrections required.. Its just that I was too bored to compile it and see, thanks anyways!

              Comment

              • compsci
                New Member
                • Jan 2007
                • 32

                #8
                Im a beginner C++ programmer and I don't quite understand the code and how the program works. Please help me understand.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Originally posted by compsci
                  Im a beginner C++ programmer and I don't quite understand the code and how the program works. Please help me understand.
                  Which parts do you not understand?

                  Comment

                  • cyberking
                    New Member
                    • Jan 2007
                    • 84

                    #10
                    Program followed by explanation

                    Here s the new program. this is important !! Refer to this as u read my explanation in the next post. This is simplified compared to the previous script. That should help u!!

                    #include<stdio. h>
                    #include<conio. h>
                    #define F_TAX 0.08

                    int overtime(int hours)
                    {
                    int extra;
                    if(hours>40)
                    extra = hours - 40;
                    return extra;
                    else
                    printf("You have not worked for extra hours!!");
                    }

                    int federal(int pay)
                    {
                    // Calculations up to u.. I am not sure how tax is accounted for!
                    // Return values accordingly. The second and the third functions u have
                    // specified are more or less the same.. they can be implemented right her in
                    // this function itself!
                    }

                    void main()
                    {
                    char f_name[20],ssn[20],addr[100],ch;
                    int phone_no,hours, hr_wage;
                    int overtime = 40,extra;

                    printf("Enter your:\n");
                    printf("First name:");
                    scanf("%s",f_na me);
                    printf("\nsocia l security no:");
                    gets(ssn);// use scanf() as above!
                    printf("\naddre ss");
                    scanf("%s",addr );
                    printf("\nPhone no:");
                    scanf("%d",phon e_no);
                    printf("\nHOurs u have worked:");
                    scanf("\nhours" );

                    clrscr(); // clears screen

                    printf("The details u have entered are:");
                    printf("%s \t %s \t %s \t %d \t %d",f_name,ssn, addr,phone_no,h ours);
                    // This is gettin to be a long program.. So i ve used tabs!! \t provides a tab space
                    // Change the code the way u want your output to be displayed

                    etra= overtime(hours) ;
                    printf("\nOvert ime = %d"extra);

                    getch();
                    }
                    Last edited by cyberking; Jan 27 '07, 08:30 PM. Reason: Hmmm.. Dont know

                    Comment

                    • cyberking
                      New Member
                      • Jan 2007
                      • 84

                      #11
                      Hi.
                      Let me try explaining to you in the most simplest way. Hope you understand it. Infact I shall make you understand it!
                      Firstly, before you write any c or c++ program, most of the times you would be making use of predefined functions
                      all included in wat is referred to as Header files. eg. stdio.h and conio.h.
                      The extension .h indicates that the file is a header file and these header files can be included in the program you write
                      by using the #include directvie.

                      So the first two statements, #include<stdio. h> and #include<conio. h> include header files in your program. stdio.h contains the
                      fnction printf and scanf() definitions ad conio.h contains function definitions for getch();
                      Without includeing these header files, u would have compilation errors..

                      Next, check out the #define F_TAX = 0.08 statement. This statement makes the value of F_TAX available throughout the program.
                      You can also define this as a variable inside the main() loop as

                      int F_TAX = 0.08;

                      int suggests the datatype, indicates that the variable F_TAX shall hold integer value.

                      Similarly

                      char var_name;// indicates that the variable var_name shall hold a character.
                      eg. char ch; // refer program

                      char var_name[n]; // indicates that the var_name variable shall hold a string of length n; n = 1,2,3....
                      eg. char addr[100]; // refer program

                      next to display a string on the console, we use printf() function.

                      syntax: printf(" data to be displayed comes within quotes ");

                      Now if u have to print a value to the screen then,

                      printf("the integer value is: %d", var_name);
                      eg; printf("Your phone no is: %d", phone_no); // Prints the value in phone_no variable

                      To scan a value, that is to get user input and store it in a variable, use the function scanf()

                      syntax: scanf("%d", &phnoe_no); // would store the value inputted by user in the phone_no variable.
                      For now dont bother about the gets(). Use scanf everywhere and should you have to input a string and store it in
                      a variable, replace %d by %u and do not include the & character before the variable name as u did to store an integer value in the above example. this
                      is important!!!

                      Should you have to print a string of characters on the console, replace %d by %u! Simple isnt it??

                      By now I guess you can understand the main() function. next comes the functions we have written. I have written the program in a simpler method to help
                      you understand better. For now, write all functions before main(). See the new program.

                      synatx of a function:


                      return_data_typ e function_name(p arameter_data_t ype parameter_name)
                      {
                      //function body
                      }

                      SImple, ain't it?

                      return type indicates a value returned to the main(). So in main(), you should have a variable to hold the returned value.

                      oofff.. This is tiring. Anyways, I ll hurry up a bit!

                      consider as an example:

                      int overtime(int hours) // refer new program
                      {
                      int extra; // A variable inside and available only for this function
                      if(hours>40)// Check if you have worked for more than 40hrs, if yes execute for loop else donot execute for loop
                      {
                      extra = hours - 40; // Calculate how many extra hours, say for eg, i ve worke 100 hrs, then extra will have 60!!
                      return extra;// send value 60 in extra to main() function
                      }
                      else
                      printf("You havent worked extra hours");

                      }

                      To hold the returned value, I have a variable named new_var in main function, print the value in new_var..

                      Calling a function from wihin main is simple. In order to call overtime function,

                      new_var = overtime(hours) ;

                      new_var holds returned value and the parameter i am passing to overtime function is hours.

                      Wow!!! I am tired, guess u too would be tired reading this. I ve tried my best to help u understand. Let me know if this helped u.

                      regards
                      Cyberking..

                      Comment

                      • compsci
                        New Member
                        • Jan 2007
                        • 32

                        #12
                        Yes thanx. I got a better understanding

                        Comment

                        Working...