Results 1 to 13 of 13

Thread: Pointer Problem

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    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?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    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;
    }

  3. #3
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    You forgot to delete the pointer.

    delete [] paPeople ;

    And what does this code do?

    Code:
    if (paPeople != NULL){
    exit(0);
    }

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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."
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    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.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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);
    
    	//...
        }
    
        //...
    }
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  12. #12
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    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.

  13. #13

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width