943,660 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 989
  • C++ RSS
Oct 28th, 2008
0

pointer structure with member as a pointer

Expand Post »
Hi everyone. I decided to try and learn C++ on my own and i have ran into a problem and i just need slight kick in the butt.

I am just learning structures and i am practicing using some of the examples at the end of the chapter in my book. When i run the code below i get an error while running the program. The error is a windows error and says my program has encountered a problem and needs to terminate. I know the problem is with *student->Tests, but i am not sure what i am doing wrong.

Any help is greatly appreciated!

#include <iostream>
using namespace std;

struct studInfo
{
char Name[31];
short Idnum;
int* Tests;
float avg;
char grade;
};

void main()
{
short numTests, numStudents;
studInfo* student;
int* testScores;

cout << "How many Students? : ";
cin >> numStudents;
cout << "\nHow many test scores? : ";
cin >> numTests;

student = new studInfo[numStudents];
testScores = new int[numTests];

for ( int i = 0; i < numStudents; i++ )
{
cout << "\nEnter ID for student " << (i + 1) << ": ";
cin >> student->Idnum;
for ( int j = 0; j < numTests; j++ )
{
cout << "\nEnter test score " << (j + 1) << " for student " << i + 1 << ": ";
cin >> *student->Tests;

}
}
}
Similar Threads
Reputation Points: 22
Solved Threads: 14
Junior Poster in Training
jbrock31 is offline Offline
73 posts
since Oct 2008
Oct 28th, 2008
0

Re: pointer structure with member as a pointer

Hi
here is what I would change:
Quote ...
struct studInfo
{
char Name[31];
short Idnum;
int Tests;
float avg;
char grade;
};
P.S. A general advice:
Quote ...
int main()
{
....
return 0;
}
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008
Oct 29th, 2008
0

Re: pointer structure with member as a pointer

Click to Expand / Collapse  Quote originally posted by jbrock31 ...
Hi everyone. I decided to try and learn C++ on my own and i have ran into a problem and i just need slight kick in the butt.

I am just learning structures and i am practicing using some of the examples at the end of the chapter in my book. When i run the code below i get an error while running the program. The error is a windows error and says my program has encountered a problem and needs to terminate. I know the problem is with *student->Tests, but i am not sure what i am doing wrong.

Any help is greatly appreciated!

#include <iostream>
using namespace std;

struct studInfo
{
char Name[31];
short Idnum;
int* Tests;
float avg;
char grade;
};

void main()
{
short numTests, numStudents;
studInfo* student;
int* testScores;

cout << "How many Students? : ";
cin >> numStudents;
cout << "\nHow many test scores? : ";
cin >> numTests;

student = new studInfo[numStudents];
testScores = new int[numTests];

for ( int i = 0; i < numStudents; i++ )
{
cout << "\nEnter ID for student " << (i + 1) << ": ";
cin >> student->Idnum;
for ( int j = 0; j < numTests; j++ )
{
cout << "\nEnter test score " << (j + 1) << " for student " << i + 1 << ": ";
cin >> *student->Tests;

}
}
}
Since you are creating a pointer to an array of structs, you must specify the index inside your for loops. So whenever you try to access a record for modification, do it using this syntax.

C++ Syntax (Toggle Plain Text)
  1. cin >> student[i].Idnum;
  2.  
  3. //to access members of the Tests array
  4. cin >> student[i].Tests[j];

Testscores seems to be unreferenced. Instead, I think you are trying to create an array of tests pointed to by a students struct member. To do this, do this instead on the 2nd 'new' keyword line

C++ Syntax (Toggle Plain Text)
  1. for (int i(0); i < iNumStudents; ++i)
  2. student[i].Tests = new int [iNumTests]

This will allow you to access the individual tests within the structure, provided an index. Also, whenever you use the new keyword, you should also use the delete keyword. Include this at the end of your program to prevent memory leaks.

C++ Syntax (Toggle Plain Text)
  1. for (int i(0); i < iNumStudents; ++i)
  2. delete [] student[i].Tests;
  3. delete [] student;
Last edited by skatamatic; Oct 29th, 2008 at 12:07 pm.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Oct 29th, 2008
1

Re: pointer structure with member as a pointer

you did not allocate memory for the int* Tests variable.

the thing is I have a

testScores = new int[numTests];

but you don't use that nowhere

Now, the thing is like this
Reading your program what you actually try to do is iterate through each student and give to him numTests marks. That means the purpose of int* Tests is to point at the start of an array of numTests elements. That means at each iteration of the outer for you need to allocate memory numTests ints in you int* Tests pointer.

Your program should look like this in oder to just work:

