GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Oct-2009, 11:55
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Help me with the end of the coding..the calculation


Dear friends,i've done a project for my course.Its about employee salary deductions...my only problem is the coding is working but its not doing the deduction and give the balance of a salary for an employee....plz help me plz..

this is the code:
CPP / C++ / C Code:
#include <conio.h>
#include <stdio.h>
int main()
{
  
	int hour;
	int day;
	int month;
	double total;
	char name[80];
	double earn;
	int earning;
	double hospital;
	double federal;
	double total_deduct;


	
{
	printf("Enter your name:");
	scanf("%s", &name);
	printf("\n");

}

	printf("Set hours of work for the day: ");
	scanf("%d", &day);
	if(day<=8)
	{
		       printf("within normal time\n");
			   printf("\n");
	}
			   
	else if(day>=8)
	{
			   printf("You working overtime\n");
			   printf("\n");
    

	}
	printf("Set work days during the month<1-31>:");
	scanf("%d", &hour);
	if(hour<=31)
	{
               printf("you work out of normally\n");
			   printf("\n");


	}

	printf("Put the months of work during the year(1-12):");
	scanf("%d", &month);
	if (month<=9)
	{
		       printf("You have a nice job\n");
			   printf("\n");
	}

	else if(month<=10)
	{
			   printf("you have 2 months of vacation\n");
			   printf("\n");
	}

	else if(month<=11)
	{
			   printf("You have only one month off\n");
			   printf("\n");
	}
	else
	{
		       printf("you cant work more than 12 months\n");
			   printf("\n");
	}
	


	printf("Enter your earnings per hour($):");
	scanf("%d", &earn);
	printf("\n");

	
	printf("Your name: %s ", name);
	printf("\n");

	{
	double total=hour*day*month;
	printf("Total working hours within the year is: %d total\n");   //calculate total of the hour and day and month
	printf("\n");
	}

	{
	int earning=total*earn;          //total earning for the month
	}
	

	printf(" **Yearly Deduction for the Employee**\n");   // for deduction
	printf("\n");

	{
	double federal=earning*.18;
	printf("FederalHolding; %.2d $",federal);    //federal holding calculation in system
	printf("\n"); 

	}
	{
	double hospital=earning*.045;
	printf("Hospitalization:  %.2d $",hospital);    // hospitalization deduction in system
	printf("\n");
	}
	{

	double total_deduct=federal+hospital;
	printf(" Total deduction: %.2d $\n");         // total deduction of federal and hospitalization
	}
	{
	double total=earning-total_deduct;
    printf(" Total wages with deduction: %.2d $",total); // total of the result after deduction and balance of the salary
	}

	return 0;
	}

below is the example of the output...
  #2  
Old 12-Oct-2009, 12:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help me with the end of the coding..the calculation


Quote:
Originally Posted by chekers
...coding is working but its not doing the deduction and give the balance of a salary for an employee.......
Well, in my opinion, if the coding does not produce the expected output, that means it is not working.
Quote:
Originally Posted by chekers
below is the example of the output...
A quick review of your post makes me believe that not a single line of your program gives a single line of the example output

So---If you would like some help, here's the deal:
  1. Show us exactly what your program gave as the output for your run.

  2. Show us exactly what you expected to get from the program as the output.

  3. Tell us exactly what you don't understand about the difference(s).

You should also tell us what compiler you are using. Tell us whether you get any messages from the compiler. Any messages at all.

Maybe then someone can help you understand how to find and correct the many errors in the program.




Regards,

Dave
  #3  
Old 12-Oct-2009, 13:09
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Help me with the end of the coding..the calculation


Thanks alot Dave for the reply..

Ask u asked...
1)My program shows the output as below :


2)I suppose i want the output to be like this..


My system works correctly until the the output line is "Your name:"
After that it need to do the calculation...so its not doing the exact calculation at all..i know i did some mistake in coding and im very beginner....i would like to know if someone can guide or try to complete the last coding so that it will give the correct output as i want....and with that i can learn from it..as it is my final semester project ..


Im using Microsoft Visual C++ 6.0 and the file name is saved as .c

The errors that shown to me now is:
project.c
D:\Project\project.c(93) : warning C4244: 'initializing' : conversion from 'double ' to 'int ', possible loss of data
D:\Project\project.c(93) : warning C4700: local variable 'total' used without having been initialized
D:\Project\project.c(101) : warning C4700: local variable 'earning' used without having been initialized
D:\Project\project.c(113) : warning C4700: local variable 'federal' used without having been initialized
D:\Project\project.c(113) : warning C4700: local variable 'hospital' used without having been initialized
D:\Project\project.c(117) : warning C4700: local variable 'total_deduct' used without having been initialized
Linking...

