Searching a File with C++

Writing to a .txt file then searching it

Page 1 of 1

7 Replies - 1668 Views - Last Post: 09 June 2009 - 04:43 PM Rate Topic: -----

#1 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Searching a File with C++

Post icon  Posted 06 June 2009 - 09:38 AM

We are looking for a basic search function to search a text file after we input the information into that file. We need to conduct a search by date that shows all records within the given dates. Also search in the same function that would give us the total number of a specific service completed. Another generic search where the user can input customer name and it will return all the information for that customer.

By the way the current code works and will run, the search funtions are not complete. You can run it to see how it writes to the file...

Please help! Thanks you, in advance.

#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <time.h>

using namespace std;

//Declare the constant variables
const double OIL_CHANGE = 19.95;
const double FULL_OIL_CHANGE = 34.95;
const double TRANS_FLUID = 109.99;
const double RADIATOR_FLUID = 69.99;
const double DIFFERENTIAL = 79.99;
const double SALES_TAX = .06;

void getCusInfo();
void getSerInfo();
void searchSerCompleted();
void searchCurrentCus();

int main()
{
	//Declare Variables
	int userChoice;
		
	//The main menu
	cout << "**********************************" << endl;
	cout << "*	Quick Oil Change Station	*" << endl;
	cout << "************MAIN MENU*************" << endl;
	cout << "*								*" << endl;
	cout << "* 1 - Add a New Customer		 *" << endl;
	cout << "* 2 - Complete a Service		 *" << endl;
	cout << "* 3 - Search Services			*" << endl;
	cout << "* 4 - Search For a Customer	  *" << endl;
	cout << "*								*" << endl;
	cout << "**********************************" << endl;
	cout << "Please choose the service used: ";
	cin >> userChoice;
	cout << endl;

	switch (userChoice)
	{

	case 1:
	//get the input from the user
	getCusInfo();
		break;
	
	case 2:
	//get services to be completed
	getSerInfo();
		break;

	case 3:
	searchSerCompleted();
		break;

	case 4:
	searchCurrentCus();
		break;
	}
	return 0;
}

