multiplying 2 variables in C++ ...

Status
Not open for further replies.

KAMAZON

Golden Member
Apr 4, 2001
1,300
0
76
www.alirazeghi.com
I know this shouldn't be so hard but I'm having a hard time just getting 2 DOUBLE variables to multiply! I don't know what I'm doing wrong, and yes, it's for a school project... but getting 2 double variables to multiply isn't the point of the project. Any suggestions? Thanks!

The problem is at the end of the code.




#include <iostream>
#include <string>
using namespace std;


const double fedWithholdingRate = 0.18;
const double stateWhithholdingRate=0.045;
const double HospDed = 25.65;
const double UnionDues = 0.02;


string employee;
double
hoursWorked = 0,
hourlyWage = 0,
totalWage = 0,
fedWitholdingAmt = 0,
stateWithholdingAmt = 0,
totDeductions = 0,
netPay = 0;

int main()
{


//Gather the employee intiials, hours worked, and hourly rate
cout << "Welcome to the Employee Payment Calculator. Please enter the requested information /n" ;
cout << "Please input employee initials: " ;
cin >> employee; cout << endl;
cout << "Please input the hours worked: ";
cin >> hoursWorked; cout << endl;
cout << "Please enter the hourly wage: ";
cin >> hourlyWage; cout << endl;

//Multiply hours worked and hourly wage to get totalWage.
Why doesn't THIS part work?? ---> hourlyWage * hoursWorked = totalWage;
cout << "total wage = " << hoursWorked * hourlyWage << endl;
cin >> hoursWorked * hourlyWage = totalWage;
cout << "internal total wage is " << totalWage;


return 0;
}
 

WildW

Senior member
Oct 3, 2008
984
20
81
evilpicard.com
When assigning a value to a variable, the variable you're assigning to needs to be on the left side of the assignment operator.

Which is to say, your stuff is the wrong sides of the equals. You want:

totalWage = hourlyWage * hoursWorked;
 

sao123

Lifer
May 27, 2002
12,653
205
106
I know this shouldn't be so hard but I'm having a hard time just getting 2 DOUBLE variables to multiply! I don't know what I'm doing wrong, and yes, it's for a school project... but getting 2 double variables to multiply isn't the point of the project. Any suggestions? Thanks!

The problem is at the end of the code.




#include <iostream>
#include <string>
using namespace std;


const double fedWithholdingRate = 0.18;
const double stateWhithholdingRate=0.045;
const double HospDed = 25.65;
const double UnionDues = 0.02;


string employee;
double
hoursWorked = 0,
hourlyWage = 0,
totalWage = 0,
fedWitholdingAmt = 0,
stateWithholdingAmt = 0,
totDeductions = 0,
netPay = 0;

int main()
{


//Gather the employee intiials, hours worked, and hourly rate
cout << "Welcome to the Employee Payment Calculator. Please enter the requested information /n" ;
cout << "Please input employee initials: " ;
cin >> employee; cout << endl;
cout << "Please input the hours worked: ";
cin >> hoursWorked; cout << endl;
cout << "Please enter the hourly wage: ";
cin >> hourlyWage; cout << endl;

//Multiply hours worked and hourly wage to get totalWage.
Why doesn't THIS part work?? ---> hourlyWage * hoursWorked = totalWage;
cout << "total wage = " << hoursWorked * hourlyWage << endl;
cin >> hoursWorked * hourlyWage = totalWage;
cout << "internal total wage is " << totalWage;


return 0;
}


the assignment operator (as its called) must have the assignee on the left and the assignment on the right.

totalWage = hourlyWage * hoursWorked; is the correct syntax.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
I know this shouldn't be so hard but I'm having a hard time just getting 2 DOUBLE variables to multiply! I don't know what I'm doing wrong, and yes, it's for a school project... but getting 2 double variables to multiply isn't the point of the project. Any suggestions? Thanks!

The problem is at the end of the code.



