(C++) - Associating strings with values read in from a file - c++

I am trying to get a value associate with a string inside a file called invoice1.txt
invoice1.txt
hammer#10.00
saw#20.00
So for example, when I lookup "hammer" I would like the expression to evaluate to 10.00.
My code so far
string search;
ifstream inFile;
string line;
double price;
inFile.open("invoice1.txt");
if(!inFile)
{
cout << "Unable to open file" << endl;
return 0;
}
else
{
int pos;
while(inFile.good())
{
getline(inFile,line);
pos=line.find(search);
if(pos!=string::npos)
{
cout<<"The item "<<search<<" costs: "// code to get the price
}
}
}
system("pause");
This is the output I am aiming for:
The item hammer costs: 10.00
The summerise, my question is:
How can I associate values with one another that are read in from a file, so I can get a price for an item without having to reparse the file and find it again?

This is what std::map is for.
What you want to do is break your problem down into multiple stages. Here is a simple set of steps that should help you (there are better ways, but I'm trying to keep things simple here).
I've added some lines to explain how to use std::map, in case you're not familiar.
Read the file line by line.
For each line that is read in, get the value after the '#' character.
Add the value to the map, using the string before '#' as the key...
priceMap[key] = price; // for example, this might evaluate to: myMap["hammer"] = 10.00
When you want to use the value, simple give the map you're key.
std::cout << priceMap["hammer"];

What do you search in line from file? You have to search for character # and split your string into two parts.
getline(inFile,line);
pos=line.find('#');
if(pos!=string::npos)
cout<<"The item "<<line.substr(0,pos)<<" costs: " << line.substr(pos+1,line.size()-1) << endl;// code to get the price
You can save item name and price in different variables if you want. If you want to do something more with a string, read this for further instructions.

Related

cash reg loading individual words from each line into the different struct members

I have this homework assignment that has me a little lost. I have been working on this assignment writing my code, erasing, rewrite, repeat etc. Here are the instructions:
Step (1)
Associated with this assignment is a data file called prices.txt. It contains details of the prices for items in a
grocery store. The first column is the barcode for the item (http://en.wikipedia.org/wiki/Barcode) the
second column, starting in position 11, is the product name, and the third column, starting in position 37 is
the product price.
Write a function with the signature int loadData() which prompts the user for the location of the data file
on disk and then loads the data from the file into an array of elements. Each element of the array should be
a struct declared as follows:
struct Item {
string code;
string name;
double price;
};
The array itself should be called items and should be declared at file scope as follows:
const int MAX_ITEMS = 100;
Item items[MAX_ITEMS];
Notice that the array has size 100, so you can assume the number of items in the data file is less than 100.
Therefore, when loadData has finished loading the data into the array some elements in the array will be unused. The function loadData returns the number of items actually loaded into the array.
I am unsure on how to attempt the function described in the instructions. My code:
int loadData () {
string inputFileName;
ifstream inputFile;
int numOfItems = 0;
cout << "Please input the name of the backup file: ";
cin >> inputFileName; //read user input for the location of the
//backup file
inputFile.open(inputFileName.c_str()); //open specified document
if (!inputFile.is_open()) { //If the file does not open the errormsg
cout << "Unable to open input file." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
exit(1);
}
//Not sure where to start. I know I need to get each element from
//each newline.
return numOfItems;
}
I wanted to figure this out on my own but that isn't gonna happen. So if I could just get some hints or even suggested pools of knowledge that would guide me or even give me an idea of where to start.
addition: input file:
10001 Apples (bunch) 4.59
10002 Bananas (bunch) 4.99
10003 Pears (bunch) 5.49
20001 White bread (loaf) 2.69
20002 Brown bread (loaf) 2.89
20003 English Muffins (bag) 3.99
30001 Sugar (5 lb bag) 3.99
30002 Tea (box) 4.29
30003 Folger's Coffee (Can) 13.29
This is the entire input file.
Since the input file seems to be using fixed-width columns, it's actually very easy to extract the fields. Just read one line at a time, and for each line get each element as a sub-string, and put into the structure members. And there are functions to convert strings to floating-point values as well.
Don't worry about the possible leading or trailing spaces, there are ways of trimming that.

How to extract specific substring from getline function in C++?

I'm fairly new to C++ so please forgive me if my terminology or methodology isn't correct.
I'm trying to write a simple program that:
Opens two input files ("infileicd" and "infilesel").
Opens a single output file "list.txt".
Compares "infilesel" to "infileicd" line by line.
If a line from "infilesel" is found in "infileicd", it writes that line from "infileicd" to "list.txt", effectively making a separate log file.
I am using the getline() function to do this but have run into trouble when trying to compare each file line. I think it might be easier if I could use only the substring of interest to use as a comparison.
The problem is that there are multiple words within the entire getline string and I am only really interested in the second one. Here are two examples:
"1529 nic1_mau_op_mode_3 "8664afm007-01" "1" OUTPUT 1 0 LOGICAL 4 4136"
"1523 pilot_mfd_only_sel "8664afm003-02" "1" OUTPUT 1 0 LOGICAL 4 4112"
"nic1_mau_op_mode_3" and "pilot_mfd_only_sel" are the only substrings of interest.
It would make it a lot easier if I could only use that second substring to compare but I don't know how to extract it specifically from the getline() function. I haven't found anything suggesting it is impossible to do this, but if it is impossible, what would be an alternative method for extracting that substring?
This is a personal project so I'm under no time contstraints.
Any assistance is greatly apprecated in advance. Here is my code (so far):
int main()
{
//Open the file to write the selected variables to.
ofstream writer("list.txt");
//Open the selected variabels file to be read.
ifstream infilesel;
infilesel.open("varsel.txt");
//Open the icd file to be read.
ifstream infileicd;
infileicd.open("aic_fdk_host.txt");
//Check icd file for errors.
if (infileicd.fail()){
cerr << "Error opening icd.\n" << endl;
return 1;
}
else {
cout << "The icd file has been opened.\n";
}
//Check selected variables file for errors.
if (infilesel.fail()){
cerr << "Error opening selection file.\n" << endl;
return 1;
}
else {
cout << "The selection file has been opened.\n";
}
//Read each infile and copy contents of icd file to the list file.
string namesel;
string nameicd;
while(!infileicd.eof()){
getline(infileicd, nameicd);
getline(infilesel, namesel);
if (nameicd != namesel){ //This is where I would like to extract and compare the two specific strings
infileicd; //Skip to next line if not the same
} else {
writer << nameicd << namesel << endl;
}
}
writer.close();
infilesel.close();
infileicd.close();
return 0;
}
So, based on what we discussed in the comments, you just need to toss the stuff you don't want. So try this:
string namesel;
string nameicd;
string junk;
while(!infileicd.eof()){
// Get the first section, which we'll ignore
getline(infileicd, junk, ' ');
getline(infilesel, junk, ' ');
// Get the real data
getline(infileicd, nameicd, ' ');
getline(infilesel, namesel, ' ');
// Get the rest of the line, which we'll ignore
getline(infileicd, junk);
getline(infilesel, junk);
Basically, getline takes a delimiter, which by default is a newline. By setting it as a space the first time, you get rid of the first junk section, using the same method, you get the part you want, and then the final portion goes to the end of the line, also ignoring it.

Parse from a file, where one of the columns has multiple value

I'm trying to load the information from a text file into a vector. However, one of the columns has multiple values in it, and I'm having problem trying to retrieve the information from there. Below is my snippet code.
The text file will have format like this:
First column will be the name, and second column will be all the classes that he is taking right now. For the second column, I create a vector to hold it, but I don't know how to do a while loop condition for it. Can anyone help me please ?
mtingley |art, music, math, history
while(getline(inUsers, textLine))
{
Student s;
string delimeter;
// put the line into buffer string
istringstream textStream(textLine);
// get userName
textStream >> userName;
// read the buffer string till '|'
getline(textStream, delimeter, '|');
cout << userName << endl;
s.SetUserName(userName);
while() // need condition in this while loop
{
textStream >> subject;
getline(textStream, delimeter, ',');
vCourse.push_back(subject);
}
}
There is a good example here: http://www.daniweb.com/software-development/cpp/threads/53349/getline-with-multiple-delimiters

Input from text file, not displaying correctly, why?

Here is some part of my main:
int main() {
Inventory Master;
bool flag;
Customer Bob("Bob", "CreditCard.txt");
Customer Chris("Chris", "CreditCard.txt" );
}
Here is my method:
Customer::Customer( string n, string fileName ) {
name = n;
ifstream Credit;
Credit.open(fileName.c_str(), ios::in);
while( Credit.good() && !Credit.eof() ) {
Credit >> card >> balance >> ws;
cout << card <<"\t" << balance << endl;
}
CreditCard _CC( int card, double balance);
}
Here is my "CreditCard.txt file:
12345 15.00
32564 20.00
The way I wanted the info to display is have line 1 "12345 15.00" assigned to Bob and line 2 assigned to Chris and do that so on and so forth if i make new instances or objects of a customer. However the way I currently implemented it is it keeps assigning "12345 15.00 and 32564 20.00" to both Bob and Chris. I could appreciate the help if someone could SHOW me how to somehow point to certain lines of the text file so Bob is assigned to line 1, Chris to line 2, and more customers to other lines when i add them in the text file.
Everything you're doing to Bob and Chris happens inside the constructor. So, as written, your code says: while the stream is in good condition and it's not the end of the file(key point), write to here.
Well, if you think about it, this will read until the end of the file is reached for each instance of Customer. That's not what you want. I might suggest adding the name as the first field in the data file for each record. You could then search the file for the correct record, assuming you ensure the names are all uniquely defined, then pull the data out string by string. That way it's not reading from the beginning to the end each time. I added "Bob" as the first field on line 1, and "Chris" to line 2 and made string name = "Chris";. So...
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string tempStr;
string name = "Chris";
ifstream Credit;
Credit.open("Info.txt", ios::in);
while( Credit.good() && !Credit.eof() )
{
getline(Credit, tempStr, ' ');//Reads the first records name field
cout << tempStr << endl;
if(name.compare(tempStr) == 0)//Compares the "name" to the field.
{ //If true they are the same
//Proceed to do reading and assignments with additional getline statements
cout << "Chris was matched the second time around!";
Credit.setstate(ios::eofbit, true);//***Sets eof to true
}
else
{
Credit.ignore(50, '\n');
//That should put Credit in the proper position to read the next name
}
}
}
The way you're doing it will cause problems. The only way that it would work for sure is if you knew where the record was at in the file. What if you had five records? By the time you got to the third one you would have to ignore, or similar, all the fields prior to the one you're working on. Also, it could be handy for a human to read a print out of the data file. Another reason to provide a label(name) to each record. Also, you're apparently using namespace std;, so I did too, but it's frowned upon.
istream.getline() http://www.cplusplus.com/reference/iostream/istream/getline/ could be your answer. Just read one line at a time.
A little example here:
http://www.cplusplus.com/forum/beginner/27799/
Little Example from one of my old homerworks:
ifstream fin(fileName);
char buffer[256];
int count = 0;
if (fin.is_open())
{
while (!fin.eof())
{
fin.getline(buffer, 256);
}
}

search for specific row c++ tab delmited

AccountNumber Type Amount
15 checking 52.42
23 savings 51.51
11 checking 12.21
is my tab delmited file
i would like to be able to search for rows by the account number. say if i put in 23, i want to get that specific row. how would id do that?
also more advance, if i wanted to change a specific value, say amount 51.51 in account 23. how do i fetch that value and replace it with a new value?
so far im just reading in row by row
string line;
ifstream is("account.txt");
if (is.is_open())
{
while (std::getline(is, line)) // read one line at a time
{
string value;
string parseline;
std::istringstream iss(line);
getline(line, parseline);
cout << parseline << endl; // do something with the value
while (iss >> value) // read one value at at time from the line
{
//cout << line << " "; // do something with the value
}
}
is.close();
}
else
cout << "File cant be opened" << endl;
return 0;
Given that each line is of variable length there is no way to index to particular row without first parsing the entire file.
But I suspect your program will want to manipulate random rows and columns. So I'd start by parsing out the entire file. Put each row into its own data structure in an array, then index that row in the array.
You can use "strtok" to split the input up into rows, and then strtok again to split each row into fields.
If I were to do this, I would first write a few functions that parse the entire file and store the data in an appropriate data structure (such as an array or std::map). Then I would use the data structure for the required operations (such as searching or editing). Finally, I would write the data structure back to a file if there are any modifications.