# Thread: Require help with several errors (c2106, C4305)

1. ## Require help with several errors (c2106, C4305)

Im new to programming. Infact, I started learning yesterday so I am inexperienced and require help. Im trying to make an age calculator. I havent even got halfway into the program and still it is giving me lots of errors. Please look at the code and the errors.

Thanks

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

int main ()
{
int D1;
int D2;
int D3;
int M1;
int M2;
int M3;
int Y1;
int Y2;
int Y3;
int G;
int L;
int Valid;
cout<<"Age Calculator\n";

cout<<"\n Enter the date of the birth (DD MM YYYY): ";

cin>> D1;
cin>> M1;
cin>> Y1;
if (M1=1 || M1=3 || M1=5 || M1=7 || M1=9 || M1=11)
int L=31;

else if (M1=4 || M1=6 || M1=8 || M1=10 || M1=12)
{int L=30;}

else if (M1=2)
{int L=29;}

else if (D1<1 || D1>L)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}

else if (M1<1 || M1>12)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}

else if (Y1<0 || Y1>2011)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}

cout<<"Enter the date of birth again  ";
cin>> D1;
cin>> M1;
cin>> Y1;

while (valid=0);

cout<<"\n Enter second day of the month (01-31): ";

cin>> D2;

cout<<"\n Enter second month: ";

cin>> M2;

cout<<"\n Enter the second year: ";

cin>> Y2;

}```

Errors:

AgeCal.cpp
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(25): warning C4305: '=' : truncation from 'int' to 'bool'
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(25): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(25): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(25): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(25): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(25): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(28): warning C4305: '=' : truncation from 'int' to 'bool'
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(28): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(28): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(28): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(28): error C2106: '=' : left operand must be l-value
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(57): error C2065: 'valid' : undeclared identifier
1>c:\users\arsalan\documents\visual studio 2010\projects\agecal\agecal\agecal.cpp(57): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1>

2. '=' is assignment.
'==' is comparison.

Don't redeclare the variables inside the if statements, you already did this outside of them. Just set them to that value.

PS: Why is a "while" hanging loose in line 57?

3. ## It worked!

I changed the = to == and that problem was solved. Now my current code is this

code1 - Pastebin.com

The program says that the date is invalid even when I put a valid date. What am I doing wrong?

4. Originally Posted by Arsalan Ahmad
The program says that the date is invalid even when I put a valid date. What am I doing wrong?
Originally Posted by GReaper
Don't redeclare the variables inside the if statements, you already did this outside of them. Just set them to that value.
( I ended up quoting myself... )

5. I changed the code. Now please look at this and tell me what is wrong with it. Thanks

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

int main ()
{
int D1;
int D2;
int D3;
int M1;
int M2;
int M3;
int Y1;
int Y2;
int Y3;
int G;
int L=0;
int Valid=1;
cout<<"Age Calculator\n";

do
{
cout<<"\n Enter the date of the birth (DD MM YYYY): ";

cin>> D1;
cin>> M1;
cin>> Y1;
if (M1==1 || M1==3 || M1==5 || M1==7 || M1==9 || M1==11)
int L=31;

if (M1==4 || M1==6 || M1==8 || M1==10 || M1==12)
int L=30;

if (M1==2)
int L=29;

if (D1<1 || D1>L)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}

else if (M1<1 || M1>12)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}

else if (Y1<0 || Y1>2011)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}
else int Valid=1;

} while (Valid==0);

cout<<"\n Enter second day of the month (01-31): ";

cin>> D2;

cout<<"\n Enter second month: ";

cin>> M2;

cout<<"\n Enter the second year: ";

cin>> Y2;

}```

6. Originally Posted by Arsalan Ahmad
I changed the code. Now please look at this and tell me what is wrong with it. Thanks

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

int main ()
{
int D1;
int D2;
int D3;
int M1;
int M2;
int M3;
int Y1;
int Y2;
int Y3;
int G;
int L=0;
int Valid=1;
cout<<"Age Calculator\n";

do
{
cout<<"\n Enter the date of the birth (DD MM YYYY): ";

cin>> D1;
cin>> M1;
cin>> Y1;
if (M1==1 || M1==3 || M1==5 || M1==7 || M1==9 || M1==11)
int L=31;

if (M1==4 || M1==6 || M1==8 || M1==10 || M1==12)
int L=30;

if (M1==2)
int L=29;

if (D1<1 || D1>L)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}

else if (M1<1 || M1>12)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}

else if (Y1<0 || Y1>2011)
{
cout<<"InValid Date. Enter Again"<<endl;
int Valid=0;
}
else int Valid=1;

} while (Valid==0);

cout<<"\n Enter second day of the month (01-31): ";

cin>> D2;

cout<<"\n Enter second month: ";

cin>> M2;

cout<<"\n Enter the second year: ";

cin>> Y2;

}```
what it should do is that it should repeat the stuff between while block for every invalid value and step out of the while loop when a valid date is entered

7. Originally Posted by GReaper
Don't redeclare the variables inside the if statements, you already did this outside of them. Just set them to that value.
Do I need to say it yet again?

Oh, okay. Inside your if, else if and else statements you have "int". Remove that particular word and nothing else for the moment.

8. The program is running fine for now. Thanks GReaper. I know it must be frustrating to repeat the small things but thanks for the patience.

9. Next, Im going to use similar validity test on the second date and then calculate their difference. Im still a bit confused about how to calculate the difference ( how to carry from the next month if the day difference becomes negative etc.)

10. For all three( years, months, days ), take the absolute values of their differences.

EDIT: Use "abs()" with "#include <cstdlib>"

11. Originally Posted by Arsalan Ahmad
I changed the code. Now please look at this and tell me what is wrong with it. Thanks
Here is quite a novel suggestion for you, instead of just stabbing in the dark here why don't you attempt to follow a concise tutorial or book so that you can actually learn the language? Here are the C++ Made Easy Tutorials found on this site; they serve as a great starting point.

12. What does it mean to "initialize" a variable? I continued on that same age calculator and Im trying to convert the months into days so that it will be easy for me to take the difference. After that, I'll convert it back into months and days.

When I run the following code, the error says that "Run-Time Check Failure #3 - The variable 'T2' is being used without being initialized."

Code:
```#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
int D1;
int D2;
int D3;
int M1;
int M2;
int M3;
int Y1;
int Y2;
int Y3;
int G;
int L=0;
int Valid=1;
int T1;
int T2;
int T3;
int LY1=0;
int LY2=0;

cout<<"Age Calculator\n";

do
{
cout<<"\n Enter the date of the birth (DD MM YYYY): ";

cin>> D1;
cin>> M1;
cin>> Y1;
if (M1==1 || M1==3 || M1==5 || M1==7 || M1==9 || M1==11)
L=31;

if (M1==4 || M1==6 || M1==8 || M1==10 || M1==12)
L=30;

if (M1==2 && Y1 % 4 == 0)
{L=29;
LY1=1;}

else if (M1==2)
L=28;

if (D1<1 || D1>L)
{
cout<<"InValid Date. Enter Again"<<endl;
Valid=0;
}

else if (M1<1 || M1>12)
{
cout<<"InValid Date. Enter Again"<<endl;
Valid=0;
}

else if (Y1<0 || Y1>2011)
{
cout<<"InValid Date. Enter Again"<<endl;
Valid=0;
}
else  Valid=1;

} while (Valid==0);

do
{
cout<<"\n Enter the date of the death (DD MM YYYY): ";

cin>> D2;
cin>> M2;
cin>> Y2;
if (M2==1 || M2==3 || M2==5 || M2==7 || M2==9 || M2==11)
L=31;

if (M2==4 || M2==6 || M2==8 || M2==10 || M2==12)
L=30;

if (M1==2 && Y1 % 4 == 0)
{L=29;
LY2=1;}

else if (M1==2)
L=28;

if (D2<1 || D2>L)
{
cout<<"InValid Date. Enter Again"<<endl;
Valid=0;
}

else if (M2<1 || M2>12)
{
cout<<"InValid Date. Enter Again"<<endl;
Valid=0;
}

else if (Y2<0 || Y2>2011 || Y2<Y1)
{
cout<<"InValid Date. Enter Again"<<endl;
Valid=0;
}
else  Valid=1;

} while (Valid==0);

