6 Replies - 18079 Views - Last Post: 11 June 2009 - 04:58 AM Rate Topic: -----

#1 binarysnake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-March 09

Passing an Array of struct by reference

Post icon  Posted 10 June 2009 - 10:52 PM

I'm trying to pass information from an external text file into an array of structures. Here, the program extracts a student's name, and four scores.
It stores the name and four scores into a struct. (I've tested out doing it with just one name and one set of scores and worked fine)
My problem begins when I try to do that but with a larger list of names and with an array of struct. Whenever I run the program, it all shows up empty
in main. But if I put code to print to the screen in the "read" function while the program reads the data, it comes up fine. Albeit with the last entry read twice, but
that's not the problem.

Long story short, the array isn't passing any values through from "read" function to "main" function.


#include<iostream>
#include<fstream>
#include<cmath>
#include<string>
#include<iomanip>

using namespace std;
#define SIZE 50

//structure
struct data
{
	string name; // student name
	double s[4]; //holds an array of 4 scores
	double average; // holds the average.
	char letter; // student letter grade
};


//prototype declarations
void open(ifstream& fin); // opens the file
void read(ifstream &fin, data student[]); // reads and stores the information into the array of struct


int main()
{
	ifstream fin;
	ofstream fout;
	data student[SIZE]; // declared the array of size SIZE
	int i,j;


	open(fin);
	read(fin,student);


// Here I try to see if the info has passed, but at runtime it shows up empty.

	for (i=0;i<SIZE;i++)
	{
		cout<<student[i].name<<endl;
		for (j=0;j<SIZE;j++)
		{
			cout<<student[i].s[j]<<endl;
		}
	}
}

void open(ifstream& fin) //generic open file code...
{
	string finput;

	cout<<"Enter Input File Name: ";
	cin>>finput;

	fin.open(finput.c_str());
	if(fin.fail())
	{
		cout<<"Input File Invalid!!!"<<endl;
	}
}

void read(ifstream &fin, data student[]) // read and store the info.
{
	int i,j=0;

	getline(fin,student[j].name); //get the name of the student.
	for(i=0;i<4;i++)
	{
		fin>>student[j].s[i]; // get scores 1 - 4 and save them.
	}
	fin.ignore(10,'\n'); 
}




the file looks like:

First Last
90 10 40 50
prima secunda
90 20 30 40




so on and so forth.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing an Array of struct by reference

#2 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Passing an Array of struct by reference

Posted 10 June 2009 - 11:17 PM

It sounds like your function copies the struct by value and that's why it doesn't carry back to main but shows it from inside the function itself. Try putting & in the parameter for the struct so it does things to the struct directly. Or don't use void and somehow return the struct? I'm not exactly sure what to do with an array of structs so partly guessing.

This post has been edited by Kanvus: 10 June 2009 - 11:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 binarysnake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-March 09

Re: Passing an Array of struct by reference

Posted 10 June 2009 - 11:20 PM

View PostKanvus, on 10 Jun, 2009 - 10:17 PM, said:

It sounds like your function copies the struct by value and that's why it doesn't carry back to main but shows it from inside the function itself. Try putting & in the parameter for the struct so it does things to the struct directly. Or don't use void and somehow return the struct? I'm not exactly sure what to do with an array of structs so partly guessing.


Well the problem with that is that it's being sent out and back in as an array. Because it's more than one student (up to 50), and arrays can't be returned. And because it's an array I can't use the & since I can only pass arrays by reference.
Was This Post Helpful? 0
  • +
  • -

#4 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Passing an Array of struct by reference

Posted 11 June 2009 - 12:13 AM

& is reference. Anyway I tried doing an example of:

1) Passing an array of structs into a void function.

2) Letting the function fill each struct in.

3) The changes are done directly to the array of structs because an array is a pointer.

So all in all, the changes are permanent and shows when cout'ed from main.

#include <iostream>
#include <string>
using namespace std;

struct Yo{
	   
	   int one;
	   int two;
	   string name;	 

};

void readstruct(struct Yo hey[])
{
   for( int i = 0; i < 50; i++ ){		 
	   
	   hey[i].name = "bob";
	   hey[i].one = i; 
								   
   }  
	 
}

int main(){
	
	Yo object[50];
	
	readstruct(object);
	
	cout << "Random struct to show: " << object[3].name 
		 << " and number " << object[45].one << endl;




 cout << endl;
 system("PAUSE");
 return 0;   
}





Oh I see something. Your read function only happens once. It needs to be in a for loop to cycle through every student, not just one.

This post has been edited by Kanvus: 11 June 2009 - 12:14 AM

Was This Post Helpful? 1
  • +
  • -

#5 binarysnake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-March 09

Re: Passing an Array of struct by reference

Posted 11 June 2009 - 12:35 AM

View PostKanvus, on 10 Jun, 2009 - 11:13 PM, said:

& is reference. Anyway I tried doing an example of:

1) Passing an array of structs into a void function.

2) Letting the function fill each struct in.

3) The changes are done directly to the array of structs because an array is a pointer.

So all in all, the changes are permanent and shows when cout'ed from main.

#include <iostream>
#include <string>
using namespace std;

struct Yo{
	   
	   int one;
	   int two;
	   string name;	 

};

void readstruct(struct Yo hey[])
{
   for( int i = 0; i < 50; i++ ){		 
	   
	   hey[i].name = "bob";
	   hey[i].one = i; 
								   
   }  
	 
}

int main(){
	
	Yo object[50];
	
	readstruct(object);
	
	cout << "Random struct to show: " << object[3].name 
		 << " and number " << object[45].one << endl;




 cout << endl;
 system("PAUSE");
 return 0;   
}





Oh I see something. Your read function only happens once. It needs to be in a for loop to cycle through every student, not just one.


So I checked my code against yours and I was able to get it working through every name. =) Thanks a bunch.

But now I have another problem...I was able to get all the names to fill up the slots that were required to be filled but there's a surplus of array slots. Say my file only has 1 student's information. There would be 49 slots left open. When I run my program, the program shows these uninitialized variables. I don't want to initialize them all to 0 because then I get a lot of zeros. I just want the array to sort of close off at the last name and ignore any unused array slots.

I read somewhere that I could initialize the array but I tried that and everything messed up anyway.

Sorry to be such a bother.
Was This Post Helpful? 0
  • +
  • -

#6 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Passing an Array of struct by reference

Posted 11 June 2009 - 12:48 AM

I would say use vectors but no...don't see how a vector of structs would work.

Not sure how your new code is but

for( int i = 0; i < SIZE; i++ ){

	if( student[i].name ){
		  
		  //Write the rest of the student's information
		  //if her name was not NULL or empty.

	}
	else continue; //Or else continue with for loop
					//without writing anything at all.
				   //And skip until the end of SIZE is reached

}



The else continue part is iffy. It's probably optional and a better idea to not have an else. Just there to make a point.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5829
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Passing an Array of struct by reference

Posted 11 June 2009 - 04:58 AM

View Postbinarysnake, on 11 Jun, 2009 - 01:35 AM, said:

But now I have another problem...I was able to get all the names to fill up the slots that were required to be filled but there's a surplus of array slots.


You have to count the number you read. Save that count. I like to make a second struct to save the current size in. Also makes passing the array around much easier.

e.g.
struct Students {
	data student[SIZE]; // max size
	int size; // actual current size
};

void read(ifstream &fin, Students &students);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1