Getting error when calling function from main()

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 46
Reputation: robgeek is an unknown quantity at this point 
Solved Threads: 0
robgeek robgeek is offline Offline
Light Poster

Getting error when calling function from main()

 
0
  #1
Dec 13th, 2008
Hi, I am trying to write an address book. The problem is: in my class declaration I declared a mutator.
In the implementation, I am asking the user to enter his address, city, state and zip.
So my prototype void set_add(string, string, string, int).
In other words I am trying to set the address, city, state, and zip variables to the one entered by the user. To implement this I am using reference.

But no matter what I put in as arguments while calling from main, I keep getting the error: variable 'variable' not declared.

Here is my code for clarity:

/******************Header**************************/
#ifndef addressType_H

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

class addressType
{
private:
	string St_address;
		string city, state;
	int Zip;

public:
	addressType(string, string, string, int);//default constructor;
	void show_add();//accessor
	void set_add(string&, string&, string&, int&);//mutator
	
};
#endif

/************Implementation*********************/
#include "addressType.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

addressType::addressType(string street, string cit, string st, int zip)
{
	street = "9803 Summer Breeze Dr.";
	St_address = street;
	cit = "Pearland";
	city = cit;
	st = "Tx";
	state = st;
	zip = 77584;
	Zip = zip;

}

void addressType::show_add()
{
	cout<<St_address<<endl;
	cout<<city<<","<<state<<endl;
	cout<<Zip<<endl;

	return;
}


void addressType::set_add(string &street, string &cit, string &st, int &zip)
{
	cout<<"Enter street address."<<endl;
	cin>>street;
	

	cout<<"Enter your city."<<endl;
	cin>>cit;
	

	cout<<"Enter your state."<<endl;
	cin>>st;
	

	cout<<"Enter the zip code."<<endl;
	cin>>zip;
	

	return;
}


/**********************main()******************/

#include "addressType.h"
#include<iostream>
#include<string>
using namespace std;

int main()
{
	addressType Avi("9803 Summer Breeze", "Pearland", "Tx", 77584);
	Avi.show_add();
	Avi.set_add(St_address, city, state, Zip);//what will be my arguments here that would set the values to the one that user inputs?
	


	return 0;
}

Thanks.
Last edited by robgeek; Dec 13th, 2008 at 4:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Getting error when calling function from main()

 
0
  #2
Dec 13th, 2008
it looks like your set_add function is expecting you to pass it some values, but then you are reading them in from cin anyway, but then not doing anything with them.
Maybe you want something more like this:
[code]
void addressType::set_add()
{
cout<<"Enter street address."<<endl;
string street;
cin>>street;
St_address = street;

cout<<"Enter your city."<<endl;
string cit;
cin>>cit;
city = cit;

cout<<"Enter your state."<<endl;
string st;
cin>>st;
state = st;

cout<<"Enter the zip code."<<endl;
cin>>zip;


return;
}

Dave
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 354
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: Getting error when calling function from main()

 
0
  #3
Dec 13th, 2008
You are passing in to set_add variable that don't exist yet! In the beginning of your main, declare them like this:
  1. string St_address;
  2. string city, state;
  3. int Zip;
Although your code was sort of hard to understand, the prototype for set_add said it was a mutator. But the way you wrote it, the object is never modified. If your goal was to set the object's values to user input, daviddoria's solution is the one to choose.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 46
Reputation: robgeek is an unknown quantity at this point 
Solved Threads: 0
robgeek robgeek is offline Offline
Light Poster

Re: Getting error when calling function from main()

 
0
  #4
Dec 13th, 2008
Thanks guys. Davidoria's method worked out. But I am confused. Why would not using reference as I did earlier is not working.

From what I understood from my course, is I am sending the reference value of street to St_address and I am manipulating the street variable which should change the St_address variable as well.
And then I was trying to call the St_address in the main().

Please explain where am I going wrong.

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Getting error when calling function from main()

 
0
  #5
Dec 13th, 2008
passing by reference is when you want to do pass a variable to a function that you want to take a new value and be used back in the calling function, ie

  1. int MyInt;
  2. Function(MyInt);
  3.  
  4. cout << MyInt;

In this case, we pass MyInt to Function by reference, then we can use MyInt back in the calling function.

In you're case, you are trying to set the member variables of the class using the mutator function, so there is no need to pass or return anything!

Dave
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC