structures containing a pointer to an array

Reply

Join Date: Feb 2005
Posts: 1
Reputation: ah01 is an unknown quantity at this point 
Solved Threads: 0
ah01 ah01 is offline Offline
Newbie Poster

structures containing a pointer to an array

 
0
  #1
Feb 15th, 2005
#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.)
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: structures containing a pointer to an array

 
0
  #2
Feb 15th, 2005
Well your first problem is that you didn't use code tags
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: structures containing a pointer to an array

 
0
  #3
Feb 15th, 2005
Using global variables makes your code harder to follow. But I immediately found this error:
  1. for (int x = 1 ; x <= number_students; x++)
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:
  1. for (int x = 10 ; x < number_students; x++)
Why are you starting with 10?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,870
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: structures containing a pointer to an array

 
0
  #4
Feb 15th, 2005
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC