# Thread: "simple" math

1. ## "simple" math

This has been bugging the hell out of me. I have a tax calculator that I making in the console to make sure I understand how simple math works but I keep getting an error "non-lvalue in assignment" and a warning that I am "converting to 'int' from 'floant'" (yet I don't do know how to convert data types yet). I am using Dev-C++ and this website's tutorials. This is the code:
Code:
```#include <iostream>

using namespace std;

int main()
{
//variables
float item_cost;
float tax_rate;
float result;
float tax;

cout<<"Tax Calculator \n";
cout<<"How much does your item cost:";
cin>> item_cost;
cin.ignore();
cout<<"\n";

cout<<"What is your area's tax rate:";
cin>> tax_rate;
cin.ignore();
cout<<"\n";

cout<<"Calculating... \n";
if (tax_rate > 0) {
return tax_rate = tax_rate/100; //error*
return item_cost * tax_rate = tax; //error**
return item_cost + tax = result; //error**
}
else {
return item_cost = result; //error*
return tax_rate = 0; //error*
}

cout<<"Report: \n";
cout<<"Item Cost: "<<item_cost<<"\n";
cout<<"Tax Rate: "<<tax_rate<<"\n";
cout<<"Tax: "<<tax<<"\n";
cout<<"Total: "<<result<<"\n";
cin.get();
}```
Error* = converting to int from float
Error** = non-lvalue in assignment

I have no clue why it gives me those errors. D:

2. Two problems. First, you're using the return statement incorrectly. You use return to exit a function. Your simple program has only a single function, main, so any call to return will exit the program. In the places you are using it you can just remove that word completely. Second, in C++ when you assign a value to a variable, the variable has to be on the left side of the = sign. So for example, if you had a variable named tax, and you wanted to calculate the tax by multiplying the item cost by the tax rate, then you would use tax = item_cost * tax_rate; not the other way around.

By the way, the default type for floating point numbers (i.e. numbers that can have fractional parts) in C++ is double, not float. I would use double instead of float since it will probably be more accurate.

3. Second, in C++ when you assign a value to a variable, the variable has to be on the left side of the = sign.
Damn, I don't know how many times I have forgotten that and did it like I was in school (gradeschool).

Ok, thanks a lot.

Popular pages Recent additions