Need help w/code error

This is a discussion on Need help w/code error within the C++ Programming forums, part of the General Programming Boards category; When I run this code, I get the prompt asking if I want to add a new record, I choose ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    Need help w/code error

    When I run this code, I get the prompt asking if I want to add a new record, I choose 'y', then I get an executable error. Can anyone tell me where the problem is?

    Code:
    //struct.h
    #ifndef _STRUCTUR_H
    #define _STRUCTUR_H
    class customerData{
    public:
    //	customerData();
    private:
    	char firstName[20];
    	char lastName[20];
    	char address[50];
    	char city[50];
    	char state[50];
    	int zip;
    	int account;
    	int pin;
    public:
    	customerData* listArray [5];
    	void addRecord();
    
    	void displayRecord (int, customerData mlRec, customerData rec);
    };
    
    void customerData::addRecord()
    {
    
    		int index = 0;
    		customerData& rec = *listArray[index];
    		
    		for (int i=0; i<5; i++)
    		listArray [i] = new customerData;
    		
    
    		cout << "First name: ";
    		cin.getline (rec.firstName, sizeof(rec.firstName) -1);
    		cout << "Last name: ";
    		cin.getline (rec.lastName, sizeof (rec.lastName) -1);
    		cout << "Address: ";
    		cin.getline(rec.address, sizeof (rec.address) -1);
    		cout << "City: ";
    		cin.getline (rec.city, sizeof (rec.city)-1);
    		cout << "State: ";
    		cin.getline (rec.state, sizeof (rec.state)-1);
    		char buff [10];
    		char buff2[10];
    		char buff3[10];
    		cout << "Zip code: ";
    		cin.getline ( buff, sizeof(buff) -1);
    		rec.zip=atoi(buff);
    		cout << "Account number: ";
    		//cin>>rec.account;
    		cin.getline(buff2, sizeof(buff2) -1);
    		rec.account=atoi(buff2);
    		cout << "PIN number: ";
    		//cin>>rec.pin;
    		cin.getline(buff3, sizeof(buff3) -1);
    		rec.pin=atoi(buff3);
    		index++;
    		cout << endl;
    	
    }
    
    void customerData::displayRecord (int num, customerData mlRec, customerData rec)
    {
    	for (int i=0; i<sizeof listArray[i]; i++)
    		rec.displayRecord (i, *listArray[i], rec);
    
    	cout << "Record " << (num + 1) << " : " << endl;
    	cout << "Name:  " << mlRec.firstName << " " ;
    	cout << mlRec.lastName;
    	cout << endl;
    	cout << "Address: "<< mlRec.address << " " ;
    	cout << endl << "            " ;
    	cout << mlRec.city << " , " << mlRec.state << "  " << mlRec.zip << endl;
    	cout << "Account: "<< mlRec.account <<  endl;
    	cout << "PIN: " << mlRec.pin << endl;
    }
    
    #endif
    
    //struct.cpp
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include "structur.h"
    
    
    int main (int, char**)
    {
    	
    	customerData data;
    
    	char response;
    	cout << "Do you want to add a customer, y/n? " ;
    	cin>>response;	
    
    
    	if (response == 'y'){
    	data.addRecord();
    	}
    	else{}
    
    	return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Your debugger can. Try following the code through until you find the point where it looks like it goes wrong. If you don't have a debugger, use the old fashioned method of simply cout'ing some messages like "checkpoint 1, all ok" etc.


    >>customerData& rec = *listArray[index];
    This isn't good. A reference variable cannot be reassigned to, so there isn't a valid use for it in your case. Besides, when you dereference the array the first time though the function, where do you think you'll end up? Could this be your problem....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks Hammer.
    I do have debugger, but don't know how to use it.


    >>customerData& rec = *listArray[index];
    How do you suggest I change this?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by LouB

    >>customerData& rec = *listArray[index];
    How do you suggest I change this?
    Just use listArray directly, something like

    listArray[index]->firstName
    etc
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks Hammer. I think that will work...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21