7 Replies - 1086 Views - Last Post: 14 August 2009 - 01:20 AM Rate Topic: -----

#1 Suigetsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-August 09

if & else if condition

Post icon  Posted 12 August 2009 - 04:12 AM

Topic Description says i need to make a program that will compute the payroll slip of a certain employee.
Conditions: if (gross>=8000, bonus will be 1000),(gross>=5000,bonus=750),( gross>=3000,bonus=500),else bonus= 0.
Same goes for tax but instead if(gross>=7000,tax=15%),(gross>=4000,tax=10%),(gross=2000,tax=5%),else tax=0.(Medicare=100),(SSS=gross*10%,)(TotalDeduction=tax+SSS+Medicare),(Net Amount=gross+bonus-total Deduction).

Here's my Code:
#include<iostream>
#include <string>

using namespace std;

int main ()

{	
	string a,a1,b;
	int r1=500,r2=400,r3=300,b1=1000,b2=750,b3=500;
	float g1,g2,g3,t1,t2,t3,td1,td2,td3,s1,s2,s3,n1,n2,n3,c;
	cout<<"\t\tPositions: "<<endl
		<<"(M)anager"<<"\t(S)upervisor"<<"\t(E)mployee"<<endl<<endl;
	cout<<"Enter Your Name: ";
	cin>>a>>a1;
	cout<<"Choose your Rate: ";
	cin>>b;
	cout<<"Enter your Working Hours: ";
	cin>>c;
	g1=r1*c;
	g2=r2*c;
	g3=r3*c;
	t1=g1*.15;
	t2=g2*.1;
	t3=g3*.05;
	s1=g1*.1;
	s2=g2*.1;
	s3=g3*.1;
	td1=t1+s1+100;
	td2=t2+s2+100;
	td3=t3+s3+100;
	n1=(g1+b1)-td1;
	n2=(g2+b2)-td2;
	n3=(g3+b3)-td3;
	
	if ((b=="M")||("m"==b))
	{
	cout<<"\n\nName: "<<a<<" "<<a1<<endl
		<<"Position: Manager"<<endl<<endl
		<<"Rate: PHP 500.00"<<"\t\tDeductions:"<<endl
		<<"Working Hours: "<<c<<"\t\tMedicare: PHP 100.00"<<endl
		<<"Gross: "<<g1<<"\t\t\tSSS: PHP "<<s1<<endl;
	
	
	}
	else if((b=="S")||("s"==b))
	{
		cout<<"Name: "<<a<<" "<<a1<<endl
		<<"Position: Supervisor"<<endl<<endl
		<<"Rate: PHP 400.00"<<"\t\tDeductions:"<<endl
		<<"Working Hours: "<<c<<"\t\tMedicare: PHP 100.00"<<endl
		<<"Gross: "<<g2<<"\t\t\tSSS: PHP "<<s2<<endl;
	
		
	}
	else if((b=="E")||("e"==b))
	{
		cout<<"Name: "<<a<<" "<<a1<<endl
		<<"Position: Employee"<<endl<<endl
		<<"Rate: PHP 300.00"<<"\t\tDeductions:"<<endl
		<<"Working Hours: "<<c<<"\t\tMedicare: PHP 100.00"<<endl
		<<"Gross: "<<g3<<"\t\t\tSSS: PHP "<<s3<<endl;
	}
	else{
		cout<<"Invalid Input!!"<<endl
			<<"Please Try Again!!"<<endl;
	} 
	if(g1>=8000)//This is the part where i got a wrong results & if i enter a wrong input & enter a working hours this statement still executes, while the upper part executes the else statement
	{
		cout<<"Bonus: PHP "<<b1<<"\t\t\tTax: PHP "<<t1<<endl
			<<"\t\t\t\tTotal Deduction: PHP "<<td1<<endl
			<<"Net Amount: PHP "<<n1<<endl;
	}
	else if(g2>=5000)
	{
		cout<<"Bonus: PHP "<<b2<<"\t\t\tTax: PHP "<<t2<<endl
			<<"\t\t\t\tTotal Deduction: PHP "<<td2<<endl
			<<"Net Amount: PHP "<<n2<<endl;
	}
	else if(g3>=3000)
	{
		cout<<"Bonus: PHP "<<b3<<"\t\t\tTax: PHP "<<t3<<endl
			<<"\t\t\t\tTotal Deduction: PHP "<<td3<<endl
			<<"Net Amount: PHP "<<n3<<endl;
	}
	
	
	else{
		cout<<"Invalid Input!!"<<endl
			<<"Please Try Again!!"<<endl;
	}
	return 0;
}		
	


i don't know how to paste the out put of this so if you want to see it just copy-paste my code and run it..For the replies thanks in advance..

Is This A Good Question/Topic? 0
  • +

Replies To: if & else if condition

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: if & else if condition

Posted 12 August 2009 - 04:21 AM

What problem are you having with your program?
Was This Post Helpful? 0
  • +
  • -

#3 Suigetsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-August 09

Re: if & else if condition

