dynamic memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard

    #1

    dynamic memory

    Define a record structure to hold a set of test scores,
    number of tests, and the average score. The number of
    records are to be determined at runtime. Also the
    number of tests for each record is to be determined at
    runtime. Calculate the average test score for the set
    the set of test scores in each record. Display records.
    Demonstrate with a least five records. I program I wrote have three errors
    and I don't really know what they are. Can anyone help?

    The errors are:
    Error 1 error C2061: syntax error : identifier
    Error 2 error C2228: left of '.test_scores' must have class/struct/union
    Error 3 error C2061: syntax error : identifier 'student' 9

    Here are the code:

    # include <iostream>

    # include <string>

    using namespace std;

    // structure declaration

    struct student_info

    {

    char name[20];

    float* test_scores; // memory will be allocated dynamically

    float average_ts;

    };

    // function prototypes passing structure into function





    void main()

    {


    int student_record= 0, number_test_sco res, number_of_stude nt;


    cout<<"How many student do you want to average test scores? : ";

    cin >>student_recor d;

    student_info* student = new student[student_record];

    cout<<"How many test scores do you want to calculate average for this
    student? : ";

    cin >>number_test_s cores;

    student.test_sc ores = new student.test_sc ores[number_test_sco res];

    }




  • Jonathan Mcdougall

    #2
    Re: dynamic memory

    Richard wrote:[color=blue]
    > Define a record structure to hold a set of test scores,
    > number of tests, and the average score. The number of
    > records are to be determined at runtime. Also the
    > number of tests for each record is to be determined at
    > runtime. Calculate the average test score for the set
    > the set of test scores in each record. Display records.
    > Demonstrate with a least five records. I program I wrote have three errors
    > and I don't really know what they are. Can anyone help?[/color]

    Please format your code next time.
    [color=blue]
    > The errors are:
    > Error 1 error C2061: syntax error : identifier
    > Error 2 error C2228: left of '.test_scores' must have class/struct/union
    > Error 3 error C2061: syntax error : identifier 'student' 9[/color]

    Please, tell us *where* they errors are next time.
    [color=blue]
    > Here are the code:
    >
    > # include <iostream>
    >
    > # include <string>
    >
    > using namespace std;[/color]


    [color=blue]
    > // structure declaration
    >
    > struct student_info
    >
    > {
    >
    > char name[20];
    >
    > float* test_scores; // memory will be allocated dynamically
    >
    > float average_ts;
    >
    > };
    >
    > // function prototypes passing structure into function
    >
    >
    >
    >
    >
    > void main()[/color]

    Illegal. main() returns an int. *Always*.

    int main()
    [color=blue]
    > {
    >
    >
    > int student_record= 0, number_test_sco res, number_of_stude nt;[/color]

    Don't define all your variables on top of the function. Define them as
    close as their first use as possible.
    [color=blue]
    >
    > cout<<"How many student do you want to average test scores? : ";[/color]

    Define student_record here.
    [color=blue]
    > cin >>student_recor d;
    >
    > student_info* student = new student[student_record];[/color]

    What is "student"? new expects a type.

    student_info* student = new student_info[student_record];

    And use descriptive names (student_count, for example, not
    student_record) .
    [color=blue]
    > cout<<"How many test scores do you want to calculate average for this
    > student? : ";[/color]

    Define number_test_sco res here.
    [color=blue]
    > cin >>number_test_s cores;
    >
    > student.test_sc ores = new student.test_sc ores[number_test_sco res];[/color]

    Several things. "student" is an array. It means there are more than one
    "student". What does

    student.test_sc ores

    means? Which student is it? You want

    student[X].test_scores

    where X is the student you want. Next, new expexts a *type*.

    student[X].test_scores = new float[number_test_sco res];
    [color=blue]
    > }[/color]

    1) you are not ready to use dynamic memory.
    2) get a good book
    3) learn the basics
    4) read the FAQ at http://www.parashift.com/c++-faq-lite, it'll help
    you
    5) start small!

    6) use std::string from <string>
    7) use standard containers, such as std::vector from <vector>


    Jonathan

    Comment

    Working...