|
-
Jan 21st, 2003, 12:47 AM
#1
Thread Starter
Stuck in the 80s
Pointer Problem
I wrote up this code for an assignment, but...I'm stuck:
Code:
#include <iostream>
#include <iomanip>
#include "people.h"
using namespace std;
int main(int argc, char *argv[]) {
PEOPLE *paPeople = NULL;
int iNumToEnter = 0;
cout << "Number of people to enter: ";
cin >> iNumToEnter;
if (paPeople != NULL) {
exit(0);
}
paPeople = new PEOPLE[iNumToEnter];
for (int i = 0; i < iNumToEnter; i++) {
cout << "Person #" << (i + 1) << "'s Name: ";
cin.ignore();
cin >> paPeople->szName;
cout << "Age of " << paPeople->szName << ": ";
cin >> paPeople->age;
}
system("CLS");
//SHWINK, problem here:
for (int i = 0; i < iNumToEnter; i++) {
cout << paPeople->szName << endl;
}
return 0;
}
Any ideas?
-
Jan 21st, 2003, 02:49 AM
#2
Hyperactive Member
Code:
#include <iostream>
#include <iomanip>
#include "people.h"
using namespace std;
int main(int argc, char *argv[]) {
PEOPLE *paPeople = NULL;
int iNumToEnter = 0;
cout << "Number of people to enter: ";
cin >> iNumToEnter;
if (paPeople != NULL) {
exit(0);
}
paPeople = new PEOPLE[iNumToEnter];
for (int i = 0; i < iNumToEnter; i++) {
cout << "Person #" << (i + 1) << "'s Name: ";
cin.ignore();
cin >> paPeople[i].szName;
cout << "Age of " << paPeople->szName << ": ";
cin >> paPeople[i].age;
}
system("CLS");
//SHWINK, problem here:
for (int i = 0; i < iNumToEnter; i++) {
cout << paPeople[i].szName << endl;
}
return 0;
}
-
Jan 21st, 2003, 04:53 AM
#3
Hyperactive Member
You forgot to delete the pointer.
delete [] paPeople ;
And what does this code do?
Code:
if (paPeople != NULL){
exit(0);
}
-
Jan 21st, 2003, 11:13 AM
#4
Thread Starter
Stuck in the 80s
Originally posted by transcendental
And what does this code do?
Code:
if (paPeople != NULL){
exit(0);
}
Hell if I know. Just something my teacher insists on us doing. Making sure the pointer is NULL before and after it's "newed."
-
Jan 21st, 2003, 11:49 AM
#5
Your teacher needs a good spanking. Making sure it's not NULL after allocating is good. Making sure it's NULL before allocating is a stupid waste.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 21st, 2003, 04:04 PM
#6
Thread Starter
Stuck in the 80s
Originally posted by CornedBee
Your teacher needs a good spanking. Making sure it's not NULL after allocating is good. Making sure it's NULL before allocating is a stupid waste.
After he assigns me a grade at the end of the semester, I'll be sure to inform him of that.
I'll ask him and see what his reasoning is, though.
-
Jan 22nd, 2003, 01:10 AM
#7
Hyperactive Member
It occurred to me it may be better for you to use this form, so that it is clear to you that you are actually using a pointer (which is to be deleted later.)
Use
(paPeople+i)->blahblah
instead of
paPeople[i].blahblah
Last edited by transcendental; Jan 22nd, 2003 at 01:17 AM.
-
Jan 22nd, 2003, 01:28 PM
#8
transcendental analytic
arrays are pointers
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 22nd, 2003, 07:36 PM
#9
Thread Starter
Stuck in the 80s
Now I'm stuck with this:
Code:
int main() {
PEOPLE *paPeople[100] = {NULL};
int iNumToEnter = 0;
//...
for (int i = 0; i < iNumToEnter; i++) {
paPeople[i] = new PEOPLE;
//...
cout << "Person #" << (i + 1) << "'s Name: ";
cin.ignore();
cin.getline(paPeople[i].szName, 25);
//...
}
//...
}
-
Jan 23rd, 2003, 02:19 AM
#10
transcendental analytic
paPeople[i] is a pointer to PEOPLE, thus use select dereferenced pointer -> instead of .
why do you need indirection here anyway?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 23rd, 2003, 04:20 AM
#11
Why is the class called PEOPLE when each instance represents a single person? Just wondering...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 23rd, 2003, 08:40 AM
#12
Hyperactive Member
Different from your first posting, now you are using an array of pointers, instead of pointer to an array.
Originally posted by The Hobo
Now I'm stuck with this:
Code:
int main() {
PEOPLE *paPeople[100] = {NULL};
int iNumToEnter = 0;
//...
for (int i = 0; i < iNumToEnter; i++) {
paPeople[i] = new PEOPLE;
//...
cout << "Person #" << (i + 1) << "'s Name: ";
cin.ignore();
cin.getline(paPeople[i].szName, 25);
//...
}
//...
}
So you should do it this way
Code:
int main() {
PEOPLE *paPeople[100] = {NULL};
int iNumToEnter = 0;
//...
for (int i = 0; i < iNumToEnter; i++) {
paPeople[i] = new PEOPLE;
//...
cout << "Person #" << (i + 1) << "'s Name: ";
cin.ignore();
cin.getline(paPeople[i]->szName, 25);
//...
}
//...
}
I hope you are not confused.
-
Jan 23rd, 2003, 12:31 PM
#13
Thread Starter
Stuck in the 80s
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|