dynamic memory

R

Richard

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_scores, number_of_student;


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

cin >>student_record;

student_info* student = new student[student_record];

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

cin >>number_test_scores;

student.test_scores = new student.test_scores[number_test_scores];

}
 
J

Jonathan Mcdougall

Richard said:
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?

Please format your code next time.
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

Please, tell us *where* they errors are next time.
Here are the code:

# include <iostream>

# include <string>

using namespace std;
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

// 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()

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

int main()
{


int student_record=0, number_test_scores, number_of_student;

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

Define student_record here.
cin >>student_record;

student_info* student = new student[student_record];

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).
cout<<"How many test scores do you want to calculate average for this
student? : ";

Define number_test_scores here.
cin >>number_test_scores;

student.test_scores = new student.test_scores[number_test_scores];

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

student.test_scores

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_scores];

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

dynamic memory 2
dynamic memory allocation 3
sorting 1
what am i doing wrong here?help 2
error 7
Python problem 1
Java Help for a Beginner 9
Help with Lowest Score Drop 0

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top