struct studInfo
{
char Name[31];
short Idnum;
int* Tests;
float avg;
char grade;
};
C++ Syntax (Toggle Plain Text)
  1. void main()
  2. {
  3. short numTests, numStudents;
  4. studInfo* student;
  5. int* testScores;
  6.  
  7. cout << "How many Students? : ";
  8. cin >> numStudents;
  9. cout << "\nHow many test scores? : ";
  10. cin >> numTests;
  11.  
  12. student = new studInfo[numStudents];
  13.  
  14. for ( int i = 0; i < numStudents; i++ )
  15. {
  16. cout << "\nEnter ID for student " << (i + 1) << ": ";
  17. cin >> student[i].Idnum;
  18.  
  19. testScores = new int[numTests];
  20.  
  21. for ( int j = 0; j < numTests; j++ )
  22. {
  23. cout << "\nEnter test score " << (j + 1) << " for student " << i + 1 << ": ";
  24. //put the result at tre corresponding position in the array
  25. cin >> *(testScores + j); //or //cin >> testScores[j]
  26.  
  27. }
  28.  
  29. student[i].Tests = testScores;
  30. }
  31. }

of course, more problems arrise.
First of all no delete[] are done so you have mem leaks.
Your structure holds a pointer to an array of numTests ints and numTests is a local variable of main. What happens if numTests gets out of scope and you still have allocated studInfo structures? I mean, if you have to iterate through the int* Tests array, how will you know how big it is? Good practice when you hold dinamic arrays in a structure would be to have an int value for each dinamyc array that holds the arrays current size !!
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Oct 29th, 2008
0

Re: pointer structure with member as a pointer

Click to Expand / Collapse  Quote originally posted by kux ...
you did not allocate memory for the int* Tests variable.

the thing is I have a

testScores = new int[numTests];

but you don't use that nowhere

Now, the thing is like this
Reading your program what you actually try to do is iterate through each student and give to him numTests marks. That means the purpose of int* Tests is to point at the start of an array of numTests elements. That means at each iteration of the outer for you need to allocate memory numTests ints in you int* Tests pointer.

Your program should look like this in oder to just work:

struct studInfo
{
char Name[31];
short Idnum;
int* Tests;
float avg;
char grade;
};
C++ Syntax (Toggle Plain Text)
  1. void main()
  2. {
  3. short numTests, numStudents;
  4. studInfo* student;
  5. int* testScores;
  6.  
  7. cout << "How many Students? : ";
  8. cin >> numStudents;
  9. cout << "\nHow many test scores? : ";
  10. cin >> numTests;
  11.  
  12. student = new studInfo[numStudents];
  13.  
  14. for ( int i = 0; i < numStudents; i++ )
  15. {
  16. cout << "\nEnter ID for student " << (i + 1) << ": ";
  17. cin >> student[i].Idnum;
  18.  
  19. testScores = new int[numTests];
  20.  
  21. for ( int j = 0; j < numTests; j++ )
  22. {
  23. cout << "\nEnter test score " << (j + 1) << " for student " << i + 1 << ": ";
  24. //put the result at tre corresponding position in the array
  25. cin >> *(testScores + j); //or //cin >> testScores[j]
  26.  
  27. }
  28.  
  29. student[i].Tests = testScores;
  30. }
  31. }

of course, more problems arrise.
First of all no delete[] are done so you have mem leaks.
Your structure holds a pointer to an array of numTests ints and numTests is a local variable of main. What happens if numTests gets out of scope and you still have allocated studInfo structures? I mean, if you have to iterate through the int* Tests array, how will you know how big it is? Good practice when you hold dinamic arrays in a structure would be to have an int value for each dinamyc array that holds the arrays current size !!
I would avoid using *(testScores + j) to reference a testScores array location. Use an index instead (testScore[i]). Avoid dereferencing when possible, as horrible messes can arrise from it, when simple indexes could clean up the solution entirely.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Oct 30th, 2008
0

Re: pointer structure with member as a pointer

Click to Expand / Collapse  Quote originally posted by skatamatic ...
I would avoid using *(testScores + j) to reference a testScores array location. Use an index instead (testScore[i]). Avoid dereferencing when possible, as horrible messes can arrise from it, when simple indexes could clean up the solution entirely.
well, i wrote the index version too. but the thing is they both mean the same thing... using an index won't protect you from going beyond the array's size...
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Oct 30th, 2008
0

Re: pointer structure with member as a pointer

Click to Expand / Collapse  Quote originally posted by kux ...
well, i wrote the index version too. but the thing is they both mean the same thing... using an index won't protect you from going beyond the array's size...
Thats true. And it's probably pretty clear with only 1 level of indirection. It's when you end up with things like *(*(*(x + i) + j) that you start pulling your hair out.
Reputation Points: 352
Solved Threads: 108
Master Poster
skatamatic is offline Offline
772 posts
since Nov 2007
Oct 30th, 2008
0

Re: pointer structure with member as a pointer

Thank you for the help! I appreciate it!
Reputation Points: 22
Solved Threads: 14
Junior Poster in Training
jbrock31 is offline Offline
73 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to Place text from a textbox into an array?
Next Thread in C++ Forum Timeline: Tick Member Function!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC