GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-May-2006, 14:56
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Difficulty Passing Structure Data Elements Between Functions


Hi,

I am having some difficulty passing structure data between different functions in a program I'm working on for school. I am receiving several compile errors. Code and errors appear below, in that order. Please forgive if I've copied too much code in, as I'm not sure exactly where my problem lies.

CPP / C++ / C Code:
//driver.cpp
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <fstream.h>
#include <math.h>
#include <string.h>
#include "C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.h"



//Start of Main
int main()
{
	char Ans;
	cout<<"Jerry Brinegar"<<endl<<"Program 1"<<endl<<"Data Structures and Functions: Mailing Labels"<<endl<<"CP287B C++ Language Programming II"<<endl;
	system("pause");




	do
	{
	
	Envelope env;

	
	env=AskUserForEnvelope();
	
	WriteEnvelope(env);



	cout<<"Do you want to make another envelope?  y/n"<<endl;
	cin>>Ans;
	

	}while (Ans=='y');

	if (Ans=='n')
	{
		cout<<"Goodbye!"<<endl<<endl;
	}

return 0;	
}

#include "C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp"


//Envelope.cpp

Envelope AskUserForEnvelope(void)
{
	Envelope e;
	cout<<endl<<"Enter the Sender Address Information: "<<endl;
	e.sender=AskUserForAddress();
	cout<<"Enter the Recipient Address Information: "<<endl;
	e.receiver=AskUserForAddress();
	return e;
	
}


Address AskUserForAddress(void)
{
	Address a;
	cout<<"Enter First Name: ";
	cin.getline(a.FName, '25');
	cin.ignore(10, '\n');
	cout<<"Enter Last Name: ";
	cin.getline(a.LName, '25');
	cin.ignore(10, '\n');
	cout<<"Enter Street Address: ";
	cin.getline(a.Street, '25');
	cin.ignore(10, '\n');
	cout<<"Enter City: ";
	cin.getline(a.City, '25');
	cin.ignore(10, '\n');
	cout<<"Enter State: ";
	cin.getline(a.State, '15');
	cin.ignore(10, '\n');
	cout<<"Enter Zip Code: ";
	cin.getline(a.Zip, '10');
	cin.ignore(10, '\n');
	return a;

}


Envelope WriteEnvelope(env)
{

	ofstream output;
	
	#define FILE_OUT "Env.txt"

	output.open(FILE_OUT, ios::out);

	output<<env.sender.FName<<" "<<env.sender.LName<<endl;
	output<<env.sender.Street<<endl;
	output<<env.sender.City<<", "<<env.sender.State<<" "<<env.sender.Zip<<endl<<endl<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.FName<<" "<<env.receiver.LName<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.Street<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.City<<", "<<env.receiver.State<<" "<<env.receiver.Zip<<endl;

	output.close();

}



//Envelope.h

struct Address
{
	char FName[50], LName[50], Street[50], City[50], State[30], Zip[15];
};

struct Envelope
{

	Address sender, receiver;
};

Address AskUserForAddress(void);	//Prototype function that returns Address type
Envelope AskUserForEnvelope(void);
Envelope WriteEnvelope();

The errors I received are:

for driver.cpp
Compiling...
Driver.cpp
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Driver.cpp(39) : error C2660: 'WriteEnvelope' : function does not take 1 parameters
c:\documents and settings\jerry brinegar\desktop\cp278 - visual c++\cp 287b c++\brinegarp1\envelope.cpp(43) : error C2065: 'env' : undeclared identifier
c:\documents and settings\jerry brinegar\desktop\cp278 - visual c++\cp 287b c++\brinegarp1\envelope.cpp(44) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
Error executing cl.exe.

Driver.obj - 3 error(s), 0 warning(s)

for envelope.cpp
Compiling...
Envelope.cpp
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(5) : error C2146: syntax error : missing ';' before identifier 'AskUserForEnvelope'
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(5) : error C2501: 'Envelope' : missing storage-class or type specifiers
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(5) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

Envelope.obj - 3 error(s), 0 warning(s)

I'm fairly certain this has to do with what's passed into the functions and the return types that passes it back, but am unsure where and am getting a little confused on what's really passing back. Coneptually, AskUserForAddress should be passing back "a" to AskUserForEnvelope 2 times, as e.sender and e.receiver, then AskUserForEnvelope should pass back "e" (sender/receiver) to the main method (env) which is in turn passed to WriteEnvelope.

Or am I totally off?

Thanks.
  #2  
Old 20-May-2006, 15:24
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Difficulty Passing Structure Data Elements Between Functions


Quote:
Originally Posted by jdbrine
I am receiving several compile errors
Well let us take a look at one of the errors.

Quote:
Originally Posted by jdbrine
C++\BrinegarP1\Driver.cpp(39) : error C2660: 'WriteEnvelope' : function does not take 1 parameters
That looks like there is an error on line 39 of "Driver.cpp" with "WriteEnvelope".
So let us take a look at that.
CPP / C++ / C Code:
WriteEnvelope(env);
Maybe this. Not sure if it is line 39 but probably close enough.
Now let us find the function "WriteEnvelope".
CPP / C++ / C Code:
Envelope WriteEnvelope(env)
{

	ofstream output;
	
	#define FILE_OUT "Env.txt"

	output.open(FILE_OUT, ios::out);

	output<<env.sender.FName<<" "<<env.sender.LName<<endl;
	output<<env.sender.Street<<endl;
	output<<env.sender.City<<", "<<env.sender.State<<" "<<env.sender.Zip<<endl<<endl<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.FName<<" "<<env.receiver.LName<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.Street<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.City<<", "<<env.receiver.State<<" "<<env.receiver.Zip<<endl;

	output.close();

}
Ok that looks ok.

Now we can take a look at the prototype.
CPP / C++ / C Code:
Envelope WriteEnvelope();
Ack!

Now see what you you can do with the rest of the errors. Post back with any questions and include any updated code and any errors you receive.
Good luck.
  #3  
Old 20-May-2006, 15:41
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Difficulty Passing Structure Data Elements Between Functions


