some questions about structure

This is a discussion on some questions about structure within the C++ Programming forums, part of the General Programming Boards category; Hi I am making a small program of students, where the user inputs the students details as age,class,name and address. ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    some questions about structure

    Hi

    I am making a small program of students, where the user inputs the students details as age,class,name and address.


    I am making a structure for the student and wanted to know that if we make a structure using strings as:

    Code:
    string student_name;
    string student_address;
    string student_age;
    
    
    Do we have to define the variables of these data members using "string" or we can even define their variables using "char"?

    Also do we initialize the data members using :

    Code:
    student ={"student_name","student_address", "student_age"};
    
    I mean in these curly brackets do we simply enter the data members or we have to mention the exact name,address and age of the student?


    Also before making struct for student do we also have to enter the pre processor for string as:
    Code:
    
    # include<string>
    I hope I am clear
    Last edited by student111; 12-27-2011 at 09:41 AM. Reason: adding more

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    Quote Originally Posted by student111 View Post
    Code:
    student ={"student_name","student_address", "student_age"};
    
    this will cause the values of student_name, student_address, and student_age to be set to the literal values "student_name", "student_address" and "student_age"

    I mean in these curly brackets do we simply enter the data members or we have to mention the exact name,address and age of the student?
    you can put variable names in there, or literal values. you have literal values in your example.

    Also before making struct for student do we also have to enter the pre processor for string as:
    Code:
    
    # include<string>
    yes, but string is in namespace std. so you'll need to either refer to the string type with its fully qualified name (std::string) or put the following line at the top of your source file, but after the #include line:

    Code:
    using std::string;

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by student111 View Post
    I am making a structure for the student and wanted to know that if we make a structure using
    You can do this. I would recommend a class (with proper setters and getters).
    If your compiler supports initializer lists, then the syntax

    Code:
    student ={"student_name","student_address", "student_age"};
    becomes possible too.
    Otherwise I would urge you to rely on a constructor.
    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.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    what is a literal value?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    a string in quotes is a literal value. a number is a literal value. just about anything that you can use to assign a value to a variable, that is not itself a variable, a macro, or a function call, is a literal.

    a few examples:
    Code:
    std::string aLiteralString = "this is a literal string";
    int aLiteralInt = 1234;
    double aLiteralDouble = 314159265.0E-8;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions on structure padding
    By sanddune008 in forum C Programming
    Replies: 1
    Last Post: 12-04-2011, 10:27 AM
  2. Replies: 4
    Last Post: 04-25-2010, 11:57 AM
  3. newbie: pointers to structure questions
    By cstudent in forum C Programming
    Replies: 11
    Last Post: 04-09-2008, 07:23 AM
  4. Replies: 4
    Last Post: 11-22-2006, 12:20 PM
  5. Passing a structure to a fxn questions
    By JackR in forum C++ Programming
    Replies: 9
    Last Post: 09-25-2006, 04:50 PM

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