{
if (M1==1)
D1=T1;

if (M1==2)
T1= D1+31;

if (M1==3 && LY1==0)
T1= D1+59;
else if (M1==3 && LY1==1)
T1= D1+60;

if (M1==4 && LY1==0)
T1= D1+90;
else if (M1==4 && LY1==1)
T1= D1+91;

if (M1==5 && LY1==0)
T1= D1+120;
else if (M1==5 && LY1==1)
T1= D1+121;

if (M1==6 && LY1==0)
T1= D1+151;
else if (M1==6 && LY1==1)
T1= D1+152;

if (M1==7 && LY1==0)
T1= D1+181;
else if (M1==7 && LY1==1)
T1= D1+182;

if (M1==8 && LY1==0)
T1= D1+212;
else if (M1==8 && LY1==1)
T1= D1+213;

if (M1==9 && LY1==0)
T1= D1+242;
else if (M1==9 && LY1==1)
T1= D1+243;

if (M1==10 && LY1==0)
T1= D1+274;
else if (M1==10 && LY1==1)
T1= D1+275;

if (M1==11 && LY1==0)
T1= D1+304;
else if (M1==11 && LY1==1)
T1= D1+305;

if (M1==12 && LY1==0)
T1= D1+335;
else if (M1==12 && LY1==1)
T1= D1+336;
}

{
if (M2==1)
D2=T2;

if (M2==2)
T2= D2+31;

if (M2==3 && LY2==0)
T2= D2+59;
else if (M2==3 && LY2==1)
T2= D2+60;

if (M2==4 && LY2==0)
T2= D2+90;
else if (M2==4 && LY2==1)
T2= D2+91;

if (M2==5 && LY2==0)
T2= D2+120;
else if (M2==5 && LY2==1)
T2= D2+121;

if (M2==6 && LY2==0)
T2= D2+151;
else if (M2==6 && LY2==1)
T2= D2+152;

if (M2==7 && LY2==0)
T2= D2+181;
else if (M2==7 && LY2==1)
T2= D2+182;

if (M2==8 && LY2==0)
T2= D2+212;
else if (M2==8 && LY2==1)
T2= D2+213;

if (M2==9 && LY2==0)
T2= D2+242;
else if (M2==9 && LY2==1)
T2= D2+243;

if (M2==10 && LY2==0)
T2= D2+274;
else if (M2==10 && LY2==1)
T2= D2+275;

if (M2==11 && LY2==0)
T2= D2+304;
else if (M2==11 && LY2==1)
T2= D2+305;

if (M2==12 && LY2==0)
T2= D2+335;
else if (M2==12 && LY2==1)
T2= D2+336;
}

T3= T2-T1;

if (T3<0)
{Y2=Y2-1;
T2= T2+365;
T3= T2-T1;
}

Y3= Y2 - Y1;

cout<<Y3<<" "<<T3;

getch();

}```

BTW, I am learning from a C++ book written by Deitel but the book doesnt help much with the errors and I started writing code about 2 days ago so I'm still learning the basics.

13. The above error just got fixed on its own. I dont know why. Well you can see the code and what it is trying to do. It does the work but is there any way to shorten or to optimize the code? Any tips or ideas are appreciated as long as they arent too complex for me at this time

14. That message means you use a variable without first giving it an initial value. This is undefined since a variable can have any random value upon creation. Initialization simply means assigning a value to a variable at the point of creation.

Then you might start learning functions. You're basically inputting a date at two points in the code, and it's exactly the same (exact the for message). That's just ideal for a function.

The variable names need work also (what do they represent, what do they mean?).

The rest of the code is not clear to me what it's supposed to do (partly due to bad variable names).