Thread: Structs:

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why not use a constructor for your class?

    Code:
    struct StudentRecord
    {
       StudentRecord() : ID(1234), GPA(4.0)
       {
          strcpy(Name, "SuperProgrammer");
       }
     
        char Name[name_size];
        int ID;
        double GPA;
    };
    Also why are you printing each individual character instead of just printing the string?
    Code:
        cout<<"Students Name: ";
        for (int i=0; i<sizeof(str1)-1; i++) {
             
            cout<<TESCStudent.Name[i];
        }
        cout << "Students Name; " << TESTCStudent.Name << endl;
    There are a couple of things wrong with the way you're printing each character, first you're printing the entire array, which in this case has more characters than the string actually contains. In your example I wouldn't be surprised that you don't see characters that the user doesn't enter. You should be stopping at the end of the string not the end of the array. These could and usually are different numbers. For example if I entered "Jim" for the name the string length would be 3 and the size of the array would still be 20.

    Oh and in the following snippet:
    Code:
        cin.getline(TESCStudent.Name,20,'\n');
    You don't need that '\n' in this call, the '\n' is the default value.


    Jim

  2. #17
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why you need to complicate the simple task of struct initializing with constants?

    Code:
    void HW_task1_2()
    {
        struct StudentRecord test2 = 
        {
            "Super Programer",
            1234,
            4.0
        };
         
        
         
        std::cout<< "Student's Name: " << test2.Name << std::endl;
        std::cout<< "Student's ID: "   << test2.ID   << std::endl;
        std::cout<< "Student's GPA: "  << test2.GPA  << std::endl;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #18
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Jim- That was a very clever way to use a function in a struct. I will remember that in the future. Yes my loop is printing out empty characters. I was able to use the sizeof -1 to prevent it in one function but was not able to do it in the other. Mainly due to the fact I didn't know how long the user input was going to be.

    Vart- You and Jim had similar ideas that I will remember in the future. It looks very clean and less confusing. I guess we code how we think about the problem to some degree. If you are confused, the code will show.

    You guys were a big help, thanks again!

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Just remember that there is very little difference between a struct and a class in C++. And remember one of the greatest strengths, IMO, of C++ is Classes. Information hiding and encapsulation are a couple of these strengths. And remember in C++ you don't need the struct qualifier except when defining the structure.

    And the sooner you start learning to use these C++ features the better off you will be. Start using std::string, std::vector and the other std::containers as soon as possible. C-strings can be very error prone and the C++ string class eliminates some of the problems.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2013, 07:55 AM
  2. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  3. [ noob question ] Help with structs within structs
    By Riverfoot in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 07:24 PM
  4. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM