Hello Im having a hard time trying to figure out what i'm doing wrong in my code the error code is as follows: In function `void bmath(int, int, int, int, int, int)
line 41 non-lvalue in assignment
line 41 expected `;' before "if"
Here is the code:
// In this functiom the user inputs data about their birth month and year and the census
//month and year while the program computes the age of the person.
#include <iostream>
using namespace std;
void bmath(int,int,int,int,int,int)
{
int year, month, censusm, censusy, calm, caly;
cout<<" Please enter your birth month and year"<<endl;
cin>>month;
cin>>year;
cout<<"Please enter the census month and year"<<endl;
cin>>censusm;
cin >>censusy;
calm=month-censusm;
caly=year-censusy;
if(calm < 0)
calm * (-1)= calm
if (caly < 0)
caly= caly * -1
}
void cmath()
{
cout<<"You were born"<<month<<year<<endl;
cout<<"The census month and year is"<<censusm<<censusy<<endl;
cout<<"You are currently"<<caly<<"years and"<<calm<<"months"<<endl;
}
On line 41 you left off a a semicolon and are assigning an rvalue to an rvalue, when you want to be assigning an rvalue to a lvalue. Basically an lvalue is something that you can assign a value to, like a variable or an array element, and an rvalue is value that you can assign (like a constant expression, a variable, a value returned from a function, etc). Lvalues cannot be a constant, function (unless it returns a reference), or anything else that shouldn't be modified. An lvalue occurs to the left of an equals sign, hence the name left-value, and a rvalue occurs to the right. I believe you meant for line 41 to be:
Okay, so I'm assuming that this was a homework assignment, or you were given some code to start. I'm guessing that you were supposed to get all of the info (year, month, censusm, censusy, calm, caly) in your main function, and then pass the data as parameters to your functions. At the moment you have a function with six arguments and another with three arguments, and you're not actually passing any arguments! What's more, you haven't named any of the arguments in your definitions of the functions. Also, your function cmath is messed up. Because you created new variables named year, month, censusm, censusy, calm and caly in your bmath function (even though they already existed as external/global variables), when you're getting getting input values for them in bmath, you're not changing the external variables because they have a higher scope. This means that when you run cmath it is printing variables with junk values.