Thread: error w/array

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

    error w/array

    What is wrong with this code?? I'm having trouble getting the FirstName array to read in correctly, I'm not sure how to read the array into the array.

    Code:
    ifstream custFile;
    custFile.open("bank2.txt");
    if (custFile.fail())
    {
    cout << "Unable to access bank records" << endl;
    exit(1);
    };
    
    ofstream custUpdate;
    custUpdate.open("recordUpdate.txt");
    if (custUpdate.fail())
    {
    cout << "Error opening outfile" << endl;
    exit(1);
    };
    
    custFile>>numrecs;
    cust CustArray[50];
    
    char FirstName[25];
    int    ChkId;
    double ChkBal;
    double SavBal;
    int PIN_num;
    
    int x=0;
    
    while (cust>>FirstName>>ChkId>>ChkBal>>SavBal>>PIN_num)
    {
    custArray[x].setFirstNameId(FirstName[x]);
    custArray[x].setChkActId(ChkId[x]);
    custArray[x].setChkAcctBal(ChkBal[x]);
    custArray[x].setSavAcctBal(SavBal[x]);
    custArray[x].setPIN(PIN_num[x]);
    x++;
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use !streamName (or streamName.is_open()) to check whether file is open. fail() won't do the trick, I'm pretty sure.

    I presume cust to be a user defined type with data members of:

    char FirstName[25];
    int ChkId;
    double ChkBal;
    double SavBal;
    int PIN_num;

    The task as I interpret it is to read customer data from a file to fill an array of cust in the program.

    The file apparetly contains how many cust records are in the file, but you have chosen to ignore that information and made the array of cust a fixed size of 50. You could use the information read in to dynamically create the array of cust to the exact size of numrecs if you want.

    You should use custFile, not cust to read in the information from the file in the while loop.

    In the set function, the arguments should just be FirstName or ChkId or PIN_num or whatever, so drop the terminal [x] from the parameter.

    Keep your fingers crossed I haven't overlooked anything

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks for your reply Elad...


    I presume cust to be a user defined type with data members of:
    Yes, this is correct.


    The task as I interpret it is to read customer data from a file to fill an array of cust in the program.
    correct again.


    The file apparetly contains how many cust records are in the file, but you have chosen to ignore that information and made the array of cust a fixed size of 50. You could use the information read in to dynamically create the array of cust to the exact size of numrecs if you want.
    I'm trying to figure out how to do that...


    You should use custFile, not cust to read in the information from the file in the while loop.
    This was just a typo on my part, I am using custFile.


    In the set function, the arguments should just be FirstName or ChkId or PIN_num or whatever, so drop the terminal [x] from the parameter.
    Ok, I'm having a hard time figuring out how to do this. I can't just drop the [x], I need to revise the code. Can you give me an example??

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    74
    Ok guys, I'm still having trouble here. I'm trying to make firstName an array within the custArray. I want to open the file bank2.txt, read the data into the array so I can make changes, then save it into custUpdate.txt. The int's and doubles are working okay. It's the name array I'm having trouble with. I'll want to add last name, address, etc too once I figure this out. I tried Elad's suggestion of:

    In the set function, the arguments should just be FirstName or ChkId or PIN_num or whatever, so drop the terminal [x] from the parameter.
    ...but I just can't figure it out.

    I'm still working off pretty much the same code as in my first post. Please help!!

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    74
    ..Should I be using a pointer???

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    74
    I"m making some progress I think. I believe I should be reading the bank2.txt file into the array like this:

    Code:
    	int numrecs = 0;
    	custFile >> numrecs;
    
    	cust custArray[50];
    	
    	char	FirstName[25][80];
    	int    ChkId;
    	double ChkBal;
    	double SavBal;
    	int    PIN_num;
    
    
    	int x = 0; 
        
    	while(!custFile.eof()){
    	
    	custFile>>FirstName[x]>>ChkId>>ChkBal>>SavBal>>PIN_num;
    	
    
    	x++;
    	}
    But when i try to run the program I'm getting a blank prompt. Any ideas??
    Last edited by LouB; 10-19-2003 at 07:35 PM.

Popular pages Recent additions subscribe to a feed