C++ Noob Easy Program

Where do I go from here?

Page 1 of 1

10 Replies - 1207 Views - Last Post: 14 June 2009 - 01:42 AM Rate Topic: -----

#1 C++NooB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-June 09

C++ Noob Easy Program

Post icon  Posted 13 June 2009 - 11:44 AM

[C++ Code] [Basic payroll program]

Hello, can you please tell me the proper way to use if statements. Here is what I got so far and just need to know how to properly set the rest of the code up. All I got to do is enter the hours worked and calculate the total including overtime at 1.5 times pay rate?


#include <iostream>
using namespace std;

int main()
{
// declare variables
double hours;
double wages;
double ot;
double payrate;

//Enter hours worked
cout << "Enter the number of hours the employee worked" ;

cin >> hours;

// Ask user to enter the pay rate
cout << "Enter the pay rate of the employee:";

cin >> wages;

//Calculate the wages

if (hours > 40 = payrate 1.5)





return 0;
}

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Noob Easy Program

#2 Masterchief117.arbiter  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 159
  • Joined: 27-March 09

Re: C++ Noob Easy Program

Posted 13 June 2009 - 11:54 AM

View PostC++NooB, on 13 Jun, 2009 - 10:44 AM, said:

[C++ Code] [Basic payroll program]

Hello, can you please tell me the proper way to use if statements. Here is what I got so far and just need to know how to properly set the rest of the code up. All I got to do is enter the hours worked and calculate the total including overtime at 1.5 times pay rate?


#include <iostream>
using namespace std;

int main()
{
// declare variables
	double hours;
	double wages;
	double ot;
	double payrate;

//Enter hours worked
cout << "Enter the number of hours the employee worked";

cin >> hours;

// Ask user to enter the pay rate
cout << "Enter the pay rate of the employee:";

cin >> wages;

//Calculate the wages

if (hours > 40 = payrate 1.5)

		
	
	
	
	return 0;
}



first off it needs to look like this
if(hours>40){

payrate=1.5;//make sure you declare payrate like this int payrate; underneath main

} 
return 0; 
}


try that

dont forget to post your code like this ([code])
([ /code ])

This post has been edited by Masterchief117.arbiter: 13 June 2009 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 UnknownFury  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 17
  • Joined: 09-March 09

Re: C++ Noob Easy Program

Posted 13 June 2009 - 12:07 PM

View PostMasterchief117.arbiter, on 13 Jun, 2009 - 10:54 AM, said:

first off it needs to look like this
if(hours>40){

payrate=1.5;//make sure you declare payrate like this int payrate; underneath main

} 
return 0; 
}



He does declare payrate, and it should be float or double; not int as he's setting it to 1.5.

An if statement has to be in the form

if (<boolean expression>) {
<statement(s)>
}


Your code you're trying to stick it all the brackets together hours>40 is your boolean expression (will have the value of either true or false) and payrate = 1.5 is your statement.

This post has been edited by UnknownFury: 13 June 2009 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#4 C++NooB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-June 09

Re: C++ Noob Easy Program

Posted 13 June 2009 - 12:14 PM

View PostUnknownFury, on 13 Jun, 2009 - 11:07 AM, said:

View PostMasterchief117.arbiter, on 13 Jun, 2009 - 10:54 AM, said:

first off it needs to look like this
if(hours>40){

payrate=1.5;//make sure you declare payrate like this int payrate; underneath main

} 
return 0; 
}



He does declare payrate, and it should be float or double; not int as he's setting it to 1.5.

An if statement has to be in the form

if (<boolean expression>) {
<statement(s)>
}


Your code you're trying to stick it all the brackets together hours>40 is your boolean expression (will have the value of either true or false) and payrate = 1.5 is your statement.


Thank you guys for helping, wow I never thought C++ would be so in depth. You really gotta know your stuff to write these programs!
Was This Post Helpful? 0
  • +
  • -

#5 Teumessian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 25-February 09

