Thread: I don't see any use in using the structure

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    I don't see any use in using the structure

    Hi

    One simple question. In the simple programs I have done so far about the use of the structure I have failed to see any benefit of using the structure syntax. Could you please tell me what is the benefit of using the structure syntax in the follow code? Thanks.

    Code:
    // number of employees having salary greater than $12,000
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    ////////////////////////////////////////////////////
    struct employee
    { int  empno; float sal; };
    ////////////////////////////////////////////////////
    
    int main()
    
    {
        int empsalgrt12 = 0;
        int numofemp;
    
        employee record;
    
        cout << "how many employees are there?: ";
        cin >> numofemp;
    
        cout << endl;
    
        for (int i=1; i<=numofemp; i++)
    
            {
             cout << "enter employ no.: ";
             cin >> record.empno;
    
             cout << "enter salary: ";
             cin >> record.sal;
    
             cout << endl;
    
            if ( record.sal > 12000 )
    
                {
                 empsalgrt12++;
                }
            }
    
        cout << "number of employees with salary greater than $12,000: " << empsalgrt12 << endl;
    
        return 0;
    
        system ("pause");
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well in that instance of the program, there is little benefit.

    But if you need to store an array of employees, with a lot more information per employee (address, years service, benefits, holidays allowed and taken, etc), then a struct is a must.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The program might be a bit trivial to see any advantage. What if you had to display all employees' data in sorted order later?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, Salem, anon.

    Salem, if you don't mind could you please help me to understand the bold phrase in your signature: If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

    What does the phrase "of undefined behaviour" mean here? Thanks.


    Probably I will get to see the benefit of the structure syntax later when I run into complex problems.

    I'm getting these errors for the code given below:
    Code:
    expected '}' before 'else'
    expected 'while' before 'v'
    expected '(' before 'v'
    'v' was not declared in this scope
    expected ')' before ';' token
    expected unqualified-id before 'while'
    expected constructor, destructor, or type conversion before '<<' token
    expected constructor, destructor, or type conversion before '<<' token
    expected unqualified-id before 'return'
    expected constructor, destructor, or type conversion before '(' token
    expected declaration before '}' token
    I understand most of the errors have to do with the problem of declaring variables within the block of the do/while loop. Correct? But I don't get the reason for the first bold error above. Arn't I supposed to enclose the if block within the braces (else if and else are part of the if statement (or, condition) ). Please help me. Thanks.

    Code:
    // sorting out telephone numbers from different cities
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    ////////////////////////////////////////////////////////
    struct teldata
    { int countrycode, citycode, number;};
    ////////////////////////////////////////////////////////
    
    int main()
    
    {
        teldata telnumber;
    
        do
            {
    
            cout << "enter the details\n";
    
            cout << "enter country code: "; cin >> telnumber.countrycode;
    
            cout << "enter city code: "; cin >> telnumber.citycode;
    
            cout << "enter the number: "; cin >> telnumber.number;
    
            int Num4rmCity1=0, Num4rmCity2=0, Num4rmOtherCity;
    
            if (telnumber.citycode == 111)
                {
                 Num4rmCity1 = Num4rmCity1++;
    
                 else if (telnumber.citycode == 222)
                 Num4rmCity2 = Num4rmCity2++;
    
                 else
                 Num4rmOtherCity = Num4rmOtherCity++;
                }
    
            char v;
    
            cout << "Do you want to enter another number: "; cin >> v;
    
            }
        while (v != 'n')
    
            cout << "tel numbers from City 1: " << Num4rmCity1 <<endl;
    
            cout << "tel numbers from City 2: " << Num4rmCity2 <<endl;
    
            cout << "tel numbers from other cities: " << Num4rmOthercity <<endl;
    
    
        return 0;
    
        system ("pause");
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    if ( condition )
    {
      //
    }
    else if ( condition )
    {
      //
    }
    else
    {
      //
    }

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Quote Originally Posted by jackson6612 View Post
    What does the phrase "of undefined behaviour" mean here? Thanks.
    Undefined behaviour is when you use c++ in a way that is not covered by the c++ standard (or is stated to be undefined by the standard) and thus there is no way to predict what will be the end-result. It might work because the compiler you are using right now behaves in a certain way, it might crash the program completely (or worse). For instance it could format your harddrive, it probably won't but that is the thing with undefined behaviour; you just don't know what will happend.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Section 1.3.12 of the C++ standard defines and discusses "undefined behaviour" (albeit with american spelling) thus;
    behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements. Undefined behavior may also be expected when this International Standard omits the description of any explicit definition of behavior. [Note: permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed.]
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also missing semicolon at the end of the while.
    Also, try to indent more consistently.
    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.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, everyone.

    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    But the above quote isn't covered by C++ standard! I'm an English learner so I'm a bit curious about the meaning. Isn't the broken glass always of undefined behavior - with glass pieces projecting in random directions? Is there broken glass with 'defined' behavior?

    Please let me know. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's a Metaphor
    If you dance on broken glass, you may get lucky and remain uninjured, or you could wind up in hospital.

    By analogy,
    If your program has undefined behaviour, you may get lucky and it will still do exactly what you wanted (and expected). Then again, it could just crash.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot for edifying me on the meaning of your signature.

    Best regards
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    19
    i think in this easy program , the easy syntax structure is enough !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. referencing a structure element from another structure
    By ajitht1986 in forum C Programming
    Replies: 5
    Last Post: 02-08-2011, 03:36 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  4. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  5. Replies: 4
    Last Post: 11-22-2006, 12:20 PM