Sie sind auf Seite 1von 12

Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

This is Google's cache of http://www.hackforums.net/printthread.php?tid=200662. It is a snapshot of the


page as it appeared on Nov 9, 2010 06:11:56 GMT. The current page could have changed in the
meantime. Learn more

These search terms are highlighted: Text-only version


after the arrays have been dynamically allocated

Need help dynamicallt allocating a structure!! - Printable Version

+- Hack Forums (http://www.hackforums.net)


+-- Forum: Programming, Coding, and Languages (/forumdisplay.php?fid=151)
+--- Forum: C/C++/Obj-C Programming (/forumdisplay.php?fid=117)
+--- Thread: Need help dynamicallt allocating a structure!! (/showthread.php?tid=200662)

Need help dynamicallt allocating a structure!! - kl2eativ - 12-08-2009 02:40 PM

Okay i have my structures, And i need to ask the user how many students, and how many
scores so I can then dynamically allocate the array. But i just cannot find the code for this..
Would I be using the "new" ?? Please help.

Code:
// Exercise 10 Course grade.
#include <iostream>
#include <iomanip>
using namespace std;

const int Size = 25; //Array size

struct Class
{
char studName[Size]; // Student name
double quizzes; // Number of quizes
double midTerm; // Midterm test score
double final; //Final test score
double finalScore; // Final score
double finalGrade; //Course grade
};

int main()
{

int grades, numstuds;

//Get the number of students


cout << "How many students are in the class?";
cin >> numstuds;

1 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

//Get the number of quizzes


cout << "How many scores are for each student?";
cin >> grades

//Dynamically allocate memory

* - Etheryte - 12-08-2009 03:36 PM

As far as I understand he's looking for the C++ equivalent for the following (this is in reg C).
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a, *b;
printf("Enter the amount of ... : ");
scanf("%d",&a);
b=(int*)malloc(a*sizeof(int));
return 0;
}

* - closed_my_HF_account - 12-08-2009 03:40 PM

(12-08-2009 03:36 PM)Etheryte Wrote: As far as I understand he's looking for the
C++ equivalent for the following (this is in reg C).
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a, *b;
printf("Enter the amount of ... : ");
scanf("%d",&a);
b=(int*)malloc(a*sizeof(int));
return 0;
}

If that's the case then he could just use that and simply change scanf to std::cin

* - g4143 - 12-08-2009 05:17 PM

2 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

Is this what your looking for: Note I'm not a C++'er so you might want to verify this

Code:
#include <iostream>

struct Class
{
char studName[20]; // Student name
double quizzes; // Number of quizes
double midTerm; // Midterm test score
double final; //Final test score
double finalScore; // Final score
double finalGrade; //Course grade
};

int main()
{
struct Class *cptr = new struct Class[10];//allocate memory for 10 structures
delete [] cptr;
return 0;
}

* - closed_my_HF_account - 12-08-2009 07:41 PM

(12-08-2009 05:17 PM)g4143 Wrote: Is this what your looking for: Note I'm not a
C++'er so you might want to verify this

Code:
<snip>

Seem's OK to me, oh kl2eativ you might want to think about changing the struct name because
Class is so easily confused with the C++ class keyword, just change it to student_classroom or
something. I've slightly editted below so you can see the memory allocation for varification in
the struc (with changed name):

Code:
#include <windows.h>
#include <iostream>

struct student_classroom
{
char studName[20]; // Student name
double quizzes; // Number of quizes
double midTerm; // Midterm test score
double final; //Final test score
double finalScore; // Final score
double finalGrade; //Course grade

3 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

};

int main()
{

struct student_classroom *cptr = new struct student_classroom[10];//allocate


memory for 10 structures

//Get the number of students


for(int i = 0; i < 10; i++)
{std::cout << "\nstudent struct memory allocation at "<<&cptr[i];}

delete [] cptr;
std::cout<<"\nClosing, please wait...\n";
Sleep(8000);
return 0;
}

* - oprichniki - 12-09-2009 12:32 AM

Haven't a clue how to do this, but I'll throw in a wild guess:


Code:
//Assuming the structure's name is studen_classrom:
student_classroom* c = (student_classroom*)malloc
(Size*sizeof(char)+5*sizeof(double));
//do stuff
free(c);

* - kl2eativ - 12-09-2009 12:44 PM

(12-08-2009 07:41 PM)Systemerror Wrote: Seem's OK to me, oh kl2eativ you


might want to think about changing the struct name because Class is so easily
confused with the C++ class keyword, just change it to student_classroom or
something. I've slightly editted below so you can see the memory allocation for
varification in the struc (with changed name):

Code:
#include <windows.h>
#include <iostream>

struct student_classroom
{
char studName[20]; // Student name
double quizzes; // Number of quizes
double midTerm; // Midterm test score
double final; //Final test score
double finalScore; // Final score
double finalGrade; //Course grade
};

4 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

