Beruflich Dokumente
Kultur Dokumente
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;
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()
{
1 of 12 December11 2:38 PM
Need help dynamicallt allocating a structure!! - Printable Version http://webcache.googleusercontent.com/search?q=cache:I5Gz...
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;
}
(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
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;
}
(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()
{
delete [] cptr;
std::cout<<"\nClosing, please wait...\n";
Sleep(8000);
return 0;
}
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()
{
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?
(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.
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;
}
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;
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;
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...
system ("pause");
return 0;
}
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;
//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...
delete [] cptr;
//Function to calculate the scores
ScoresCalc();
FinalCalc();
system ("pause");
return 0;
}
//************************
//Functions definitions *
//************************
void ScoresCalc()
{
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.
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...
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;
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();
//************************
//Functions definitions *
//************************
void ScoresCalc()
{
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.
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:
Please give me ideas, I want to have the formulas for my math calculations.
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:
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.
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.
(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