Code:
#include <iostream>
#include <string>
using namespace std;


	const double fedWithholdingRate = 0.18;
	const double stateWhithholdingRate=0.045;
	const double HospDed = 25.65;
	const double UnionDues = 0.02;

	
	string employee;
	double 
		hoursWorked = 0, 
		hourlyWage = 0, 
		totalWage = 0, 
		fedWitholdingAmt = 0,
		stateWithholdingAmt = 0, 
		totDeductions = 0, 
		netPay = 0;

	int main()
{


//Gather the employee intiials, hours worked, and hourly rate
    cout << "Welcome to the Employee Payment Calculator.  Please enter the requested information /n" ;
	cout << "Please input employee initials: " ;
	cin >> employee; cout << endl;
	cout << "Please input the hours worked: ";
	cin >> hoursWorked; cout << endl;
	cout << "Please enter the hourly wage: ";
	cin >> hourlyWage; cout << endl;

//Multiply hours worked and hourly wage to get totalWage.
Why doesn't THIS part work?? --->	hourlyWage * hoursWorked = totalWage;
	cout << "total wage = " << hoursWorked * hourlyWage << endl;
	cin >> hoursWorked * hourlyWage = totalWage; 
	cout << "internal total wage is " << totalWage;


    return 0;
}

Code blocks, use 'em, We fought long and hard to get them on these forums :p.

As has been stated, without doing some fancy operator overloading (what is the point) you can't assign stuff on the left to stuff on the right.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net

Red Squirrel

No Lifer
May 24, 2003
69,934
13,458
126
www.anyf.ca
When assigning a value always think of it as this:

Variable = [whatever is here]

So Variable is set to whatever is after the equal sign. This can be a single number, another variable, an equation, or even a function that returns a numerical type.

This goes for most programming languages that I've seen. Some may use different syntax but same idea.
 

KAMAZON

Golden Member
Apr 4, 2001
1,300
0
76
www.alirazeghi.com
Shoot, guys I got another problem :(
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'double' (or there is no acceptable conversion)

It's coming from this line:
cin >> totalWage = hoursWorked * hourlyWage;

I'm sorry but what is it even saying? Thanks
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
Operator overloading tends to lead to some pretty confusing error messages.

In this case, as is common, I tend to jump right to the specified line and take a look and see if the code makes any sense to begin with. Oftentimes the problem is obvious and I smack my head.

Does this line of code make any sense to you as it is? Because if you can't explain it to yourself, the compiler sure isn't going to understand you.
 

PingSpike

Lifer
Feb 25, 2004
21,755
599
126
Remove that line? I'm not sure what you're trying to accomplish with it, but that is not correct syntax regardless. If you made the fixes suggested for the lvalue assignment issue, totalWage should already have the correct value assigned and there's no need for another cin.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Keep your = statements separate from your input/output statements unless you put () around the mess. And that is not guaranteed to work/be a good idea
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
Shoot, guys I got another problem :(
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'double' (or there is no acceptable conversion)

It's coming from this line:
cin >> totalWage = hoursWorked * hourlyWage;

I'm sorry but what is it even saying? Thanks

What are you trying to do with that line? What variable are you trying to input?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,592
4,498
75
It's coming from this line:
cin >> totalWage = hoursWorked * hourlyWage;

Yeah, what are you trying to do there?

Setting a variable to the result of a formula doesn't require cin. You'd just say:

totalWage = hoursWorked * hourlyWage;

cin is *only* for reading user input. When reading user input, you put it in a variable (someplace for the computer to store the information). And that's all you do on that line:

cin >> totalWage;

But I think you want the former rather than the latter.
 

Mohamed Alturfi

Junior Member
Apr 22, 2018
2
0
1
I know this is old lol but for any new people experiencing same issue, I fixed it.
So basically you have the "hoursWorked" and "hourWage" as string values basically, you cannot multiply letters aka you cant multiply 2 strings, so you would simply change the hoursWorked and hourWage from string values to int.
 

sweenish

Diamond Member
May 21, 2013
3,656
60
91
Not sure if I should report or not. For now, just wtf? That's nowhere near correct.
 

Mohamed Alturfi

Junior Member
Apr 22, 2018
2
0
1
sorry bro, but I used visual studios and made this adjustment and it worked for me, so i don't know why you would think it's incorrect.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,592
4,498
75
This necro isn't adding much to this thread, so I think I'll just lock it - Programming Moderator Ken g6
 
Status
Not open for further replies.