| | |
structures containing a pointer to an array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 1
Reputation:
Solved Threads: 0
#include <iostream>
#include <stdio.h>
#include <iomanip>
using namespace std;
struct person
{
char name[15];
int Idnum;
int *testscores;
float average;
char grade;
} *student, freshman;
//////////// function prototype ///////////
void allocate_memory(int number_students , int number_scores);
void get_data(int number_students ,int number_scores);
float validate_score(int score);
void print_results(int number_students ,int number_scores);
void release_memory(int number_students );
////////////////////////////////////////////
int main()
{
int number_students , number_scores;
cout <<"How many students in this class: ";
cin >> number_students;
cout <<"how many scores for each student: ";
cin >> number_scores;
allocate_memory(number_students , number_scores);
get_data(number_students , number_scores);
// average = average_score();
print_results(number_students , number_scores);
release_memory(number_students);
return 0;
} // end od main
////////////////////////////////////////////////
//////// functions definition /////////////////
void allocate_memory(int number_students , int number_scores)
{
student = new person[number_students];
for (int x = 1 ; x <= number_students; x++)
{
student[x].testscores = new int[number_scores];
}
} // end allocate memory
//////////////////////////////////////
void get_data(int number_students ,int number_scores)
{
float total = 0;
int validate = 0;
for (int k = 0; k < number_students ; k++)
{
student[k].average = 0;
cout <<"What is the name of student # " << (k+1) << ": ";
cin >> student[k].name;
cout <<"What is the ID number of " << student[k].name << " : " ;
cin >> student[k].Idnum ;
for (int i=0 ; i < number_scores ; i++)
{
cout <<"What is score # " << (i+1) << " for " << student[k].name << " : " ;
cin >> student[k].testscores[i];
validate = validate_score(student[k].testscores[i]);
if(!validate)
{
i = i - 1;
cout <<"Invalid score value, Re-Enter Score : " << endl;
}
else
{
total = total + student[k].testscores[i];
}
} // end of inner for loop ( looping through scores)
student[k].average = (total / number_scores);
} //end of outer for loop (looping through all students)
} // end of get_data
/////////////////////////////////////
float validate_score(int score)
{
if (score < 0 || score > 100)
{
return 0;
}
else
{
return 1;
}
}// end of validate
//////////////////////////////////////
void print_results(int number_students ,int number_scores)
{
for (int k = 0; k < number_students; k++)
{
for (int j=0 ; j < number_scores ; j++)
{
cout << "using structure variable = " << student[k].testscores[j] << endl;
}
}
}//end of print data
///////////////////////////////////////
void release_memory(int number_students)
{
for (int x = 10 ; x < number_students; x++)
{
delete [] student[x].testscores ;
}
delete [] student;
return;
} // end of release memory
////////////////////////////////////////
sorry for not using tags, I need to learn how to use them. Tha above code suppose to do the followings:
1) ask the user to to select how many students and how many scores to enter for each student.
2) the structure has a pointer to an array where the scores need to be stored for each student.
3) allocate memry for the students and allocate memory for the scores arrays.
4) print out the contents of the arrays.
I do not want to use classes or vector , only memory allcation to the above and basic structure.
When the program reaches the point where to input the first score, it gives an error??? how to allocate memory for the above problem corectly??? please help. I have spent alot of time tweek the code but no use(the code compiles in vc++ without any problem, but fails during running.)
#include <stdio.h>
#include <iomanip>
using namespace std;
struct person
{
char name[15];
int Idnum;
int *testscores;
float average;
char grade;
} *student, freshman;
//////////// function prototype ///////////
void allocate_memory(int number_students , int number_scores);
void get_data(int number_students ,int number_scores);
float validate_score(int score);
void print_results(int number_students ,int number_scores);
void release_memory(int number_students );
////////////////////////////////////////////
int main()
{
int number_students , number_scores;
cout <<"How many students in this class: ";
cin >> number_students;
cout <<"how many scores for each student: ";
cin >> number_scores;
allocate_memory(number_students , number_scores);
get_data(number_students , number_scores);
// average = average_score();
print_results(number_students , number_scores);
release_memory(number_students);
return 0;
} // end od main
////////////////////////////////////////////////
//////// functions definition /////////////////
void allocate_memory(int number_students , int number_scores)
{
student = new person[number_students];
for (int x = 1 ; x <= number_students; x++)
{
student[x].testscores = new int[number_scores];
}
} // end allocate memory
//////////////////////////////////////
void get_data(int number_students ,int number_scores)
{
float total = 0;
int validate = 0;
for (int k = 0; k < number_students ; k++)
{
student[k].average = 0;
cout <<"What is the name of student # " << (k+1) << ": ";
cin >> student[k].name;
cout <<"What is the ID number of " << student[k].name << " : " ;
cin >> student[k].Idnum ;
for (int i=0 ; i < number_scores ; i++)
{
cout <<"What is score # " << (i+1) << " for " << student[k].name << " : " ;
cin >> student[k].testscores[i];
validate = validate_score(student[k].testscores[i]);
if(!validate)
{
i = i - 1;
cout <<"Invalid score value, Re-Enter Score : " << endl;
}
else
{
total = total + student[k].testscores[i];
}
} // end of inner for loop ( looping through scores)
student[k].average = (total / number_scores);
} //end of outer for loop (looping through all students)
} // end of get_data
/////////////////////////////////////
float validate_score(int score)
{
if (score < 0 || score > 100)
{
return 0;
}
else
{
return 1;
}
}// end of validate
//////////////////////////////////////
void print_results(int number_students ,int number_scores)
{
for (int k = 0; k < number_students; k++)
{
for (int j=0 ; j < number_scores ; j++)
{
cout << "using structure variable = " << student[k].testscores[j] << endl;
}
}
}//end of print data
///////////////////////////////////////
void release_memory(int number_students)
{
for (int x = 10 ; x < number_students; x++)
{
delete [] student[x].testscores ;
}
delete [] student;
return;
} // end of release memory
////////////////////////////////////////
sorry for not using tags, I need to learn how to use them. Tha above code suppose to do the followings:
1) ask the user to to select how many students and how many scores to enter for each student.
2) the structure has a pointer to an array where the scores need to be stored for each student.
3) allocate memry for the students and allocate memory for the scores arrays.
4) print out the contents of the arrays.
I do not want to use classes or vector , only memory allcation to the above and basic structure.
When the program reaches the point where to input the first score, it gives an error??? how to allocate memory for the above problem corectly??? please help. I have spent alot of time tweek the code but no use(the code compiles in vc++ without any problem, but fails during running.)
•
•
Join Date: Nov 2004
Posts: 108
Reputation:
Solved Threads: 3
Well your first problem is that you didn't use code tags
Read This
Read This
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Server: irc.daniweb.com
Channel: #C++
Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Using global variables makes your code harder to follow. But I immediately found this error:
Why are you starting with 1 and ending at number_students when the array only goes from 0 to number_students - 1?
This is suspicious as well:
Why are you starting with 10?
C++ Syntax (Toggle Plain Text)
for (int x = 1 ; x <= number_students; x++)
This is suspicious as well:
C++ Syntax (Toggle Plain Text)
for (int x = 10 ; x < number_students; x++)
I'm here to prove you wrong.
As Narue so kindly pointed out, you went past the array, that gets you into garbage values. There is also a casting problem with the variable validate. You may want to add checks for non-numeric input, or you get into strange loops! I don't have VC++ up, so I tested the corrected code on Dev-C++
[php]
#include <iostream>
//#include <stdio.h>
//#include <iomanip>
using namespace std;
struct person
{
char name[15];
int Idnum;
int *testscores;
float average;
char grade;
} *student, freshman;
//////////// function prototype ///////////
void allocate_memory(int number_students , int number_scores);
void get_data(int number_students ,int number_scores);
float validate_score(int score);
void print_results(int number_students ,int number_scores);
void release_memory(int number_students );
////////////////////////////////////////////
int main()
{
int number_students , number_scores;
cout <<"How many students in this class: ";
cin >> number_students; // needs check for non-numeric input
cout <<"how many scores for each student: ";
cin >> number_scores; // needs check for non-numeric input
allocate_memory(number_students , number_scores);
get_data(number_students , number_scores);
// average = average_score();
print_results(number_students , number_scores);
release_memory(number_students);
system("PAUSE"); // wait, needed for Dev-C++
return 0;
} // end of main
////////////////////////////////////////////////
//////// functions definition /////////////////
void allocate_memory(int number_students , int number_scores)
{
student = new person[number_students];
// zero base this, array goes only to number_students - 1 !!!!!
for (int x = 0 ; x < number_students; x++)
{
student[x].testscores = new int[number_scores];
}
} // end allocate memory
//////////////////////////////////////
void get_data(int number_students ,int number_scores)
{
float total = 0;
int validate = 0;
for (int k = 0; k < number_students; k++)
{
student[k].average = 0;
cout << "What is the name of student # " << (k+1) << ": ";
cin >> student[k].name;
cout << "What is the ID number of " << student[k].name << " : " ;
cin >> student[k].Idnum ;
for (int i = 0 ; i < number_scores; i++)
{
cout <<"What is score # " << (i+1) << " for " << student[k].name << " : " ;
cin >> student[k].testscores[i];
validate = (int)validate_score(student[k].testscores[i]); // cast to int from float!!!!
if (!validate)
{
i = i - 1;
cout <<"Invalid score value, Re-Enter Score : " << endl;
}
else
{
total = total + student[k].testscores[i];
}
} // end of inner for loop ( looping through scores)
student[k].average = (total / number_scores);
} //end of outer for loop (looping through all students)
} // end of get_data
/////////////////////////////////////
float validate_score(int score)
{
if (score < 0 || score > 100)
{
return 0;
}
else
{
return 1;
}
}// end of validate
//////////////////////////////////////
void print_results(int number_students ,int number_scores)
{
for (int k = 0; k < number_students; k++)
{
for (int j = 0 ; j < number_scores; j++)
{
cout << "using structure variable = " << student[k].testscores[j] << endl;
}
}
}//end of print data
///////////////////////////////////////
void release_memory(int number_students)
{
for (int x = 0 ; x < number_students; x++) // zero base this!!!!
{
delete [] student[x].testscores ;
}
delete [] student;
return;
} // end of release memory
////////////////////////////////////////
[/php]
Now finish your averaging and grade math etc.
[php]
#include <iostream>
//#include <stdio.h>
//#include <iomanip>
using namespace std;
struct person
{
char name[15];
int Idnum;
int *testscores;
float average;
char grade;
} *student, freshman;
//////////// function prototype ///////////
void allocate_memory(int number_students , int number_scores);
void get_data(int number_students ,int number_scores);
float validate_score(int score);
void print_results(int number_students ,int number_scores);
void release_memory(int number_students );
////////////////////////////////////////////
int main()
{
int number_students , number_scores;
cout <<"How many students in this class: ";
cin >> number_students; // needs check for non-numeric input
cout <<"how many scores for each student: ";
cin >> number_scores; // needs check for non-numeric input
allocate_memory(number_students , number_scores);
get_data(number_students , number_scores);
// average = average_score();
print_results(number_students , number_scores);
release_memory(number_students);
system("PAUSE"); // wait, needed for Dev-C++
return 0;
} // end of main
////////////////////////////////////////////////
//////// functions definition /////////////////
void allocate_memory(int number_students , int number_scores)
{
student = new person[number_students];
// zero base this, array goes only to number_students - 1 !!!!!
for (int x = 0 ; x < number_students; x++)
{
student[x].testscores = new int[number_scores];
}
} // end allocate memory
//////////////////////////////////////
void get_data(int number_students ,int number_scores)
{
float total = 0;
int validate = 0;
for (int k = 0; k < number_students; k++)
{
student[k].average = 0;
cout << "What is the name of student # " << (k+1) << ": ";
cin >> student[k].name;
cout << "What is the ID number of " << student[k].name << " : " ;
cin >> student[k].Idnum ;
for (int i = 0 ; i < number_scores; i++)
{
cout <<"What is score # " << (i+1) << " for " << student[k].name << " : " ;
cin >> student[k].testscores[i];
validate = (int)validate_score(student[k].testscores[i]); // cast to int from float!!!!
if (!validate)
{
i = i - 1;
cout <<"Invalid score value, Re-Enter Score : " << endl;
}
else
{
total = total + student[k].testscores[i];
}
} // end of inner for loop ( looping through scores)
student[k].average = (total / number_scores);
} //end of outer for loop (looping through all students)
} // end of get_data
/////////////////////////////////////
float validate_score(int score)
{
if (score < 0 || score > 100)
{
return 0;
}
else
{
return 1;
}
}// end of validate
//////////////////////////////////////
void print_results(int number_students ,int number_scores)
{
for (int k = 0; k < number_students; k++)
{
for (int j = 0 ; j < number_scores; j++)
{
cout << "using structure variable = " << student[k].testscores[j] << endl;
}
}
}//end of print data
///////////////////////////////////////
void release_memory(int number_students)
{
for (int x = 0 ; x < number_students; x++) // zero base this!!!!
{
delete [] student[x].testscores ;
}
delete [] student;
return;
} // end of release memory
////////////////////////////////////////
[/php]
Now finish your averaging and grade math etc.
May 'the Google' be with you!
![]() |
Similar Threads
- Quick Question: Pointer to a 2D array (C++)
- Passing a pointer to an array. (C)
- Pointer to array (C++)
- pointer and array arithmetic (C)
- Declaration of dynamic pointer array puzzle. (C)
Other Threads in the C++ Forum
- Previous Thread: reading binary image using C/C++
- Next Thread: 1 bit adder
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






