I am trying to read in information from an external file using ifstream. I use fin >> to input the data usually. Now I have to input a full name. Can anyone tell me what I use to do this? do I use a string type variable and then just fin >> string1.

Example

112 2 Any Name Here
1 0 4 5

I need to pull the Any Name Here into a variable and then use my usual
fin >> whatevertopullin1

Also if I have an array serviceCounts[j] how do I add one to the j value?

I need to pull the Any Name Here into a variable and then use my usual

the >> insert operator stops reading the keyboard when it encounters the first space. If you want the string to include spaces, then use getlin()

std::string full_name;
getline(cin,full_name);

>> Also if I have an array serviceCounts[j] how do I add one to the j value?

use j++. Either of the two methods below will work.

serviceCounts[i][j];
j++;

or sorthand
 serviceCounts[i][j++];

If i am reading in a value name transCode. It will be an A V or C is this how i set up the if statements

if(transCode == 'A') do i need to use quotes or single quotes or quotes at all. I have made the function compile but have yet to make it work to see if it is entering the if statements correctly.

If transCode is of type std::string, then you would use double quotes. The compiler will complain loudly if you use single quotes.
if(transCode == "A")

First declare a C standard char string..
char str1[100];

Once you create your input file object...
ifstream fin("filename");

Then you do the fin >>...
fin >> str1;

the value is not a string it is a char. it is char transCode. Then i have if (transCode == "C")i am using double quotes and it is telliing me c++ forbids comparison between pointer and integer. Thanks for all your help by the way everyone.

well, you might as well post some code that shows how you declared the variables and how they are being used. This guessing game is the pits. If transCode is a single character, then you have to use single quotes. If it is std::string then use double quotes. If it is char* then you use neither -- use strcmp() instead. There are so many possibilities that it isn't possible to give you a straight answer because you didn't provide enough information of show us any useful code.

int ProcessTransData(ifstream& transIn, int& i, int& numClients)
{
	char transCode;
	transIn >> transCode;
	
	if (transCode == "C")
	{
		ChangeName(transIn, numClients);
	}
	else if (transCode == "A")
	{
		AddServiceCount();
	}
	else if (transCode == "V")
	{
		ViewAllInfo();
	}
	return i;
}

I have now added to the if statement a call to a function which has a linear search. It finds them correctly and changes them if I do not have any other transactions in the Trans.data file. but the moment I add some if messus up immediately after the first other code.

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
using std::string;
const int MAXCLIENTS = 30;
const int MAXHISTORIES = 10;

int clientNumber[MAXCLIENTS];
int numHistories[MAXHISTORIES];
int year[MAXCLIENTS][MAXHISTORIES];
int serviceType[MAXCLIENTS][MAXHISTORIES];
int serviceCounts[MAXCLIENTS][MAXHISTORIES];
double chargeAmount[MAXCLIENTS][MAXHISTORIES];
string newClientName[MAXCLIENTS];
string clientName[MAXCLIENTS];
int numClients = 0;

void SetFormat(ofstream& fout);
void Header(ofstream& fout);
int ProcessLawnData(ifstream& fin);
int ProcessTransData(ifstream& transIn, int& numClients, int& i);
int ChangeName(int& target, int& numClients, int& i, string& newClientName);
void AddServiceCount();
void ViewAllInfo();

int main()
{
	//set ofstream and fstream to fin and fout
	ifstream fin;
	ofstream fout;
	ifstream transIn;
	int i =0;
	//open input streams and output stream
	fin.open ("Lawn.data");
	if (fin.fail())
	{
		cout << "Error opening Lawn.data";
		exit(1);
	}
	fout.open ("Report.out");
	if (fout.fail())
	{
		cout << "Error opening Report.out";
		exit(1);
	}
	transIn.open("Trans.data");
	if(transIn.fail())
	{
		cout << "Error opening Trans.data";
		exit(1);
	}
	SetFormat(fout);
	Header(fout);
	ProcessLawnData(fin);
	ProcessTransData(transIn, i, numClients);
	fin.close();
	fout.close();
	transIn.close();

 }

/************************************************************************************************/
/* Function Name: SetFormat									*/
/* Function Purpose: Setup formatting of fout							*/
/* Input Parameters: none									*/
/* Return value: none										*/
/************************************************************************************************/
void SetFormat (ofstream& fout)
{
	fout.setf(ios::fixed);
	fout.setf(ios::showpoint);
	fout.precision(2);
	return;
}  //end Set Format

/************************************************************************************************/
/* Function Name: Header									*/
/* Function Purpose: Output the header of file							*/
/* Input Parameters: none									*/
/* Return value: none										*/
/************************************************************************************************/
void Header (ofstream& fout)
{
	// output header to file
	return;
} //end Header

/************************************************************************************************/
/* Function Name: ProcessLawnData								*/
/* Function Purpose: Processes File Lawn.data							*/
/* Input Parameters: year, numHistories, clientNumber, clientName, serviceType, chargeAmount	*/			/*		     serviceCounts								*/
/* Return value: i										*/
/************************************************************************************************/
int ProcessLawnData(ifstream& fin)
{
	int i = 0; //holder for client in array
	int j = 0; //holder for service year
	
	//read file until last clientNumber is reached
	while (fin >> clientNumber[i])
	{
		
		//input the numHistories and clientName
		fin >> numHistories[i];
		getline(fin, clientName[i]);
		// input the service history until numHistories is reached
		for (j = 0; j < numHistories[i]; j++)
		{
			fin >> year[i][j];
			fin >> serviceType[i][j];
			fin >> chargeAmount[i][j];
			fin >> serviceCounts[i][j];
		}
		//move to next client
		i++;
		numClients++;
		
	}
	return i;
} //end ProcessLawnData
int ProcessTransData(ifstream& transIn, int& i, int& numClients)
{
	string newClientName;
	char transCode;
	int target;
	while(transIn >> transCode)
	{
		
		if (transCode == 'C')
		{
			transIn >> target;
			getline(transIn, newClientName);
			ChangeName(target, numClients, i, newClientName);
		}
		else if (transCode == 'A')
		{
			transIn >> clientNumber[i];
		}
		else if (transCode == 'V')
		{
			transIn >> clientNumber[i];
		}
		i++;
	}
}
int ChangeName(int& target, int& numClients, int& i, string& newClientName)
{
	cout << target;
	bool found = false;
	while((!found) && (i < numClients))
	if (target == clientNumber[i])
		found = true;
	else
		i++;
	if (found)
	{
		cout << "found\n";
		clientName[i] = newClientName;
		return i;
	}
	else
		cout << "not found";
		return -1;
}

This is what the Trans.data file looks like:

C 111 Changed Robert
A 112
C 113 Changed Dionne
C 112 Changed Jeff

This article has been dead for over six months. Start a new discussion instead.