2 Replies - 551 Views - Last Post: 13 November 2009 - 07:00 AM Rate Topic: -----

#1 akihabara85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-November 09

How to not use global variable without affecting the program?

Posted 13 November 2009 - 06:27 AM

Hi all, anything i can do to not use global variable without affecting the program?
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int total=0; //global variable

class CustData
{
	  public:
		CustData();	// constructor 
		int setData(string,string,string,string,string,string);
		void setBank(string, string, float, int);
		void setInv(string, string, string, float, float);
		void displayBank();
		void displayData(int);
		void displayOverall();
		void displaySummary();
		string num_id;
		
	  private:
		string id;
		string fname;
		string lname;
		string add;
		string phonenum;
		string type;
		string accountnum[8];
		string stype[8];
		string investnum[8];
		string investtype[8];
		float balance[8];
		float interest[8];
		float investvalue[8];
		float currvalue[8];
		float profit[8];
		int numYears[8];
		int bankacc,investacc;
		static int normal,silver,gold,platinum;

};

CustData::CustData()
{
	 bankacc=0;
	 investacc=0;
}

int CustData::normal=0, CustData::silver=0,CustData::gold=0,CustData::platinum=0;

// set input from customer.txt
int CustData::setData(string cust_id,string first, string last,string address,string pnum,string ntype)
{
	char temp[8];
	
	strcpy(temp,ntype.c_str());
	for (int x = 0; x <9; x++)
		temp[x] = tolower(temp[x]);
	ntype = temp;
//check for valid id input from customers.txt
	if(cust_id.length() == 5)
		for(int x = 0; x<5; x++)
		{
			if(isdigit(cust_id[x]));
			else return 0;
			num_id = cust_id;
		}
	else return 0;
//convert first name, last name and address to lower case
	strcpy(temp,first.c_str());
	for (int i=1;i<first.length();i++)
	   temp[i] = tolower(temp[i]);
	fname=temp;
	
	strcpy(temp,last.c_str());
	for (int i=1;i<last.length();i++)
	   temp[i] = tolower(temp[i]);
	lname=temp;
	
	strcpy(temp,address.c_str());
	for (int i=1;i<address.length();i++)
	   temp[i] = tolower(temp[i]);
	add=temp;
//check for valid 8 digit telephone input
	if(pnum.length() == 8)
		for(int x = 0; x<8; x++)
		{
			if(isdigit(pnum[x]));
			else return 0;
			phonenum = pnum;
		}
	else return 0;
//check customer type and increment to calculate total num of customer for each type
	if((ntype[0]=='G')||(ntype[0]=='g'))
	{
		type = ntype;
		gold++;
	}
	else if((ntype[0]=='N')||(ntype[0]=='n')&&(ntype.length()==6))
	{
		type = ntype;
		normal++;
	}
	else if((ntype[0]=='P')||(ntype[0]=='p'))
	{
		type = ntype;
		platinum++;
	}
	else if((ntype[0]=='S')||(ntype[0]=='s'))
	{
		type = ntype;
		silver++;
	}
	else return 0;

	return 1;
}

void CustData::displayOverall()  // display overall detail for customer type
{
	cout << "=================================================================" << endl;
	cout << "*** Overall Customer Statistics ***" << endl;
	cout << "Number of customers type Normal: " << normal << endl;
	cout << "Number of customers type Silver: " << silver << endl;
	cout << "Number of customers type Gold: " << gold << endl;
	cout << "Number of customers type Platinum: " << platinum << endl;
	cout << "Total number of Customer: " << total << endl;
}

void CustData::displayData(int num) // display details of customers,bank account and investment account
{
	 cout<< "Name: " << fname << " " << lname << endl;
	 cout<< "Address: " << add << endl;
	 cout<< "Phone: " << phonenum << endl;
	 cout<< "Customer Type: " << type << endl<<endl;
	 
	 if(bankacc>0) cout << "*** Bank accounts ***" << endl;
	 
	 for (int i=0; i< bankacc; i++)
	 {
	 cout << setiosflags(ios::left);
	 cout << setiosflags(ios::fixed);
	 cout << "Number: " << accountnum[i] << endl;
	 cout << "Type: " << stype[i] << endl;
	 cout << "Original Balance: $" << setw(8) << setprecision(2)<< balance[i] << endl;
	 cout << "Num of Years: " << numYears[i] << endl;
	 cout << "Interest Earned: $" << setw(8) << setprecision(2) << interest[i]<<endl;
	 cout << "Final Balance: $" << setw(8) << setprecision(2) << balance[i]+interest[i]<< endl<<endl;
	 }
	 
	if(investacc>0) cout<<"*** Investment Accounts ***"<<endl<<endl;

	for(int i = 0; i < investacc; i++)
	{
		cout<<"Number: "<<investnum[i]<<endl;
		cout<<"Type: "<<investtype[i]<<endl;
		cout<<"Invested Value: $"<<investvalue[i]<<endl;
		cout<<"Current Value : $"<<currvalue[i]<<endl;
		if(profit[i]<0) cout<<"Gain/Loss: -$"<<0-profit[i]<<endl;
		else cout<<"Gain/Loss: $"<<profit[i]<<endl;

		cout<<endl;
	}
}