int main()
{

struct student_classroom *cptr = new struct


student_classroom[10];//allocate memory for 10 structures

//Get the number of students


for(int i = 0; i < 10; i++)
{std::cout << "\nstudent struct memory allocation at "<<&cptr[i];}

delete [] cptr;
std::cout<<"\nClosing, please wait...\n";
Sleep(8000);
return 0;
}

Thank you guys for all your help, But why whould you do new struct
student_classroom[10];//allocate memory for 10 structures?? Why only for ten? How about if
there are more students in the classroom?

* - g4143 - 12-09-2009 12:50 PM

(12-09-2009 12:44 PM)kl2eativ Wrote: Thank you guys for all your help, But why
whould you do new struct student_classroom[10];//allocate memory for 10
structures?? Why only for ten? How about if there are more students in the
classroom?

It was just an example, you could replace the value 10 with a integer variable.

* - Etheryte - 12-09-2009 01:00 PM

If I'm not mistaken, you could modify the snippets provided to get what you want.
I don't really speak C++, so excuse me if I screwed up somewhere.
Code:
#include <iostream>

struct Class
{
char studName[20]; // Student name
double quizzes; // Number of quizes
double midTerm; // Midterm test score
double final; //Final test score
double finalScore; // Final score
double finalGrade; //Course grade
};

int main()

5 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

{
int a;
do{
std::cout<<"Enter the amount of ...: ";
std::cin >> a;
}while(a<1||a>30);
struct Class *cptr = new struct Class[a];
return 0;
}

* - kl2eativ - 12-09-2009 01:42 PM

guys, sorry to keep bothering but this is my last HW of the semester and I really need to do it to
maintain my B. why the compiler keeps telling me that I is undeclared at the loop??

Code:
// Exercise 10 Course grade.
#include <iostream>
#include <iomanip>
#include <new>
using namespace std;

const int Size = 25; //Array size

struct student_classroom
{
char studName[Size]; // Student name
double quizzes[]; // Number of quizes
double midTerm; // Midterm test score
double programmingExercises[]; //Programming exersices scores
double final; //Final test score
double finalScore; // Final score
double finalGrade; //Course grade
};

int main()
{
int studnum, scoresnum;
//Ask how many students in the class
cout << "How many students are in the classroom?";
cin >> studnum;

//Allocate memory depending on number of students


struct student_classroom *cptr = new struct student_classroom[studnum];

//Ask how many scores per student


cout << "How many scores per each student?";
cin >> scoresnum;
cout << "Enter the scores for each student";
for (i = 0, i < scoresnum ,i++)
cin >> student_classroom.quizzes[i];

delete [] cptr;

6 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

std::cout<<"\nClosing, please wait...\n";

system ("pause");
return 0;
}

* - Etheryte - 12-09-2009 01:45 PM

Because you're missing the declaration of i in your main.


Code:
int i;
The error tells you what's up, use it.

* - closed_my_HF_account - 12-09-2009 02:40 PM

well if that's the case then declare it! like so:


Code:
for (int i = 0; i < scoresnum ;i++)

* - kl2eativ - 12-09-2009 03:05 PM

This is what I have so far but I think I have many things really wrong. Please take a look.

Code:
// Exercise 10 Course grade.
#include <iostream>
#include <iomanip>
#include <new>
using namespace std;

const int Size = 25; //Array size

//Function prototypes;
void ScoresCalc(double, double);
void FinalCalc(int, char);

struct student_classroom
{
char studName[Size]; // Student name
double quizzes[]; // Number of quizes
double midTerm; // Midterm test score
double programmingExercises[]; //Programming exersices scores
double final; //Final test score
double finalScore; // Final score
double finalGrade; //Course grade
};

int main()
{
int studnum, scoresnum, i;

7 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

//Ask how many students in the class


cout << "How many students are in the classroom?";
cin >> studnum;

//Allocate memory depending on number of students


struct student_classroom *cptr = new struct student_classroom[studnum];

//Ask how many scores per student


cout << "How many scores per each student?";
cin >> scoresnum;
//Get the student ID
cout << "Please enter the student ID number";
for (i = 0; i < scoresnum ;i++)
cin >> student_classroom.studName[i]
//Enter the score for each quiz
cout << "Enter the scores quizes for each student";
cin >> student_classroom.quizzes[i];
//Enter score for each programming exercise
cout << "Enter the programming excercises scores for each student";
cin >> student_classroom.programmingExercices[i];
//Get the score for the midterm
cout << "Please enter the score of the Midterm";
cin >> student_classroom.midTerm[i]

delete [] cptr;
//Function to calculate the scores
ScoresCalc();
FinalCalc();

system ("pause");
return 0;
}

//************************
//Functions definitions *
//************************