CPP / C++ / C Code:
Envelope WriteEnvelope(env)
{
Parameters need types, also.
  #4  
Old 20-May-2006, 23:51
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Difficulty Passing Structure Data Elements Between Functions


I apologize but I'm not following what you mean.

The prototype should be something like:

CPP / C++ / C Code:
Envelope WriteEnvelope(char);


and

CPP / C++ / C Code:
Envelope WriteEnvelope(env)
{

Parameters need types, also.

You mean it should be
CPP / C++ / C Code:
 Envelope WriteEnvelope(char env)
{

???
  #5  
Old 20-May-2006, 23:57
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Difficulty Passing Structure Data Elements Between Functions


Well, from this line in WriteEnvelope (and other lines)
CPP / C++ / C Code:
output<<env.sender.FName<<" "<<env.sender.LName<<endl;
it seems pretty clear that env is an Envelope. So your prototype needs to be
CPP / C++ / C Code:
Envelope WriteEnvelope(Envelope);
and the definition
CPP / C++ / C Code:
Envelope WriteEnvelope(Envelope env)
{

By the way, perhaps WriteEnvelope should return void because it is defined as returning an Envelope, but if you look in the function definition there is no return statement.
  #6  
Old 21-May-2006, 00:19
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Difficulty Passing Structure Data Elements Between Functions


Yes, I agree about not sending data back from WriteEnvelope, there isn't any need, So I've changed that:

CPP / C++ / C Code:
//Envelope.h

struct Address
{
	char FName[50], LName[50], Street[50], City[50], State[30], Zip[15];
};

struct Envelope
{

	Address sender, receiver;
};

Address AskUserForAddress(void);	//Prototype function that returns Address type
Envelope AskUserForEnvelope(void);
void WriteEnvelope(Envelope);


//Envelope.cpp

Envelope AskUserForEnvelope(void)
{
	Envelope e;
	cout<<endl<<"Enter the Sender Address Information: "<<endl;
	e.sender=AskUserForAddress();
	cout<<"Enter the Recipient Address Information: "<<endl;
	e.receiver=AskUserForAddress();
	return e;
	
}


Address AskUserForAddress(void)
{
	Address a;
	cout<<"Enter First Name: ";
	cin.getline(a.FName, '25');
	cin.ignore(10, '\n');
	cout<<"Enter Last Name: ";
	cin.getline(a.LName, '25');
	cin.ignore(10, '\n');
	cout<<"Enter Street Address: ";
	cin.getline(a.Street, '25');
	cin.ignore(10, '\n');
	cout<<"Enter City: ";
	cin.getline(a.City, '25');
	cin.ignore(10, '\n');
	cout<<"Enter State: ";
	cin.getline(a.State, '15');
	cin.ignore(10, '\n');
	cout<<"Enter Zip Code: ";
	cin.getline(a.Zip, '10');
	cin.ignore(10, '\n');
	return a;

}


void WriteEnvelope(Envelope env)
{

	ofstream output;
	
	#define FILE_OUT "Env.txt"

	output.open(FILE_OUT, ios::out);

	output<<env.sender.FName<<" "<<env.sender.LName<<endl;
	output<<env.sender.Street<<endl;
	output<<env.sender.City<<", "<<env.sender.State<<" "<<env.sender.Zip<<endl<<endl<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.FName<<" "<<env.receiver.LName<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.Street<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.City<<", "<<env.receiver.State<<" "<<env.receiver.Zip<<endl;

	output.close();


}

But I still have my compile errors in Envelope.cpp.

ompiling...
Envelope.cpp
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(5) : error C2146: syntax error : missing ';' before identifier 'AskUserForEnvelope'
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(5) : error C2501: 'Envelope' : missing storage-class or type specifiers
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(5) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

BrinegarP1.exe - 3 error(s), 0 warning(s)


Thank you!
  #7  
Old 21-May-2006, 01:19
Johnny Johnny is offline
New Member
 
Join Date: May 2006
Posts: 27
Johnny is on a distinguished road

Re: Difficulty Passing Structure Data Elements Between Functions


Well you are giving it a data type of Envelope, but the compiler doesn't know what that data type is. You need to include Envelope.h in Envelope.cpp

Then wehn the compiler tries compiling Envelope.cpp it will know what type Envelope is.
  #8  
Old 21-May-2006, 11:43
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Difficulty Passing Structure Data Elements Between Functions


There are 2 .cpp files: driver.cpp and envelope.cpp. In driver there is an include for envelope.h:

CPP / C++ / C Code:
//driver.cpp
#include "C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.h"



//Start of Main
int main()
{
	char Ans;
	cout<<"Jerry Brinegar"<<endl<<"Program 1"<<endl<<"Data Structures and Functions: Mailing Labels"<<endl<<"CP287B C++ Language Programming II"<<endl;
	system("pause");




	do
	{
	
	Envelope env;

	
	env=AskUserForEnvelope();
	
	WriteEnvelope(env);



	cout<<"Do you want to make another envelope?  y/n"<<endl;
	cin>>Ans;
	

	}while (Ans=='y');

	if (Ans=='n')
	{
		cout<<"Goodbye!"<<endl<<endl;
	}

return 0;	
}

#include "C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp"


//envelope.cpp

Envelope AskUserForEnvelope(void)
{
	Envelope e;
	cout<<endl<<"Enter the Sender Address Information: "<<endl;
	e.sender=AskUserForAddress();
	cout<<"Enter the Recipient Address Information: "<<endl;
	e.receiver=AskUserForAddress();
	return e;
	
}


Address AskUserForAddress(void)
{
	Address a;
	cout<<"Enter First Name: ";
	cin.getline(a.FName, '25');
	cin.ignore(10, '\n');
	cout<<"Enter Last Name: ";
	cin.getline(a.LName, '25');
	cin.ignore(10, '\n');
	cout<<"Enter Street Address: ";
	cin.getline(a.Street, '25');
	cin.ignore(10, '\n');
	cout<<"Enter City: ";
	cin.getline(a.City, '25');
	cin.ignore(10, '\n');
	cout<<"Enter State: ";
	cin.getline(a.State, '15');
	cin.ignore(10, '\n');
	cout<<"Enter Zip Code: ";
	cin.getline(a.Zip, '10');
	cin.ignore(10, '\n');
	return a;

}


void WriteEnvelope(Envelope env)
{

	ofstream output;
	
	#define FILE_OUT "Env.txt"

	output.open(FILE_OUT, ios::out);

	output<<env.sender.FName<<" "<<env.sender.LName<<endl;
	output<<env.sender.Street<<endl;
	output<<env.sender.City<<", "<<env.sender.State<<" "<<env.sender.Zip<<endl<<endl<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.FName<<" "<<env.receiver.LName<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.Street<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.City<<", "<<env.receiver.State<<" "<<env.receiver.Zip<<endl;

	output.close();


}

//envelope.h

struct Address
{
	char FName[50], LName[50], Street[50], City[50], State[30], Zip[15];
};

struct Envelope
{

	Address sender, receiver;
};

Address AskUserForAddress(void);	//Prototype function that returns Address type
Envelope AskUserForEnvelope(void);
void WriteEnvelope(Envelope);



If I were to add the #include statement into envelope.cpp I would get redefinition errors because it already exists in driver.cpp:

Compiling...
Driver.cpp
c:\documents and settings\jerry brinegar\desktop\cp278 - visual c++\cp 287b c++\brinegarp1\envelope.h(6) : error C2011: 'Address' : 'struct' type redefinition
c:\documents and settings\jerry brinegar\desktop\cp278 - visual c++\cp 287b c++\brinegarp1\envelope.h(11) : error C2011: 'Envelope' : 'struct' type redefinition
Error executing cl.exe.

Driver.obj - 2 error(s), 0 warning(s)


So, I'm back where I started with the errors compiling envelope.cpp:

Compiling...
Envelope.cpp
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(6) : error C2146: syntax error : missing ';' before identifier 'AskUserForEnvelope'
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(6) : error C2501: 'Envelope' : missing storage-class or type specifiers
C:\Documents and Settings\Jerry Brinegar\Desktop\CP278 - Visual C++\CP 287B C++\BrinegarP1\Envelope.cpp(6) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

BrinegarP1.exe - 3 error(s), 0 warning(s)
  #9  
Old 21-May-2006, 12:08
Johnny Johnny is offline
New Member
 
Join Date: May 2006
Posts: 27
Johnny is on a distinguished road

Re: Difficulty Passing Structure Data Elements Between Functions


It is the way you are including it, I didn't notice that or I would have told you before. You are including the envelope.cpp wrong.

If I include it like a header file here are the errors I get

Quote:
------ Build started: Project: Driver, Configuration: Release Win32 ------
Compiling...
driver.cpp
c:\documents and settings\shark\desktop\c++ by example\Envelope.h(2) : error C2011: 'Address' : 'struct' type redefinition
c:\documents and settings\shark\desktop\c++ by example\Envelope.h(2) : see declaration of 'Address'
c:\documents and settings\shark\desktop\c++ by example\Envelope.h(7) : error C2011: 'Envelope' : 'struct' type redefinition
c:\documents and settings\shark\desktop\c++ by example\Envelope.h(7) : see declaration of 'Envelope'

But if I don't #include "Envelope.cpp"
And instead include the file in my project in my compiler then here is what happens.

Quote:
------ Build started: Project: Driver, Configuration: Release Win32 ------
Compiling...
driver.cpp
Linking...
LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/OPT:ICF' specification
Embedding manifest...
Build log was saved at "file://c:\Documents and Settings\Shark\Desktop\Driver\Release\BuildLog.htm "
Driver - 0 error(s), 1 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

It successfully builds the project, the reason for this is because it compiles each .cpp file seperately, so it doesn't see the include files from the other .cpp files.

I included this in both driver.cpp and Envelope.cpp

Code:
#include <stdio.h> #include <conio.h> #include <iostream> #include <iomanip> #include <stdlib.h> #include <fstream> #include <math.h> #include <string.h> #include "Envelope.h" using namespace std;

You will notice certain files don't have the .h extension and I also have "using namespace std;" that isn't to do with the problem, that is just because that is modern C++. Your code seems to be using older C++, but my compiler requires newer C++.

The actual problem as I said was that firstly you are including the .cpp file wrong, it needs to be added to your project. If you are compiling from the command line I am not sure the switch you need to use in order to let the compiler know you are using multiple .cpp files. This would come down to your specific compiler so you would need to search the docs.

Secondly, once you have added the cpp file correctly just include envelope.h and it will (should) compile fine as I showed
  #10  
Old 21-May-2006, 13:03
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: Difficulty Passing Structure Data Elements Between Functions


I think I got it....Thanks!!!!
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
Strange C++ code memory leakage problem gaoanyu C++ Forum 7 04-Nov-2005 09:09
passing tables as arguments to functions. kobi_hikri C Programming Language 8 27-Jul-2005 08:29
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:57.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.