void CustData::displaySummary()  // display customer portfolio
{
	 float totalamt=0, totalinv=0;
	 
	 for(int i=0; i< bankacc; i++)
	 {
		   totalamt+= balance[i]+interest[i];
		   totalinv+= currvalue[i];
	 }
	 cout << setiosflags(ios::right);
	 cout << setiosflags(ios::fixed);
	 cout<<num_id<<"\t\t$ ";
	 cout << setw(8) << setprecision(2) << totalamt<<"\t$ ";
	 cout << setw(9) << setprecision(2) << totalinv<<"\t$ ";
	 cout << setw(9) << setprecision(2) << totalamt+totalinv << endl;
}
void CustData::setBank(string acc,string s, float bal,int yrs) // set input from account.txt
{
	   char temp[20];
	   
	   strcpy(temp,s.c_str());
	   for(int i=0; i<s.length(); i++)
	   {
	   temp[i]=tolower(temp[i]);  // convert account type to lowercase
	   s=temp;
	   }
	   //savings
	   if((s[0] == 'S')||(s[0] == 's'))
	   {
		  accountnum[bankacc]=acc;
		  stype[bankacc]=s;
		  balance[bankacc] = bal;
		  numYears[bankacc] = yrs;
		  if(type[0]=='N'||type[0]=='n') interest[bankacc] = bal*0.015*yrs;
		  else if(type[0]=='S'||type[0]=='s') interest[bankacc] = bal*0.02*yrs;
		  else if(type[0]=='G'||type[0]=='g')  interest[bankacc] = bal * 0.025*yrs;
		  else if(type[0]=='P'||type[0]=='p') interest[bankacc] = bal * 0.03*yrs;
	   }
	   //checkings
	   else if((s[0] == 'C')||(s[0] == 'c'))
	   {
		   accountnum[bankacc]=acc;
		   stype[bankacc]=s;
		   balance[bankacc] = bal;
		   numYears[bankacc] = yrs;
		   if(type[0]=='N'||type[0]=='n') interest[bankacc] = bal*0.01*yrs;
		   else if(type[0]=='S'||type[0]=='s') interest[bankacc] = bal*0.0125*yrs;
		   else if(type[0]=='G'||type[0]=='g')  interest[bankacc] = bal * 0.015*yrs;
		   else if(type[0]=='P'||type[0]=='p') interest[bankacc] = bal * 0.0175*yrs;
	   }
	   //fixed deposit
	   else if((s[0] == 'F')||(s[0] == 'f')&&(s.length()==12))
	   {
		  accountnum[bankacc]=acc;
		  stype[bankacc]=s;
		  balance[bankacc] = bal;
		  numYears[bankacc] = yrs;
		  if(type[0]=='N'||type[0]=='n') interest[bankacc] = bal*0.02*yrs;
		  else if(type[0]=='S'||type[0]=='s') interest[bankacc] = bal*0.03*yrs;
		  else if(type[0]=='G'||type[0]=='g')  interest[bankacc] = bal * 0.04*yrs;
		  else if(type[0]=='P'||type[0]=='p') interest[bankacc] = bal * 0.05*yrs;
	   }
	   bankacc++;
}

void CustData::setInv(string id,string invnum,string invtype,float value,float cvalue) // set input from investments.txt
{	
	investnum[investacc]=invnum;
	investtype[investacc]=invtype;
	investvalue[investacc]=value;
	currvalue[investacc]=cvalue;
	profit[investacc] = cvalue - value;

	investacc++;
}	

void read_Cust(CustData[]);  
void read_Bank(CustData[]);
void read_Inv(CustData[]);
void display(CustData[]);

