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!!!

Dani AI

Generated

Quick diagnosis and what went wrong (ties to and )
Removing the newlines as suggested changed the file layout, but that alone doesn't fix searching. Two bugs stand out in the existing code: calling line.find(name); without checking its return value does nothing, and testing line == name only succeeds when the whole line exactly equals the query. Decide on a consistent on-disk format first, then parse to match it.

Two practical, safe formats and how to search them

  1. One-record-per-line with a delimiter (best if names/addresses contain spaces). Write records like:

    Name|Address|Phone

    Then read each file line, split on |, and compare the name field (use string::find for substring matches or == for exact). Using istringstream + getline(..., delimiter) works well; see the istringstream docs for details (https://en.cppreference.com/w/cpp/io/basic_istringstream) and string::find behavior (https://en.cppreference.com/w/cpp/string/basic_string/find).

  2. Multi-line records (the three-lines-per-person approach). If you keep that format, when you match a name line, immediately getline two more lines for address and phone. Be careful to check EOF so you don’t read past the file.

Small tips and troubleshooting checklist

  • Check the return of find: compare to std::string::npos.
  • Trim trailing/leading whitespace before comparing, or normalize case if you want case-insensitive search.
  • Prefer std::string + std::getline for console/file names instead of fixed-size char buffers.
  • If mixing operator>> and getline, consume the leftover newline (std::getline will read an empty line otherwise).
  • Pick one file format and write code to parse that format; inconsistent formats cause the exact symptoms seen here.

These changes will make searches reliable and let you print the associated address and number once a name is found.

Recommended Answers

All 4 Replies

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...

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.