void ScoresCalc()
{

* - oprichniki - 12-09-2009 05:19 PM

Well, as a C user I can say that structures don't allow for ambiguous array sizes, unless you
work with pointers.

C++ might have changed that rule, but as far as I know your structure has errors because it
declares arrays without specifying their size.

* - g4143 - 12-09-2009 06:47 PM

Your really display some fundamental problems in your code - Try Googling "Teach yourself C++

8 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

in 21 days" I think its available on the internet..

Code:
// Exercise 10 Course grade.
#include <iostream>
#include <iomanip>
#include <new>
using namespace std;

#define SIZE 25

//Function prototypes;
void ScoresCalc(double, double);
void FinalCalc(int, char);

struct student_classroom
{
char studName[SIZE]; // Student name
double quizzes; // Number of quizes
double midTerm; // Midterm test score
double programmingExercises; //Programming exersices scores
double final; //Final test score
double finalScore; // Final score
double finalGrade; //Course grade
};

int main()
{
int studnum, scoresnum, i;
cout << "How many students are in the classroom?";
cin >> studnum;

struct student_classroom *cptr = new struct student_classroom[studnum];

cout << "How many scores per each student?";


cin >> scoresnum;

cout << "Please enter the student ID number";


for (i = 0; i < scoresnum ;i++)//not really sure where your for statement end
or starts so I just included everything
{
cin >> student_classroom.studName[i]
cout << "Enter the scores quizes for each student";
cin >> student_classroom.quizzes[i];
cout << "Enter the programming excercises scores for each student";
cin >> student_classroom.programmingExercices[i];
cout << "Please enter the score of the Midterm";
cin >> student_classroom.midTerm[i]
}

delete [] cptr;

//*****************************************************************
//your deleting your structures(data) before your using it
//**************************************************************

9 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

ScoresCalc();
FinalCalc();

system ("pause");//very bad programming practice


return 0;
}

//************************
//Functions definitions *
//************************

void ScoresCalc()
{

* - closed_my_HF_account - 12-09-2009 07:19 PM

I agree, when I saw he didn't know how to handle an un-declared varibable compiler error and
then ask what to do I thought exactly the same thing - I think he should get a book and start
from page 1.

* - kl2eativ - 12-11-2009 08:11 PM

guys I really need you help on my last assignment. Here are the instructions

The program should keep a list of scores for a group of students. It should ask the user how
many scores there are and how many students there are. It should then dynamically allocate an
array of structures. Each structure's programmingExercises, and quizzes should point to a
dynamically allocated array that will hold the scores for programmingExercises, and quizzes
respectively.

After the arrays have been dynamically allocated, the program should ask for the student
ID number and all the test scores for each student. The courseGrade should be calculated as
follows:

1. programmingExercises = (Sum of all programming exercise scores / total programming


exercises points) * .75
2. quizzes = Sum of all quiz scores * .10
3. MidTerm = MidTerm exam score * .05
4. final = Final exam score * .10
5. finalScore = 1 + 2 +3 + 4
6. finalGrade = finalScore expressed as a letter grade.

Please give me ideas, I want to have the formulas for my math calculations.

* - closed_my_HF_account - 12-11-2009 08:20 PM

10 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

(12-11-2009 08:11 PM)kl2eativ Wrote: guys I really need you help on my last
assignment. Here are the instructions

The program should keep a list of scores for a group of students. It should ask the
user how many scores there are and how many students there are. It should then
dynamically allocate an array of structures. Each structure's programmingExercises,
and quizzes should point to a dynamically allocated array that will hold the scores
for programmingExercises, and quizzes respectively.

After the arrays have been dynamically allocated, the program should ask for
the student ID number and all the test scores for each student. The courseGrade
should be calculated as follows:

1. programmingExercises = (Sum of all programming exercise scores / total


programming exercises points) * .75
2. quizzes = Sum of all quiz scores * .10
3. MidTerm = MidTerm exam score * .05
4. final = Final exam score * .10
5. finalScore = 1 + 2 +3 + 4
6. finalGrade = finalScore expressed as a letter grade.

Please give me ideas, I want to have the formulas for my math calculations.

We're not here to do your homework for you, you would have learnt how to do this in your class
over time otherwize you wouldn't have been given the exersize, therefore, if you can't do this
yourself then you don't deserve to pass.

* - kl2eativ - 12-11-2009 08:23 PM

(12-11-2009 08:20 PM)Systemerror Wrote: We're not here to do your homework


for you, you would have learnt how to do this in your class over time otherwize you
wouldn't have been given the exersize, therefore, if you can't do this yourself then
you don't deserve to pass.

Well to tell you the truth, i did not learn a thing with this teacher, he really sucks ass, whatever i
learn, i learned it online or the book, not through him. Thanks for the help though.

* - closed_my_HF_account - 12-11-2009 08:26 PM

(12-11-2009 08:23 PM)kl2eativ Wrote: Well to tell you the truth, i did not learn a
thing with this teacher, he really sucks ass, whatever i learn, i learned it online or
the book, not through him. Thanks for the help though.

11 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...

You're only given an assignment (in any topic) if you've learnt everything within it that it entails,
it's more likely that you wasn't paying attention or something along those lines.

12 of 12 December11 2:38 PM

Das könnte Ihnen auch gefallen