/*
pull the system date
use a counter for each service
get a grand total for each service used
query sales and services by date ranges
*/
void getCusInfo()
{
	ofstream cusOutFile;
	//open the file to write customer information to
	cusOutFile.open("customer.txt", ios::app);

	cusOutFile << fixed << showpoint;
	cusOutFile << setprecision(2);

	string fname, lname, home, cell, work, plateNum;
	int confirm=0;
	while (confirm != 1)
	{
	cout << "First Name: ";
	cin >> fname;
	cout << "Last Name: ";
	cin >> lname;
	cout << "Home Phone: ";
	cin >> home;
	cout << "Cell Phone: ";
	cin >> cell;
	cout << "Work Phone: ";
	cin >> work;
	cout << "Plate Number: ";
	cin >> plateNum;
	cout << endl << endl;

	//output displayed to user
	cout << "Name:		" << fname << " " << lname << endl;
	cout << "Home Phone:	"  << home << endl;
	cout << "Cell Phone:	" << cell <<endl;
	cout << "Work Phone:	" << work << endl;
	cout << "Plate Number:	" << plateNum << endl;
	cout << endl;
	cout << "Is this correct?  (1=yes, 2=no)";
	cin >> confirm;
	cout << endl << endl;
	//write the data to the file
	if (confirm == 1)
	{
	cusOutFile << "Name:		" << fname << " " << lname << endl;
	cusOutFile << "Home Phone:	"  << home << endl;
	cusOutFile << "Cell Phone:	" << cell << endl;
	cusOutFile << "Work Phone:	" << work << endl;
	cusOutFile << "Plate Number:	" << plateNum << endl;
	cusOutFile << "--------------------------------" << endl;
	}
	}
	cusOutFile.close();
}
void getSerInfo()
{
	int index, num, counter = 0;
	double subTotal1 = 0.0, subTotal2 = 0.0, subTotal3 = 0.0, subTotal4 = 0.0, subTotal5 = 0.0;
	double grandSubTotal, salesTax, totalDue;

	//print the date and time
	char dateStr[9];
	char timeStr[9];
	_strdate( dateStr);
	printf("The current date is %s \n", dateStr);
	_strtime( timeStr);
	printf("The current time is %s \n", timeStr);
	cout << endl;
	cout << endl;

	ofstream serOutFile;

	//open the file to write customer information to
	serOutFile.open("services.txt", ios::app);

	serOutFile << fixed << showpoint;
	serOutFile << setprecision(2);

	cout << "How many services were completed: ";
	cin >> index;
	cout << endl;

	//display the services menu to the user
	cout << "*****Quick Oil Change Station*****" << endl;
	cout << "*								*" << endl;
	cout << "************MAIN MENU*************" << endl;
	cout << "*								*" << endl;
	cout << "* 1 - Basic Oil Change		   *" << endl;
	cout << "* 2 - Full Service Oil Change	*" << endl;
	cout << "* 3 - Transmission Fluid Service *" << endl;
	cout << "* 4 - Radiator Fluid Exchange	*" << endl;
	cout << "* 5 - Differential Service	   *" << endl;
	cout << "*								*" << endl;
	cout << "**********************************" << endl;
	cout << "Please choose the service used: ";

	//collect the prices for the services used
	do 
	{
	cin >> num;
	counter++;
	switch (num)
	{
	case 1:
		subTotal1 += OIL_CHANGE;
		break;
	case 2:
		subTotal2 += FULL_OIL_CHANGE;
		break;
	case 3:
		subTotal3 += TRANS_FLUID;
		break;
	case 4:
		subTotal4 += RADIATOR_FLUID;
		break;
	case 5:
		subTotal5 += DIFFERENTIAL;
	}

	//total the services
	grandSubTotal = subTotal1 + subTotal2 + subTotal3 + subTotal4 + subTotal5;
	}

	while (counter < index);

	//time stamp for the file
	serOutFile << "Date: " << dateStr << " : ";
	serOutFile << "Time: " << timeStr << " : ";

	//determine which services to print to the file
	if (subTotal1 > 0.0)
		serOutFile << "Oil Change : ";
	if (subTotal2 > 0.0)
		serOutFile << "Full Oil Change : ";
	if (subTotal3 > 0.0)
		serOutFile << "Transmission Fluid Service : ";
	if (subTotal4 > 0.0)
		serOutFile << "Radiator Fluid Exchange : ";
	if (subTotal5 > 0.0)
		serOutFile << "Differential Service : ";
	serOutFile << endl;

	//Calculate the sales tax and the total due
	salesTax = grandSubTotal * SALES_TAX;
	totalDue = grandSubTotal + salesTax;

	//Display the output to the user
	cout << "The subtotal is............$" << grandSubTotal << endl;
	cout << "The sales tax is...........$" << salesTax << endl;
	cout << "The total due is...........$" << totalDue << endl;

	serOutFile.close();
}

void searchSerCompleted()
{
	int searchSerPref;
	ifstream serInFile;
//open the file to read customer services from
	serInFile.open("services.txt");
	cout << "******SEARCH SERVICES COMPLETED*****" << endl;
	cout << "*								  *" << endl;
	cout << "*  1 - Basic Oil Change			*" << endl;
	cout << "*  2 - Full Service Oil Change	 *" << endl;
	cout << "*  3 - Transmission Fluid Service  *" << endl;
	cout << "*  4 - Radiator Fluid Exchange	 *" << endl;
	cout << "*  5 - Differential Service		*" << endl;
	cout << "*  6 - Search By Date			  *" << endl;
	cout << "*								  *" << endl;
	cout << "************************************" << endl;
	cout << "Please choose how to conduct your search: ";
	cin >> searchSerPref;

	switch (searchSerPref)
	{
	case  1:
		break;

	case 2:
		break;
	case 3:
		break;
	case 4:
		break;
	case 5:
		break;
	case 6:
		break;
	}
	cout << "you think!??!?!";
	//close the service files
	serInFile.close();
}

void searchCurrentCus()
{
	int searchSerPref;
	ifstream cusInFile;
//open the file to read customer information from
	cusInFile.open("customer.txt");
	cout << "**********CUSTOMER SEARCH***********" << endl;
	cout << "*								  *" << endl;
	cout << "*  1 - First Name				  *" << endl;
	cout << "*  2 - Last Name				   *" << endl;
	cout << "*  3 - Plate Number				*" << endl;
	cout << "*  4 - Phone Number				*" << endl;
	cout << "*  5 - Date						*" << endl;
	cout << "*								  *" << endl;
	cout << "************************************" << endl;
	cout << "How would you like to search for the customer?  ";
	cin >> searchSerPref;
	cout << endl << endl;
	switch (searchSerPref)
	{
	case  1:
		break;

	case 2:
		break;
	case 3:
		break;
	case 4:
		break;
	case 5:
		break;
	case 6:
		break;
	}
//close the customer files
	cusInFile.close();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Searching a File with C++

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Searching a File with C++

Posted 06 June 2009 - 09:47 AM

If you were to perform the search manually, how would you do it?
Was This Post Helpful? 0
  • +
  • -

#3 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Re: Searching a File with C++

Posted 06 June 2009 - 10:03 AM

View PostOler1s, on 6 Jun, 2009 - 08:47 AM, said:

If you were to perform the search manually, how would you do it?


First we would look at the file, find the dates asked for, then give the requested information back to the user.
EX. We would search the string "date" - 05/20/09 and any services performed on this date within the text file would be displayed to the user.

For the second search we would count how many times the service type is entered into the file and return the total amount.
EX. We would search the string "oil change" and the search would count each time the string "oil change" was written in the file and then return the total number of oil changes to the user.

Third search, we would search for the string "fname" or "lname" asked for by the user, and read the line back to the user, display all info for the record.
EX. The user asks to find the string "amanda" then the program would return all the records containing the string "amanda".

Let us know if you need more information... We have an idea of how to read from records, but not in C++.
(getline, putback, imports, etc... ???)
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Searching a File with C++

Posted 06 June 2009 - 10:30 AM

I was actually looking for a different kind of answer, but your post did give valuable information in any case.

I can't tell if this is an academic assignment, or what, but I want to note that the queries you want to be able to do are best managed by a database. Especially if you intend for this application to be in practical use with reasonably regular usage, then you want to be taking advantage of a database here. I wouldn't be writing a C++ application in that case, too.

Constructing a database yourself is no trivial task either, so I'll assume you're content with doing a naive search for your three types of queries. On small files, or academic assignments, the approach I'm hinting at is acceptable. When I asked you about performing the search manually, it wasn't really so much a question that I couldn't answer, so much as a question I want you to consider.

You've given here a relatively high level description of what you want accomplished. "Look" at the file is a high level description. It's something you as a human can accomplish. On that note, it isn't something specific to programming. If you want to look at a file, open it up in Notepad. There's no programming involved here, just using a computer.

I want you in particular to imagine you are a computer application. Thus, you can't take actions like "look" at a file. Rather you're restricted to behaviors that other applications have to do. For example, you can read a file. But remember that reading a file is nothing like a human looking at a file (which involves using another application to view the file). Data is presented like a stream. You have a long line of data, and you walk through it block by block.

Because all three types of searches will use the same concepts to accomplish their ideas, I will start with what the easiest one to consider first. That's the third search you described. That is given a first name, or a last name, find the record (if it exists) that matches that name and display the details to the user. To figure out how to do this, consider the following questions in order:

(1) If you were asked to repeatedly show all records to the user, one by one, how would you accomplish it?
(2) If in addition to the above, you needed to indicate whether each record you showed was that belonging to "amanda" (first name), how would you do it?
(3) Then, if you have answers to 1 and 2, how would you restrict displaying records, so that only the one belongs to "amanda" was shown?
Was This Post Helpful? 1
  • +
  • -

#5 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Re: Searching a File with C++

Posted 06 June 2009 - 12:18 PM

View PostOler1s, on 6 Jun, 2009 - 09:30 AM, said:

(1) If you were asked to repeatedly show all records to the user, one by one, how would you accomplish it?
(2) If in addition to the above, you needed to indicate whether each record you showed was that belonging to "amanda" (first name), how would you do it?
(3) Then, if you have answers to 1 and 2, how would you restrict displaying records, so that only the one belongs to "amanda" was shown?


I greatly appreciate the direction and guidance that you are providing, however the questions that you pose are the same ones I am asking myself. I have reached a rut and over the past 4 days have not been able to find an example of how to read data that has been sent to a file (i.e. search the file) I am unaware and do not poses the knowledge of where to go from here. Any help would be greatly appreciated. Here is my best attempt at answering your questions off the top of my head...

(1)To repeatedly show the records I would initiate a do/while loop to read the data in from the file and print it to the screen until the bottom/end of the file was reached.

(2)To indicate if the record belonged to "amanda", within the loop I would check to see if (variable) string strName was equal to "amanda". If so I would display all the information on the screen that was associated with that name.

(3)To restrict all the information being displayed to only one record I would displaying all the information until the value of strName no longer equaled "amanda". Meaning that a new record has been reached.
Was This Post Helpful? 0
  • +
  • -

#6 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: Searching a File with C++

Posted 06 June 2009 - 01:05 PM

Hi mate, i know your looking for a search in a file option.

I was wondering why your not using a container to store your data ie vector

there lots of functions to allow you to search threw the data and find what you are looking for. Be alot easier aswell

You should consider this, spend an hour and save days
Kevin





http://www.cplusplus...nce/stl/vector/
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Searching a File with C++

Posted 06 June 2009 - 08:20 PM

Quote

I greatly appreciate the direction and guidance that you are providing, however the questions that you pose are the same ones I am asking myself.
That's good. Although, as you've just seen here, telling us not only your problem, but what you are thinking and how you tried to tackle the problem is helpful. Taking the time to give us that information gets you your answer faster in the end.

Quote

have not been able to find an example of how to read data that has been sent to a file (i.e. search the file)
Reading and searching are not the same thing. You won't get copy-paste direct examples for what you need, but surely you aren't suggesting that you couldn't get any direction at all? Looking up in your C++ book for reading files, looking at online references, directly searching for "C++ search file" or "C++ read file" get you plenty of directions, and suggestions on what functions to use. Quite a bit of information is in other forum posts asking the same kind of question you ask now.

I know you don't have the answer on you, but what's important is that you can take small steps towards it. There's a bit of code and thinking involved before you can finally achieve "searching a file". When I asked you the three questions, they weren't a test of your knowledge. They were a suggestion of how to approach the problem. At least, that's how I would have done so. I didn't want off the top of the head answers. They aren't something you would answer off the top of the head, unless you have sufficient experience.

Start with the simplest thing possible. Looking at what you have so far, you have a function called searchCurrentCus() where you want to implement this search feature. Let's attempt to do case 1 in that function. We don't have a clue as to how to do the search yet, but here's a good bet. It involves reading the file.

So here's a first task. Read a line from the file and display it. That's all you have to do.

EDIT: To clarify, for case 1, read a line, display the line. If you can do that successfully, post the output here, so I can see it. And of course, if you have errors copy paste the errors.

This post has been edited by Oler1s: 06 June 2009 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Searching a File with C++

Posted 09 June 2009 - 04:43 PM

I didnt read all of it but im guessing your problem is searching the txt file.

i guess you could just do this:
cin>>mySEARCH;
while(getline(myFile,line,'~'))
{
  count++;
  if(mySEARCH == line);
	{cout<<count<<endl;
	  if(count == count+1) // increase the one to go to next line
		{myFile<<"WRITE THIS TO THE NEXT LINE"<<endl;}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1