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.

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:

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;
}

You are passing in to set_add variable that don't exist yet! In the beginning of your main, declare them like this:

string St_address;
string city, state;
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.

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.

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

int MyInt;
Function(MyInt);

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.