Im a beginner to c++
after looking over this code you will be able to tell
the outfile code seems to work fine no errors
but any feedback will be nice
the infile code is the one i have problems with

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


int main()
{
string name;
string address;
string number;

	ofstream outfile;
	outfile.open("white.txt", ofstream::out | ofstream::app);

	
	do
	{
	cout << " Upper Case 'Q' to Quit Or" << endl;
	cout << " Enter persons full name : ";
	getline(cin,name);
	
	if ( name == "Q" )
			break;

	cout << "\n Enter persons address : ";
	getline(cin,address);
	cout << "\n Enter persons phone number : ";
	getline(cin,number);
	
	
	cout << address << "  " << name << "  " << number << endl;
	
		
	outfile << name << endl;
	outfile << address << endl;
	outfile << number << endl;
	
	
	}
	
	while ( name != "Q" );

	outfile.close();

	return 0;
}

I cant seem to figure out a way to find() a string in the file and then display the name, address, and number

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
const int size = 10;
int main()
{
	string line, name;


	char file[size];
	ifstream infile;

	cout << " enter file name :";
	cin.getline(file,size);
	infile.open(file);
if(!infile.is_open())
{
	cout << " could not open file" << file << endl;
	cout << " program Terminating....\n";
	exit(EXIT_FAILURE);
}

cout << " enter name of person";
cin >> name;



if(infile)
{
	while ( getline ( infile , line ))
	{
		
			line.find(name);
			if ( line == name )
			{
				cout << line;
		
			}
	}
}



infile.close();

	return 0;
}

this only will find the name typed in and display that name

if(infile)
{
	while ( getline ( infile , line ))
	{
		
			line.find(name);
			if ( line == name )
			{
				cout << line;
		
			}
	}
}

any help would be great!!!

Your problem is the way you are shifting your strings in the ostream

outfile << name << endl;
	outfile << address << endl;
	outfile << number << endl;

address and number are not connected to the name in any way.

note: avoid using global variables, declare these in main (or some other function)

string name;
string address;
string number;

ok i made them local but what do you mean not connected in any way?
sould i take out the endl;

Test your program :) write the informations of a few imaginary people in your text file and open it. You should see the names, addresses and numbers on different lines.

TL;DR remove the endl

i removed the endl;
and all text is on the same line in the file but when i search for a string using the find() it does not display anything it just ends the program
the file is formatted like this
name address number
name address number
ect...

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