Thread: accessing array of structs values

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Question accessing array of structs values

    Ok, after much help from everyone at this site, I've run upon my newest problem using an array of structs.

    I successfully get the data loaded into the array, map it correctly and can call the information from the struct loaded in the subscript of the array.

    However, when I try to use the data I store in a very robust array of struct, I appear to just get memory addresses, not the values. I've tried about 30 combinations of calls to get the results, which I know are there, because as a process the data and its being loaded into the structs I see it on screen. Am I not getting to the proper location of the array or am I just calling a memory address not the value. I've attached my code, thanks as always for looking.

    Code:
    #include <fstream.h>
    #include "string.h"
    
    //define struct
    struct partnerInfo
    {
    	char partnerId [11];
    	int casecount;
    	int wavenumber;
    	int starttime;
    	int endtime;
    	int station;
    	char date [11];
    
    };
    
    int main ()
    {
    	//list variables for input record type
    	char recType;
    	char date [11];
    	float hours;
    	float minutes;
    	float seconds;
    	char partnerName [30];
    	char partnerId [11];
    	int stationId;
    	char eventType [30];
    	int waveNumber;
    	char tableName [11];
    	char tableId [11];
    	int count = 0;
    	int count2 =0;
    	int index;
    	int totalTime = 0;
    	int MasterCount = 0;
    	int mcCount3 = 0;
    	
    
    	//open file for input and grab first record
    /*	ifstream fin;
    	fin.open("C:\\data\\MoLic2.txt");
    	if (fin.fail())
    	{
    		cout <<"file open failed!"<<endl;
    	
    	} */
    	ofstream fout;
    	fout.open("C:\\palletperf.txt", ios::app);
    	if (fout.fail())
    	{
    		cout <<"FAILED TO OPEN OUTPUT FILE"<<endl;
    	} 
    	else
    	{
    		
    	/*	do
    		{
    		fin >> recType;
    		fin >> date;
    		fin >> hours;
    		fin >> minutes;
    		fin >> seconds;
    		fin >> partnerName;
    		fin >> partnerId;
    		fin >> stationId;
    		fin.ignore();
    		fin.getline(eventType, 30);
    		
    
    		//declare array of strings for station to partnerId reference
    		char stations [20] [11];
    		
    		//determine recType and populate array if necessary
    		
    		if (recType == 'P')
    		{
    			strcpy( stations[stationId], partnerId ); //set element equal to partnerId
    			cout <<stations[stationId]<<endl; //display the element just set
    		}
    		else if (recType == 'L')
    		{
    			
    			fout <<stations [stationId]<<" "<<partnerId<<" "<<stationId<<" "<<hours<<" "
    				 <<minutes<<" "<<seconds<<" "<<date<<" "<<recType<<" "<<eventType<<endl;
    			cout <<"record written"<<endl;
    		}
    		}while (!fin.eof());
    		fin.close();
    		fout.close(); */  
    		
    		//open partnerlist.txt and palletperf.txt compare strings to match name with records
    		
    		ifstream fin1;
    		fin1.open("C:\\palletperf.txt", ios::eofbit);
    		if (fin1.fail())
    		{
    
    			cout <<"failed to open palletperf.txt";
    		}
    		
    		ifstream fin2;
    		fin2.open("C:\\partnerlist.txt");
    		if (fin2.fail())
    		{
    			cout <<"failed to open partnerlist.txt";
    		}
    		
    		ofstream fout2;
    		fout2.open("C:\\palletresults.txt", ios::app);
    		//input partners ids into array table for binary search
    		char partnerTable [300] [11];
    		do 
    		{
    			fin2>>tableName;
    			fin2>>partnerTable[count];
    		//	cout <<partnerTable[count]<<endl;
    			count++;
    		}while (!fin2.eof());
    	
    
    		//load struct values
    		partnerInfo partnerList [300] [21] ;
    		
    		do
    		{
    		fin1>>partnerId;
    		fin1>>waveNumber;
    		fin1>>stationId;
    		fin1>>hours;
    		fin1>>minutes;
    		fin1>>seconds;
    		fin1>>date;
    		fin1>>recType;
    		fin1>>eventType;
    
    		//run linear search on partnerTable array to find index point for struct array
    		while (count2 <100)
    		{
    			if ((strcmp(partnerTable[count2], partnerId)==0))
    			{
    				index = count2;
    			}
    			count2++;
    		};
    		//calculate total seconds
    		totalTime = (hours*3600)+(minutes*60)+seconds;
    		cout <<MasterCount<<endl;
    
    		if (MasterCount<1)
    		{
    			int mcCount1 = 0;
    			int mcCount2 = 0;
    			do
    			{
    				do
    				{
    		//		cout << "setting counts to zero"<<endl;
    				partnerList[mcCount1] [mcCount2].casecount =0;
    				partnerList[mcCount1] [mcCount2].starttime =0;
    				partnerList[mcCount1] [mcCount2].endtime =0;
    				mcCount2++;
    				}while (mcCount2<21);
    				mcCount2 = 0;
    				mcCount1++;
    			}while ((mcCount1 <300));
    		}
    
    		//determine partner to add to list array of struct for wavelist
    		strcpy(partnerList[count2] [waveNumber].partnerId, partnerId);
    		partnerList[count2] [waveNumber].station = stationId;
    		partnerList[count2] [waveNumber].wavenumber = waveNumber;
    		strcpy(partnerList[count2] [waveNumber].date, date);
    		partnerList[count2] [waveNumber].casecount++;
    		//determine if this is first record for wave
    		if (partnerList[count2] [waveNumber].starttime==0)
    		{
    			partnerList[count2] [waveNumber].starttime = totalTime;
    		}
    		partnerList[count2] [waveNumber].endtime = totalTime;
    
    		MasterCount++;
    	
    
    		//test only display results
    /*		cout<<partnerList[count2] [waveNumber].partnerId<<" "
    			<<partnerList[count2] [waveNumber].wavenumber<<" "
    			<<partnerList[count2] [waveNumber].station<<" "
    			<<partnerList[count2] [waveNumber].casecount<<" "
    			<<partnerList[count2] [waveNumber].starttime<<" "
    			<<partnerList[count2] [waveNumber].endtime<<" "
    			<<partnerList[count2] [waveNumber].date<<endl; */
    
    		}while (!fin1.eof());
    		
    		
    		//close all output and input files
    	//	fin.close();
    		fin1.close();
    		fin2.close();
    		 
    		 
    		//filter through arrays of data and calculate percentages
    		do
    		{
    			//open output file for end results to load listbox
    			ofstream fout3;
    			fout3.open("C:\\palletperfList.txt", ios::app);
    			int mcCount4 = 0;
    			int totalSec = 0;
    			int totalHrs = 0;
    			int totalCss = 0;
    			int casperHr = 0;
    			int percent  = 0;
    			do
    			{
    				//input array values and then make calculations	
    				totalSec = partnerList[mcCount3] [mcCount4].endtime - partnerList[mcCount3] [mcCount4].starttime;
    				totalHrs = totalSec/3600;
    				totalCss = partnerList[mcCount3] [mcCount4].casecount;
    		//		casperHr = totalCss/ totalHrs;
    				percent  = (casperHr / 300) * 100;
    				cout <<partnerList[mcCount3] [mcCount4].partnerId<<" "
    					 <<partnerList[mcCount3] [mcCount4].wavenumber<<" "
    					 <<partnerList[mcCount3] [mcCount4].station<<" "
    					 <<casperHr<<" "<<percent<<" "
    					 <<partnerList[mcCount3] [mcCount4].date<<endl;
    				mcCount4++;
    			}while (mcCount4<21);
    			mcCount4 = 0;
    			mcCount3++;
    		}while ((mcCount3 <300)); 
    	
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I don't have time to go through and analyze the code (speaking of which, thank you very much for using code tags), but by what you're describing it sounds like you're referencing a pointer incorrectly. Pointers store the memory address of the first piece of data, so if you don't reference them correctly, instead of getting the value at that location, you get the address of that location.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Give us a clue as to exactly which line is giving incorrect output...
    Also, post a sample of the input data, so we can test it easier.
    Also also, posting code with half of it commented out makes it real hard for us to work with. You need to strip it down, and post only what you think we need to see.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    164
    I see your point Hammer.

    This is the specific portion of code that is confusing me:
    Code:
    0003175602 4 0 3847 36467 38892 2004-07-02
    //filter through arrays of data and calculate percentages
    		do
    		{
    			//open output file for end results to load listbox
    			ofstream fout3;
    			fout3.open("C:\\palletperfList.txt", ios::app);
    			int mcCount4 = 0;
    			int totalSec = 0;
    			int totalHrs = 0;
    			int totalCss = 0;
    			int casperHr = 0;
    			int percent  = 0;
    			do
    			{
    				//input array values and then make calculations	
    				totalSec = partnerList[mcCount3] [mcCount4].endtime - partnerList[mcCount3] [mcCount4].starttime;
    				totalHrs = totalSec/3600;
    				totalCss = partnerList[mcCount3] [mcCount4].casecount;
    				casperHr = totalCss/ totalHrs;
    				percent  = (casperHr / 300) * 100;
    				cout <<partnerList[mcCount3] [mcCount4].partnerId<<" "
    					 <<partnerList[mcCount3] [mcCount4].wavenumber<<" "
    					 <<partnerList[mcCount3] [mcCount4].station<<" "
    					 <<casperHr<<" "<<percent<<" "
    					 <<partnerList[mcCount3] [mcCount4].date<<endl; 
    				mcCount4++;
    			}while (mcCount4<21);
    			mcCount4 = 0;
    			mcCount3++;
    		}while ((mcCount3 <300));
    The input is already loaded into the struct in multiple array subscripts, but this is a sample of what is loaded into the struct, and the print to screen I use to test and make sure its actually getting loaded:
    Code:
    0003175602 4 0 3847 36467 38892 2004-07-02
    I apologize for the excessive amounts of commented out code, I've been doing alot of testing trying to find my problem.

    I think the first reply is hitting close to the issue, about the pointers but I am not as familiar with pointers. However, all the books I've been reading on accessing structs and arrays data has not used reference to using pointer type syntax such as the '&' to retrieve the data. the reading implies that the subscript location or address when called defaults the value at that address not the address in memory itself.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Have you tried just outputting the contents of all the structs onto the screen? And what do you mean by you're displaying everything on the screen as it's loaded - are you displaying what's inside the struct, or what you're reading from the file?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    164
    I'm outputing on screen whats loaded into the struct. I have attempted to output the structs on screen but have not been overly successful. Which I know is part of the problem, my lack of knowledge with arrays and structs.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I have attempted to output the structs on screen but have not been overly successful.
    Does that mean what came out of the struct wasn't what you expected, or does it mean that you couldn't figure out how to?
    switch(your answer)
    {
    case option 1:
    That's probably your problem!
    case option 2:
    Post the code you tried using to output the structs so we can fix it, because we need to know for sure that the data in the structs is fine.
    }

    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    164
    The output to screen is from this section of code which seems to tell me the information is being loaded into the struct.

    Code:
    //determine partner to add to list array of struct for wavelist
    		strcpy(partnerList[count2] [waveNumber].partnerId, partnerId);
    		partnerList[count2] [waveNumber].station = stationId;
    		partnerList[count2] [waveNumber].wavenumber = waveNumber;
    		strcpy(partnerList[count2] [waveNumber].date, date);
    		partnerList[count2] [waveNumber].casecount++;
    		//determine if this is first record for wave
    		if (partnerList[count2] [waveNumber].starttime==0)
    		{
    			partnerList[count2] [waveNumber].starttime = totalTime;
    		}
    		partnerList[count2] [waveNumber].endtime = totalTime;
    
    		MasterCount++;
    	
    
    		//test only display results
    /*		cout<<partnerList[count2] [waveNumber].partnerId<<" "
    			<<partnerList[count2] [waveNumber].wavenumber<<" "
    			<<partnerList[count2] [waveNumber].station<<" "
    			<<partnerList[count2] [waveNumber].casecount<<" "
    			<<partnerList[count2] [waveNumber].starttime<<" "
    			<<partnerList[count2] [waveNumber].endtime<<" "
    			<<partnerList[count2] [waveNumber].date<<endl; */
    however, if I were to attempt just to review a specific subscript containing a struct such as:
    Code:
    cout <<partnerList[1] [1].partnerId<<" "
    					 <<partnerList[1] [1].wavenumber<<" "
    					 <<partnerList[1] [1].station<<" "
    					 <<casperHr<<" "<<percent<<" "
    					 <<partnerList[1] [1].date<<endl;
    I get a compiler error, stating misuse of cout, and conversion violations with the variables. Thats what I meant but not very successful.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    164
    sorry, I forgot to take the comment out settings from my cout statement in the first set of posted code. They are not normally there when testing the output.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Strange, nothing looks particularly wrong with that. Can you give the exact compiler errors?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    164
    ya, I tried the following, and did not get an error message, but this was the output on screen that was returned from the array subscript:
    Code:
    cout <<partnerList [1][1].partnerId<<" "
           <<partnerList [1][1].wavenumber<<" "
           <<partnerList [1][1].station<<" "
           <<partnerList [1][1].casecount<<" "
           <<partnerList [1][1].starttime<<" "
           <<partnerList [1][1].endtime<<" "
           <<partnerList [1][1].date<<endl;
    and again just got what appears to be the ascii equivalent of memory addresses. Sorry I was not able to produce the error messages again. Hunter2, I appreciate the time looking at this, I don't know what the minds at this site get out of helping guys like me, but I really apreciate it.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>what appears to be the ascii equivalent of memory addresses
    Actually, that sounds like just garbage values (you get a bunch of weird symbols, hearts, spades, happy faces, lines etc.?). That's what happens when the variables in a structure don't get set, and you try printing them out - you get random values.

    Ok, I just copied your code and pasted it into MSVC so I can read it better. I have a few things to point out:
    -What is ios::eofbit supposed to do?
    -Take a look at this:
    Code:
    while (count2 <100)
    {
    	if ((strcmp(partnerTable[count2], partnerId)==0))
    	{
    		index = count2;
    	}
    	count2++;
    };
    What's it supposed to do?? index isn't referenced anywhere else; and count2 is never reset anywhere in the do-while loop anyways. **Actually, after reading further it seems you might consider using index instead of count2 in a bunch of places. Also, you might consider looking into the 'break' keyword, or find some way to stop the loop once you've found the right index.

    And here:
    Code:
    strcpy(partnerList[count2] [waveNumber].partnerId, partnerId);
    Since count2 never gets reset, and the loop keeps going until 100, count2 will always be 100 even if it's at the wrong index. That would account for why your structures aren't getting filled in properly.

    Take a look at those, and see if that fixes anything
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    May 2004
    Posts
    164
    will do, thanks hunter, I'll let you know what I come up with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. better way to scanf of a array of structs member?
    By stabu in forum C Programming
    Replies: 3
    Last Post: 07-17-2008, 04:51 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Replies: 12
    Last Post: 12-06-2005, 08:30 PM
  5. Eliminating duplicate values from a 20x2 array
    By desipunjabi in forum C Programming
    Replies: 2
    Last Post: 10-29-2005, 09:11 PM