Require help with several errors (c2106, C4305)

This is a discussion on Require help with several errors (c2106, C4305) within the C++ Programming forums, part of the General Programming Boards category; Im new to programming. Infact, I started learning yesterday so I am inexperienced and require help. Im trying to make ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    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. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    '=' 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?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    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. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by Arsalan Ahmad View Post
    The program says that the date is invalid even when I put a valid date. What am I doing wrong?
    Quote Originally Posted by GReaper View Post
    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... )
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    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;
    
    
    
        
    
    
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
    Last edited by Arsalan Ahmad; 10-06-2011 at 03:00 PM.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    Quote Originally Posted by Arsalan Ahmad View Post
    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. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Quote Originally Posted by GReaper View Post
    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.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    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. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    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. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    For all three( years, months, days ), take the absolute values of their differences.

    EDIT: Use "abs()" with "#include <cstdlib>"
    Last edited by GReaper; 10-06-2011 at 03:30 PM.
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Arsalan Ahmad View Post
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    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. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    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. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    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).
    Last edited by Elysia; 10-07-2011 at 09:18 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs and Error C2106
    By Dest in forum C Programming
    Replies: 5
    Last Post: 04-13-2010, 08:00 AM
  2. error C2106: '=' : left operand must be l-value
    By janicecp in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2009, 08:51 AM
  3. error C2106: '=' : left operand must be l-value
    By sobieski19 in forum C Programming
    Replies: 33
    Last Post: 02-08-2008, 03:43 AM
  4. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  5. error C2106: (populating strings)
    By jdinger in forum C++ Programming
    Replies: 5
    Last Post: 03-05-2002, 02:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21