5 Replies - 296 Views - Last Post: 18 March 2012 - 06:00 PM Rate Topic: -----

#1 azzclown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 16-March 12

Help with easy code.

Posted 16 March 2012 - 09:42 PM

Hello everyone im new here. Im taking a computer science Bachelor degree in Computer Science for a year now so i think you'll see lot of me here. Right now im taking some classes and both of them being java and c++ related. This question is very simple im sure, i just cant figure it out. I got 2 variables and i want to do something after a variable is exceeded. How i do that? This is what i have so far:

if (ht <= 40)
	{
		sueldo= ht * ph;
			cout<<"Su sueldo es de:"<<sueldo<<endl;
	}
	else
	{
		sueldo= 
			cout<<"Su sueldo es de:"<<sueldo<<endl;
	}


Basically what i want to do is after 40 is exceeded the worker is going to be paid double.
Sueldo= Salary.
This is the first equation = hours worked * payment per hour.

Any help is appreciated, thanks!

This post has been edited by Atli: 16 March 2012 - 09:53 PM
Reason for edit:: Added [code]...[/code] tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Help with easy code.

#2 azzclown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 16-March 12

Re: Help with easy code.

Posted 16 March 2012 - 09:48 PM

Cant find the post settind/edit button maybe i can get help with that too :).
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Help with easy code.

Posted 16 March 2012 - 09:58 PM

Thread moved to the C and C++ forum.

View Postazzclown, on 17 March 2012 - 04:48 AM, said:

Cant find the post settind/edit button maybe i can get help with that too :).

Only members with more than 15 posts can edit their posts, so it won't be there for you yet. (See this)
Was This Post Helpful? 0
  • +
  • -

#4 azzclown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 16-March 12

Re: Help with easy code.

Posted 16 March 2012 - 10:44 PM

i did it! I tried this in collage actually and could not get it either, still learning. Anyway here i leave it in case someone has the same question can get help from here.
#include <iostream>
using namespace std;
void main()

{
	//declarar variables

	double ph;
	double ht;
	double sueldo;//salary
	double phE;//payment per hour extra
	double htE;//hours extra
	double sueldoExtra;//extra salary
	
	//interaccion con usuario(interaction)
	cout<<"Inserte Pago por hora:"<<endl;
	cin>>ph;

	cout<<"Inserte Horas trabajadas:"<<endl;
	cin>>ht;



	//para dar resultado (Results)

	if (ht <= 40)
	{
		sueldo= ht * ph;
			cout<<"Su sueldo es de:"<<sueldo<<endl;
	}
	else
	{
		sueldo = 40 * ph;//25
		phE = ph * 2;//5+5= 10
		htE= ht - 40;//6= 1
		sueldoExtra=  htE * phE + sueldo;
			cout<<"Su sueldo es de:"<<sueldoExtra<<endl;
	}	
	

	system("pause");

}




Okay so i did 3 new variables (phe,hte and sueldoExtra). The normal salary equation before exceding the hours called sueldo will now be replaced with //sueldo= (hours before exceeding) * payment per hour. phE the new variable will be doubled by multiplying by 2. htE will substract the hours before exceeding with the hours imputed by users. Then you just use the equation that is in sueldoExtra variable.

This post has been edited by Atli: 17 March 2012 - 01:28 AM
Reason for edit:: Fixed the [code] ... [/code] tags.

Was This Post Helpful? 0
  • +
  • -

#5 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Help with easy code.

Posted 17 March 2012 - 03:17 AM

Glad you made it work :)
But here's what I noticed in your code:
sueldoExtra=  htE * phE + sueldo;

While this works for this particular program, the logic is a bit off here. sueldoExtra is the amount of extra salary based on the variable name, but here you are assigning it the total salary amount, which is not what the name suggests. I'm not trying to be harsh with your code but imagine a larger source code. You should always name the variables after what they really mean to you, in order to improve the code readability.

So, I suggest changing this part of program to something like this:
else {
    sueldo = 40 * ph;//25
    phE = ph * 2;//5+5= 10
    htE= ht - 40;//6= 1
    sueldoExtra=  htE * phE;
    cout<<"Su sueldo es de:"<<sueldo + sueldoExtra<<endl;
    /* You can even print out a bit more detailed information here; something like:
    cout<<"Extra Hours Worked: "<<htE<<endl;
    cout<<"Salary: "<<sueldo<<endl;
    cout<<"Extra Salary: "<<sueldoEx<<endl;
    */
}


Another thing you can do is to write a function which does the calculation and returns the amount of salary. Look:
double calculate_total_salary(const double& perhour, const double& hours, const double& experhour, const double& max) {
    double salary = 0;
    if(hours <= max) {
        salary = perhour * hours;
    } else {
        salary = perhour*max + experhour*(hours-max);
    }
    return salary;
}


Now, in your main function you can do this:
int main() {
    double payment_perhour;
    double hours;

    cout<<"Insert Payment Per Hour:"<<endl;
    cin>>payment_perhour;
    cout<<"Insert Hours Worked:"<<endl;
    cin>>hours;
    cout<<calculate_total_salary(payment_perhour, hours, payment_perhour*2, 40)<<endl;
    /*Suppose you want to calculate for another set of data, no problem!
    cout<<calculate_total_salary(12, 35, 20, 40)<<endl;
    */
    return 0;
}


This function can be easily called whenever you want it. So you don't have to write the calculations again. I call this an improvement ;)
Was This Post Helpful? 1
  • +
  • -

#6 azzclown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 16-March 12

Re: Help with easy code.

Posted 18 March 2012 - 06:00 PM

Thank you! Your tips are appreciated a lot. Notes have been taken.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1