Posted 12 August 2009 - 04:58 AM

View PostseeP+, on 12 Aug, 2009 - 03:21 AM, said:

What problem are you having with your program?

the problem i think is in this part of my code:
 if(g1>=8000)//This is the part where i got a wrong results & if i enter a wrong input & enter a working hours this statement still executes, while the upper part executes the else statement
	{
		cout<<"Bonus: PHP "<<b1<<"\t\t\tTax: PHP "<<t1<<endl
			<<"\t\t\t\tTotal Deduction: PHP "<<td1<<endl
			<<"Net Amount: PHP "<<n1<<endl;
	}
	else if(g2>=5000)
	{
		cout<<"Bonus: PHP "<<b2<<"\t\t\tTax: PHP "<<t2<<endl
			<<"\t\t\t\tTotal Deduction: PHP "<<td2<<endl
			<<"Net Amount: PHP "<<n2<<endl;
	}
	else if(g3>=3000)
	{
		cout<<"Bonus: PHP "<<b3<<"\t\t\tTax: PHP "<<t3<<endl
			<<"\t\t\t\tTotal Deduction: PHP "<<td3<<endl
			<<"Net Amount: PHP "<<n3<<endl;
	}
   
   
	else{
		cout<<"Invalid Input!!"<<endl
			<<"Please Try Again!!"<<endl;
	}
	return 0;
}		

the Bonus,Tax,TD & Net amount gives me a wrong result
Try running my program and input any positions that is listed and you will see what I'm talking about ..<<<Sorry if this may sound rude .
Was This Post Helpful? 0
  • +
  • -

#4 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: if & else if condition

Posted 12 August 2009 - 05:51 AM

if(g1>=8000)//This is the part where i got a wrong results & if i enter a wrong input & enter a working hours this statement still executes, while the upper part executes the else statement
	{
		cout<<"Bonus: PHP "<<b1<<"\t\t\tTax: PHP "<<t1<<endl
			<<"\t\t\t\tTotal Deduction: PHP "<<td1<<endl
			<<"Net Amount: PHP "<<n1<<endl;


This code still executes because all that this "if" statement needs is for g1 to be greater than 8000. No other condition needs to be met. If you enter 40 hours it will execute, but if your enter 1 hour it will not. The program will end with your INVALID PLEASE TRY AGAIN output. Add another condition for the "if" statement

This post has been edited by seeP+: 12 August 2009 - 05:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: if & else if condition

Posted 12 August 2009 - 05:52 AM

Um change all your if else statements to if statements. Or check that you are comparing the right variables. You have
if (g1 >= 8000)
else if ( g2 >= 5000)
else if ( g3 >= 3000)
else



Now whats wrong with this ? Well in order for it to execute correctly you need to compare the same variables... Meaning it should either be all of either g1, g2 or g3. Such as
if (g1 >= 8000)
else if ( g1 >= 5000)
else if ( g1 >= 3000)
else



Notice the difference ?
Was This Post Helpful? 0
  • +
  • -

#6 Suigetsu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 08-August 09

Re: if & else if condition

Posted 12 August 2009 - 10:38 PM

View PostseeP+, on 12 Aug, 2009 - 04:51 AM, said:

if(g1>=8000)//This is the part where i got a wrong results & if i enter a wrong input & enter a working hours this statement still executes, while the upper part executes the else statement
	{
		cout<<"Bonus: PHP "<<b1<<"\t\t\tTax: PHP "<<t1<<endl
			<<"\t\t\t\tTotal Deduction: PHP "<<td1<<endl
			<<"Net Amount: PHP "<<n1<<endl;


This code still executes because all that this "if" statement needs is for g1 to be greater than 8000. No other condition needs to be met. If you enter 40 hours it will execute, but if your enter 1 hour it will not. The program will end with your INVALID PLEASE TRY AGAIN output. Add another condition for the "if" statement



View Postbbq, on 12 Aug, 2009 - 04:52 AM, said:

Um change all your if else statements to if statements. Or check that you are comparing the right variables. You have
if (g1 >= 8000)
else if ( g2 >= 5000)
else if ( g3 >= 3000)
else



Now whats wrong with this ? Well in order for it to execute correctly you need to compare the same variables... Meaning it should either be all of either g1, g2 or g3. Such as
if (g1 >= 8000)
else if ( g1 >= 5000)
else if ( g1 >= 3000)
else



Notice the difference ?

i think i get what your pointing out i tried doing it but it gives me this result: Bonus: 750 Bonus 500 Bonus 500. Anyway thanks for the help damn my head hurts..The bad news is we are going to do it tomorrow T_T I'm dead.
Was This Post Helpful? 0
  • +
  • -

#7 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: if & else if condition

Posted 13 August 2009 - 05:28 AM

How does your current code look like?
Was This Post Helpful? 0
  • +
  • -

#8 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,619
  • Joined: 15-May 08

Re: if & else if condition

Posted 14 August 2009 - 01:20 AM

Yea post your updated code and i will try giving you a more direct nudge in the right direction =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1