project.exe - 0 error(s), 6 warning(s)
  #4  
Old 12-Oct-2009, 14:06
zalezog zalezog is offline
Junior Member
 
Join Date: Oct 2007
Posts: 33
zalezog will become famous soon enough

Re: Help me with the end of the coding..the calculation


First get rid of non standard header
CPP / C++ / C Code:
#include <conio.h>
Has absolutely no purpose.
Let's look at this line
Quote:
CPP / C++ / C Code:
double total=hour*day*month;
	printf("Total working hours within the year is: %d total\n");   //calculate total of the hour and day and month
	printf("\n");

total is already defined at the beginning,that's a redeclaration.
The datatype of total is double.

Quote:
CPP / C++ / C Code:
int hour;
	int day;
	int month;
	double total;
	char name[80];
	double earn;
	int earning;
	double hospital;
	double federal;
	double total_deduct;


Also in the next printf statement , you aren't passing any parameters to printf
and the format specifier ,since it is double should not be %d ( that's for integers)should be %lf for doubles.

Something like this (now you don't need braces)
CPP / C++ / C Code:
              total=hour*day*month;
	printf("Total working hours within the year is: %.2lf total\n", total);
Although Microsoft Visual C++ 6.0 should still produce a warning :
Code:
warning C4244: '=' : conversion from 'double ' to 'int ', possible loss of data
Since your hour, day and month are integers.

A similar argument for
CPP / C++ / C Code:
int earning=total*earn; .
.
.
double federal=earning*.18; /*there shouldn't be a double should be 
federal = ..*/
printf("FederalHolding; %.2d $",federal);    /*printf(" ....%.2lf", federal)*/
.
.
double hospital=earning*.045;
printf("Hospitalization:  %.2d $",hospital);    
.
.
double total=earning-total_deduct;
printf(" Total wages with deduction: %.2d $",total); 

What's the reason behind the opening and closing braces when you compute
earning , total ....
Quote:
CPP / C++ / C Code:
             {
	 int earning=total*earn;          //total earning for the month
	}
             .
.
{
	double total=hour*day*month;
	printf("Total working hours within the year is: %d total\n");   
                            //calculate total  of  the hour and day and month
	printf("\n");
}

Were they there to suppress compile time errors
May be this program can be done better with some helper functions.
  #5  
Old 12-Oct-2009, 14:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help me with the end of the coding..the calculation


Quote:
Originally Posted by chekers
...
My system works correctly until the the output line is "Your name:"
How do you know that it is working? I mean, how do you know that it reads the input values as you expected?

I strongly recommend that, for debugging purposes, you print out all input values as the program reads them:

Right after your statement reads the value of day print out the value.
CPP / C++ / C Code:
	scanf("%d", &day);
        printf("You entered %d\n", day);

Same for hour, month, earn, and all variables whose values you get from the user. Note that the format specifier for scanning a double is "%lf" not "%d". The format specifier for printing a double is "%f", not "%d"

Now, look at the line flagged by the first two warnings:
Code:
D:\Project\project.c(93) : warning C4244: 'initializing' : conversion from 'double ' to 'int ', possible loss of data D:\Project\project.c(93) : warning C4700: local variable 'total' used without having been initialized

Why did you create a new variable in that block named "earning" ??? You already have a variable named "earning" which is visible everywhere else in the program. By creating a new variable in the block (lines 92--94) you are "hiding" the one that you want to use inside that block, and the local variable inside that block is no longer valid after the program progresses beyond the end of that block.

So: delete the '{' and '}' lines (or comment them out) and change your assignment statement to
CPP / C++ / C Code:
        earning=total*earn;          //total earning for the month


Compile again, and at least the first warning should go away.

Now look at the second warning that you showed:
Code:
D:\Project\project.c(93) : warning C4700: local variable 'total' used without having been initialized

I'm thinking it is still there. What the heck does it mean?

Well look at some statements before your original line 93:

CPP / C++ / C Code:
        {
        double total=hour*day*month;
        printf("Total working hours within the year is: %d total\n");   //calculate total of the hour and day and month
        printf("\n");
        }
;


        //{  Your original line 92 not needed
        earning=total*earn;   // Modified line 93 so that it doesn't declare local variable again
        //}  Original line 94 not needed

By creating a block and declaring a variable, "total," inside that block you have "hidden" the real "total" variable that was declared at the beginning of the program and you assigned a value to the local variable inside the block. After the program progresses beyond that particular block, the value is lost, and the overall "total" still hasn't been assigned a value.

So, again, you can eliminate the "{" and "}" or comment them out, and do not declare a new local variable:

CPP / C++ / C Code:
	//{  Not needed
	total=hour*day*month; // Modified: Don't declare local variable
	printf("Total working hours within the year is: %d total\n");   //calculate total of the hour and day and month
	printf("\n");
        //}  Not needed

Work your way through all of the warnings. You shouldn't see any messages at all from the compiler. See Footnote.

Sometimes compilers give warnings that a knowledgeable might want to ignore. For now, your goal should be to eliminate all warnings. That won't guarantee that the program will work the way that you intended, and, in fact there are a number of problems in your code that do not cause warnings from your particular compiler, but I think that getting rid of compiler warning messages is an important first step.


Debugging code and figuring out how to use compiler messages to help you see problems is a learned skill. I mean, no one was born knowing this stuff.


Regards,

Dave

Footnote: Sometimes the compiler messages may not make sense (at least not until you have seen them about a million times), but they should make you look really hard at lines that they tell you about. Sometimes the actual problem may be somewhere in the file before the one that is flagged, so you have to look at everything.
  #6  
Old 13-Oct-2009, 10:41
chekers chekers is offline
New Member
 
Join Date: Oct 2009
Posts: 15
chekers is on a distinguished road

Re: Help me with the end of the coding..the calculation


Hello Guys....im done with the project..its working very well...

this is the coding i would like to share with u all...if got any problems,do let me know...im so satisfied now
CPP / C++ / C Code:
#include <stdio.h>
int main()
{
  
	int hour;
	int day;
	int month;
	double total;
	char name[80];
	int earn;
	double earning;
	double hospital;
	double federal;
	double housing_loan;
	double total_deduct;


	
{
	printf("Enter Your Name:");
	scanf("%s", &name);
	printf("\n");

}

	printf("Enter Your Hours Of Work For The Day: ");
	scanf("%d", &day);
	if(day<=8)
	{
		       printf("You work within normal hour\n");
			   printf("\n");
	}
			   
	else if(day>=8)
	{
			   printf("You are working overtime\n");
			   printf("\n");
    

	}
	printf("Enter Your Days Of Work During The Month<1-31>:");
	scanf("%d", &hour);
	if(hour<=31)
	{
               printf("you work out of normally\n");
			   printf("\n");


	}

	printf("Put the months of work during the year(1-12):");
	scanf("%d", &month);
	if (month<=9)
	{
		       printf("you work exactly on given month\n");
			   printf("\n");
	}

	else if(month<=10)
	{
			   printf("you have 2 months of vacation\n");
			   printf("\n");
	}

	else if(month<=11)
	{
			   printf("You have only one month off\n");
			   printf("\n");
	}
	else
	{
		       printf("you cant work more than 12 months\n");
			   printf("\n");
	}
	


	printf("Enter your earnings per hour($):");
	scanf("%d", &earn);
	printf("\n");

	
	printf("Your name: %s ", name);
	printf("\n");

	

	total=hour*day*month;
	printf("Total working hours within the year is: %.f",total);   //calculate total of the hour and day and month
    printf("\n");
		
    earning=total*earn; 
	printf("Total Earning Of Yourself is:RM %.2f",earning);   
	printf("\n");                               //total earning for the month
	


	printf(" ~~~~~~Yearly Deduction for the Employee~~~~~~~\n");   // for deduction
	printf("\n");



	

	
    federal=earning*.18;
	printf("FederalHolding;RM %.2f",federal);    //federal holding calculation in system
	printf("\n"); 
	


	hospital=earning*.045;
	printf("Hospitalization:RM %.2f",hospital);    // hospitalization deduction in system
	printf("\n");



	housing_loan =earning*.035;
	printf("Housing loan:RM %.2f",housing_loan);    // housing loan deduction in system
	printf("\n");
	
	


	total_deduct=federal+hospital+housing_loan;
	printf("Total deduction done is:RM %.2f",total_deduct);  // total deduction of federal and hospitalization
	printf("\n");                              
	


	total=earning-total_deduct;
    printf("Total salary after deduction is :RM %.2f",total); // total of the result after deduction and balance of the salary
	printf("\n"); 

	

	return 0;
	}
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding style and guideline ahbi82 MS Visual C++ / MFC Forum 0 24-Sep-2009 00:46
Run exe file in C++ coding sitha C++ Forum 4 02-Mar-2007 00:19
Coding Contest #1 davis Miscellaneous Programming Forum 0 12-Jun-2006 09:29
Pls help in this coding. harsha C++ Forum 5 08-Apr-2004 21:48
C Coding Style dsmith Miscellaneous Programming Forum 10 24-Feb-2004 07:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:32.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.