Dear All,
I have a code as below. First I call the readFile in here I can read the value into the name variable which I have declared as string. The problem now as I run the loop I want to put each of the name into an array of names which is the clientName. Here is where I am stuck char *isName = new char;*isName = name;strcpy(clientName[clientCount],isName);? Any help please? Thank you.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void readFile(char* [],int []);
void caclulateInterest(char * [],int [],int []);
int main()
{
	int totalClient = 10;
	char *clientName[10];
	int clientSavings[10],clientInterest[10];
	
    readFile(clientName,clientSavings);
	//caclulateInterest(clientName,clientSavings,clientInterest);
}

void readFile(char *clientName[],int clientSavings[])
{
	int clientCount=0;
	ifstream inFile;
	inFile.open("example.txt");
	 
 
	if(inFile.is_open())
	{   
	    
		while(!inFile.eof()) 
		{
		 string name="";
		 double salary = 0.00;	
         getline(inFile,name,'#');
		 inFile>>salary;
		 char *isName = new char[20];
		 *isName = name;
		 strcpy(clientName[clientCount],isName);
         //cout<<"\nName"<<name;
		 //cout<<"\nSalary"<<salary;
		 clientCount++;
		}
		//system("pause");
	}
	else
	{
	  cout<<"Failed to open";	
	}
   
}
void caclulateInterest(char *clientName[],int clientSavings[],int clientInterest[])
{
   for(int i=0; i<2; i++)
   {
	   cout<<"Name is : "<<*clientName[i];
   }
   system("pause");
}

Recommended Answers

In readFile, first you use std::string and then you use C-strings. You should pick one and be consistent. If your concern is getline there is a version that is a member method of istream (http://www.cplusplus.com/reference/iostream/istream/getline/). Also, line 35 will not work.

Jump to Post

All 2 Replies

In readFile, first you use std::string and then you use C-strings. You should pick one and be consistent. If your concern is getline there is a version that is a member method of istream (http://www.cplusplus.com/reference/iostream/istream/getline/). Also, line 35 will not work.

Dear Jonsa,
I should put like this is it std::string name="";. Can you tell me which one to pick I am quite confuse too ?

Be a part of the DaniWeb community

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