Re: C++ Noob Easy Program

Posted 13 June 2009 - 01:24 PM

 #include <iostream>
using namespace std;

int main()
{
// declare variables
double hours;
double wages;
double ot;
double pay;
double totalPay;

//Enter hours worked
cout << "Enter the number of hours the employee worked:";

cin >> hours;

// Ask user to enter the pay rate
cout << "Enter the pay rate of the employee per hour:";

cin >> wages;

//Calculate the wages

if (hours > 40)
	{
	ot = ( (hours-40) * (wages*1.5) );
	pay = (40 * wages);
	totalPay = ot + pay;
	}

else
	{
	totalPay = (hours * wages);
	}
return 0;
}


I think, didn't test it.
Was This Post Helpful? 0
  • +
  • -

#6 C++NooB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-June 09

Re: C++ Noob Easy Program

Posted 13 June 2009 - 01:44 PM

View PostTeumessian, on 13 Jun, 2009 - 12:24 PM, said:

 #include <iostream>
using namespace std;

int main()
{
// declare variables
double hours;
double wages;
double ot;
double pay;
double totalPay;

//Enter hours worked
cout << "Enter the number of hours the employee worked:";

cin >> hours;

// Ask user to enter the pay rate
cout << "Enter the pay rate of the employee per hour:";

cin >> wages;

//Calculate the wages

if (hours > 40)
	{
	ot = ( (hours-40) * (wages*1.5) );
	pay = (40 * wages);
	totalPay = ot + pay;
	}

else
	{
	totalPay = (hours * wages);
	}
return 0;
}


I think, didn't test it.


It complies just great! However, it does not display the total? I cant seem to figure out why though? It should be displaying the total wages..hmmm.. otherwise there are no errors at all
Was This Post Helpful? 0
  • +
  • -

#7 Teumessian  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 25-February 09

Re: C++ Noob Easy Program

Posted 13 June 2009 - 01:50 PM

Again I didn't compile it so i'm just guessing it works.
 #include <iostream>
using namespace std;

int main()
{
// declare variables
double hours;
double wages;
double ot;
double pay;
double totalPay;

//Enter hours worked
cout << "Enter the number of hours the employee worked:";

cin >> hours;

// Ask user to enter the pay rate
cout << "\n" << "Enter the pay rate of the employee per hour:";

cin >> wages;

//Calculate the wages

if (hours > 40)
	{
	ot = ( (hours-40) * (wages*1.5) );
	pay = (40 * wages);
	totalPay = ot + pay;
	}

else
	{
	totalPay = (hours * wages);
	}
cout << "\n" << "Total Pay:" << totalPay;
return 0;
}

Fixed, sorry I assumed you only needed help with the numbers.
EDIT: Made the output neater.

This post has been edited by Teumessian: 13 June 2009 - 01:55 PM

Was This Post Helpful? 1
  • +
  • -

#8 C++NooB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-June 09

Re: C++ Noob Easy Program

Posted 13 June 2009 - 01:52 PM

WOW thanks so much!!! I really appreciate it a lot!
Was This Post Helpful? 0
  • +
  • -

#9 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: C++ Noob Easy Program

Posted 13 June 2009 - 01:52 PM

just add this,


    cout << " total = ";
    cout << fixed << totalPay;




by the way, fixed keyword fixes the mathematical expression of the number .. it's useful for huge numbers and fractions

This post has been edited by Plus: 13 June 2009 - 02:02 PM

Was This Post Helpful? 1
  • +
  • -

#10 'L#'  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-June 09

Re: C++ Noob Easy Program

Posted 14 June 2009 - 01:19 AM

I have seen a similar example in another thread, where are you getting this example from?
Was This Post Helpful? 0
  • +
  • -

#11 fastman007  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 25
  • Joined: 12-April 09

Re: C++ Noob Easy Program

Posted 14 June 2009 - 01:42 AM

all problem solved already!!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1