Like Tree2Likes
  • 2 Post By grumpy

Some help understanding certain errors

This is a discussion on Some help understanding certain errors within the C++ Programming forums, part of the General Programming Boards category; Hello members, I just started programming and had to work on a certain assignment. The program ought to do different ...

  1. #1
    Liz
    Liz is offline
    Registered User Liz's Avatar
    Join Date
    Sep 2012
    Posts
    4

    Unhappy Some help understanding certain errors

    Hello members,

    I just started programming and had to work on a certain assignment. The program ought to do different things like verifying age, calculating age etc. So I split it and worked on small parts.


    The first part with verifying age is done with no errors. I started working on part 2 which was to calculate the age of the user. I copied the code from notepad, pasted it into the existing code but it showed some errors I don't understand yet. The error stated that in the very last else-statement 'expected primary expression before else and expected ';' before else. What also bothers me is that when I debug it, the program runs the 'part-1' but ends there, while there also is a 'part 2'.

    Any guidance would be greatly appreciated. As until now, we only covered the pure 'basics' and the different boolean expressions.

    ~liz~
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    
    int main ()
    {
     int PEILJAAR;
    PEILJAAR: 2012;
    
    
     int PEILMAAND;
    PEILMAAND: 9;
    
    
     int PEILDAG;
    PEILDAG: 23;  
    
    
    int j, m, d;
    int Leeftijd_maandentotaal;
    int Leeftijd_jaar;
    int Leeftijd_maanden;
    
    
    
       //Eerst vragen we de gebruiker om zijn geboortegegevens in te vullen//
                
        cout << "Voer geboortejaar in."<< endl;
        cin >> j;     
            if (j<1912 || j>2002) {
            cout << "Te jong/te oud. "<< endl; //We kijken gelijk al of hij qua jaar te jong of te oud is//
            Sleep (500);
            }         
            else 
            {
                 
        cout <<"Voer geboortemaand in."<< endl;
        cin >> m;
            if (m<1 || m>12){
               cout <<"Dat kan niet. "<< endl; //We controleren ook gelijk of hij wel een normale maand invult//
               Sleep (500);
               }
            else {
            cout <<"Voer geboortedag in. "<<endl;
            cin >> d;
        
        /*Nu gaan we met behulp van if-else de gehele datum klopt, samen met de ingevulde datum.
          De laatste els if statement controleert het schrikkeljaar voor februari. Zo hebben we alle mogelijke data gecontroleerd*/
        
         if ((d<1 || d>31) && (m==1,3,5,7,8,10,12))
            cout <<"Dat kan niet."<< endl;
            
            
         else if ((d<1 || d>30) && (m==4,6,9,11))
              cout <<"Dat kan niet."<< endl;
         
         else if ((j%4!=0)&&(m==2) && (d==29))
              cout <<"Dat kan niet."<< endl;
         
          /*this was part 1*/  
               
               }
               }
                        
         /*De leeftijd berekenen we vervolgend met het volgende stuk code. Er zijn 4 verschillende mogelijkheden 
        , waarvan het programma een uitvoert*/
    
    
     /*part 2 begins here*/
    
    
    if ((d<PEILDAG) && (m<PEILMAAND) && (j<PEILJAAR)){
       
       Leeftijd_maandentotaal = ((PEILMAAND-m)+(PEILJAAR-j)*12);
       Leeftijd_jaar = (PEILJAAR-j);
       Leeftijd_maanden; ((PEILMAAND-m));
        
        cout<<"Jouw leeftijd is "<<Leeftijd_maandentotaal<<" maanden totaal en "<<Leeftijd_jaar<<"jaar en "<<Leeftijd_maanden<<"maanden."<< endl;}
    
    
    else if ((d>PEILDAG) && (m>PEILMAAND) && (j<PEILJAAR)){
    
    
        Leeftijd_maandentotaal = (((PEILJAAR-j-1)*12)+(m-PEILMAAND)-1);
        Leeftijd_jaar = (PEILJAAR-j-1);
        Leeftijd_maanden = (12+(m-PEILMAAND)-1);
    
    
        cout<<"Jouw leeftijd is "<<Leeftijd_maandentotaal<<" maanden totaal en "<<Leeftijd_jaar<<"jaar en "<<Leeftijd_maanden<<"maanden."<< endl;}
    
    
    else if ((d<PEILDAG) && (m>PEILMAAND) && (j<PEILJAAR));{
    
    
        Leeftijd_maandentotaal = (((PEILJAAR-j)*12)+(m-PEILMAAND));
        Leeftijd_jaar = (PEILJAAR-j-1);
        Leeftijd_maanden = (12+(m-PEILMAAND));
    
    
        cout<<"Jouw leeftijd is "<<Leeftijd_maandentotaal<<" maanden totaal en "<<Leeftijd_jaar<<"jaar en "<<Leeftijd_maanden<<"maanden."<< endl;}
    
    
    else if ((d>PEILDAG) && (m<PEILMAAND) && (j<PEILJAAR)){
    
    
        Leeftijd_maandentotaal = (((PEILJAAR-j)*12)+((m-PEILMAAND)+1));
        Leeftijd_jaar = (PEILJAAR-j);
        Leeftijd_maanden = ((PEILMAAND-m)-1);
    
    
        cout<<"Jouw leeftijd is "<<Leeftijd_maandentotaal<<" maanden totaal en "<<Leeftijd_jaar<<"jaar en "<<Leeftijd_maanden<<"maanden."<< endl;}
        
    
    
    
    
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    This snippet compiles but is certainly not what you mean:
    Code:
     int PEILJAAR;
    PEILJAAR: 2012;
     
     
     int PEILMAAND;
    PEILMAAND: 9;
     
     
     int PEILDAG;
    PEILDAG: 23;
    The syntax to init. a variable is:
    Code:
    int pi = 3.14;
    Also before continuing, please indent correctly your code, it's not readable like that.
    After you do that, your syntax error will probably become obvious.

    EDIT:
    C doesn't allow to check directly if a value belongs to a list or a set, this syntax, once again compiles but is not what you mean:
    Code:
    if ((d<1 || d>31) && (m==1,3,5,7,8,10,12))
    (repeated several times)
    You have to check for equality for each value.

    EDIT2:
    Another syntax mistake:
    Code:
     Leeftijd_maanden; ((PEILMAAND-m));
    ** If you use properly your compiler, it would have warned you about all of them **

    EDIT3:
    Another one:
    Code:
    else if ((d<PEILDAG) && (m>PEILMAAND) && (j<PEILJAAR));{
    Fix all of that and maybe your program will do something.
    I know you're beginning but I *strongly* suggest you work on your programming style (e.g braces position) and review the basic syntax of C.
    I should have said C++ but you didn't use any OO concept in here except cout, cin.
    Last edited by root4; 09-10-2012 at 03:54 PM.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    you have left a stray ; at the end of your else if statement, line 93 - And sort out your style on the curly braces /block statements - this is what may have lead to you being unable to spot that error. I got a mass of warnings on compiling the 'fixed ' version too, i have not examined for other problems.
    Last edited by rogster001; 09-10-2012 at 03:47 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Liz
    Liz is offline
    Registered User Liz's Avatar
    Join Date
    Sep 2012
    Posts
    4
    Thank you all .
    I will look into that and keep you updated on how it's going.
    Regards,

    Liz

  5. #5
    Liz
    Liz is offline
    Registered User Liz's Avatar
    Join Date
    Sep 2012
    Posts
    4
    @Roger, that's is strange. It's full of errors and synthax mistakes and yet little shows up.
    As root4 suggested I'll take another look at the 'basics' chapter, and also start fixing my synthax style. Then I'll continue spotting the other errors and fix them too.

    I'm however a bit stressed as I need to get the whole bunch done, including all the 'other functions/tasks' to prop in. And that next week (total of 2,5 week)
    Not that I started late; I've been working non-stop on this ever since. But maybe I'm thinking up structures that are too difficult?

    (Sigh). Maybe it is all way simpler then what I'm making of it until now

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Your strategy is good (divide and work on each sub-problem), but unfortunately your code doesn't (yet) reflect it.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,536
    Quote Originally Posted by Liz View Post
    @Roger, that's is strange. It's full of errors and synthax mistakes and yet little shows up.
    Compilers, by default, are configured so they don't issue warning or error messages on code that is valid but probably doesn't work as intended. You need to read documentation for your compiler to learn how to use it, in a manner that does issue more warnings and errors.

    If it sounds crazy to you that you need to do that, you're not alone. The reason is historical: a whole generation of lazy programmers cared more about getting code to quickly compile without errors than they did about getting code that works. So they lobbied compiler vendors to disable more informative diagnostics.

    Quote Originally Posted by Liz View Post
    I'm however a bit stressed as I need to get the whole bunch done, including all the 'other functions/tasks' to prop in. And that next week (total of 2,5 week)
    Not that I started late; I've been working non-stop on this ever since. But maybe I'm thinking up structures that are too difficult?

    (Sigh). Maybe it is all way simpler then what I'm making of it until now
    You need to relax. Accept that a compiler is an ignorant pedant that interprets C code in a specific manner. If you make a mistake in communication, it will blithely produce results your don't want.

    Your problem is that you are trying to describe things in a manner that is loose, and hand-wavy. If you described what you are trying to do to a person, that person might grasp intuitively what you are trying to do, and do the things you have forgotten to explain. So that type of communication works with getting people to do things. However, a compiler doesn't work that way. It will take your loose and hand-wavy description, interpret it pedantically, and not try to fill in the gaps you have left. It will therefore do exactly what you have told it, and no more.

    How should you respond to that? You need to be precise, and break big problems down to smaller problems.

    If you are thinking in terms of "do X to a bunch of Y's" then, unless someone else has told the compiler how to "do X to a bunch of Y's" (i.e. they've written a function that can be used for the purpose), you have to tell the compiler HOW to do it, not just WHAT you want to do You need to teach our ignorant pedant. So, if you want to test if a value m is equal to one of the values
    Code:
    {1,3,5,7,8,10,12}
    it is not enough to tell the compiler
    Code:
    (m==1,3,5,7,8,10,12)
    You need to compare the value m against the value 1 then against the value 3, then ....... (note I'm using ..... to mean keep repeating something). So you need to write code like
    Code:
    (m == 1 || m == 3 || m == 5 || ....)
    Once you have told the compiler how to do it, it will do the job exactly, every time. But if you make one little error, it will make the same mistake, exactly, every time. (Just cross fingers and hope that nothing outside your control makes the compiler malfunction).

    Generally, the approach is very simple but requires people to give specific attention to detail, which many people find to be hard work. It is a skill that some people find easier to pick up than others. Most people expect to describe problems imprecisely, and have someone/something else fill in the unstated details. A C compiler will not do that (unless someone has, very precisely, given it a set of rules to do so). A computer will not do that either. Since they won't, the programmer must.

    Making a computer accept things that are described in loose and fuzzy ways .... which a person uses to communicate .... is a subject of ongoing research. Until that research succeeds - the skill of programming is equivalent to the skill of talking to an ignorant pedant.
    Last edited by grumpy; 09-10-2012 at 05:18 PM.
    rags_to_riches and iMalc like this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Just in case you don't notice it easily, the semicolon near the end of line 93 is a mistake.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Liz
    Liz is offline
    Registered User Liz's Avatar
    Join Date
    Sep 2012
    Posts
    4
    Thank you all folks .
    Today I had another lecture and working class on this project and it's going in a positive direction now. Together with your suggestions and the instructions of the prof I sorted out the mess and errors. Still have to finish up, compile and polish it a bit .

    When it's done I'll update again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Some Understanding
    By Typina in forum C Programming
    Replies: 3
    Last Post: 03-29-2010, 02:30 PM
  2. New to C, need help understanding errors
    By 3wit in forum C Programming
    Replies: 20
    Last Post: 04-10-2008, 08:52 PM
  3. Need help understanding..
    By TylerMoyer in forum Game Programming
    Replies: 2
    Last Post: 08-07-2007, 11:11 AM
  4. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 09:43 PM

Tags for this Thread


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