int main()
{
	CustData customer[20];
	
	read_Cust(customer);
	read_Bank(customer);
	read_Inv(customer);
  
	customer[0].displayOverall();
	display(customer);
	
	system("pause");
	return 0;
}

void read_Cust(CustData customer[])	// read input from customers.txt
{
	ifstream fin;
	string first,last,addr,ntype,num_id,hp;
		
	fin.open("customers.txt");
	if (!fin.good())
	{
		cout << "File not found" << endl;
		exit(1);
	}
	while(!fin.eof())
	{
		fin>> num_id >> first >> last >> addr >> hp >> ntype;
		total += customer[total].setData(num_id,first,last,addr,hp,ntype);
	}
	fin.close();
}

void read_Bank(CustData customer[])	 //read input from account.txt
{
	 ifstream fin;
	 string id, atype, anum, yrs, bal;
	 float balance;
	 int numyears;
	 char temp[10];
	 
	 fin.open("account.txt");
	 if (!fin.good())
	 {
		 cout << "File not found" << endl;
		 exit(1);	
	 }
	 while(!fin.eof())
	 {
		 fin >> id >> anum >> atype >> bal >> yrs;
		 
		 if((anum.length()==6)&&(isdigit(anum[0,1,2,3,4,5])))
		 {
		  if((atype.length()>=7)&&(bal[0]!='-')&&(isdigit(bal[0,1,2,3])))
		  {
		   strcpy(temp,bal.c_str());
		   balance=atof(temp);
		   strcpy(temp,yrs.c_str());
		   numyears=atoi(temp);
		   for(int count = 0; count < total; count++)
		   {
			if(customer[count].num_id == id)
			customer[count].setBank(anum,atype,balance,numyears);
		   }
		  }
		 }
	}
	fin.close();
}

void read_Inv(CustData customer[])   //read input from investment.txt
{
	 ifstream fin;
	 string id, invnum, invtype, invalue, currval;
	 float value;
	 float cvalue;
	 char temp[10];
	 
	 fin.open("investment.txt");
	 if (!fin.good())
	 {
		 cout << "File not found" << endl;
		 exit(1);	
	 }
	 while(!fin.eof())
	 {
		 fin >> id >> invnum >> invtype >> invalue >> currval;
		 
		 if((invnum.length()==7)&&(isdigit(invnum[0,1,2,3,4,5,6])))
		 {
		  if(isdigit(invalue[0,1,2]))
		  {
		   strcpy(temp,invalue.c_str());
		   value=atof(temp);
		   strcpy(temp,currval.c_str());
		   cvalue=atof(temp);
		   for(int count = 0; count < total; count++)
		   {
			if(customer[count].num_id == id)
			customer[count].setInv(id, invnum,invtype,value,cvalue);
		   }
		  }
		 }
	}
	fin.close();
}

void display(CustData customer[])	 //display of individual customer heading and customer portfolio heading
{
	 for(int i = 0; i < total; i++)
	{
		cout<<"========================================="<<endl;
		cout<<"***   Individual Customer Statistics  ***"<<endl;
		cout<<"***   Particulars   ***"<<endl;
		customer[i].displayData(i);
	}
	 
	cout<<"========================================="<<endl;
	cout<<"***   Summary of Customer Portfolios  ***"<<endl;
	cout<<"CustomerID  TotalBankBalance  TotalInvestValue  GrandTotal"<<endl;
	string lastcust;
	string lastshown="0";
	int showCust;

	for(int x = 0; x < total; x++)
	{
		lastcust="99999";
		for(int count = 0; count < total; count++)
		{
			if(customer[count].num_id<lastcust&&customer[count].num_id>lastshown)
			{
				lastcust = customer[count].num_id;
				showCust = count;
			}
		}
		lastshown = customer[showCust].num_id;
		customer[showCust].displaySummary();
	}
}


This post has been edited by akihabara85: 13 November 2009 - 06:29 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to not use global variable without affecting the program?

#2 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: How to not use global variable without affecting the program?

Posted 13 November 2009 - 06:49 AM

would a global pointer make you feel any better?

The repetitive way you could do it is just by passing the reference or value of your variable into every function it is used in.
Was This Post Helpful? 0
  • +
  • -

#3 akihabara85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 09-November 09

Re: How to not use global variable without affecting the program?

Posted 13 November 2009 - 07:00 AM

hi, can u show me how can i get to do it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1