• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

multiplying 2 variables in C++ ...

Status
Not open for further replies.

KAMAZON

Golden Member
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;
}
 
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;
 
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.
 
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 😛.

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.
 
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.
 
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
 
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.
 
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.
 
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
 
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?
 
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.
 
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.
 
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.
Back
Top