Searching a file for a string, then outputting a float

Good day,

I am working on a program for our business where the user inputs a hose assembly number and the program outputs the price.

What I have managed to do so far is to take the input and break it into the the components that make up this hose assembly. This compoents are stored as strings.

What I would like to do is take the components stored as separate strings, search a text file like such, where the first column is the component number and the second is the price:
302-6 7.80
302-8 8.95
10643-6-4 11.30
10643-6-6 12.35
10643-8-6 13.15
10643-8-8 15.85
Then I want to take the price that is immediately after it and store it as a float.

For example I want search for 10643-8-8 stored in string named hose_end1_no and then find and store 15.85 in a float named hose_end1_price. Likewise for 302-8 stored in hose_no and store 8.95 in hose_price.

I hope this makes sense. If it doesn't I will try to clarify. Thanks for any help you can give.

Troy
> I am working on a program for our business

I.e., it's a homework problem.

What do you have so far?
Do you know how to open a file for input?
Do you know how to read a line from the file?
Nope, this really is for business, our hydraulic repair shop. I want to create an application that will quickly price out a hose assembly by reading the components from a file instead of selecting the commonents individually like we do now.

Anyway, my days of doing homework problems are long gone

Back to my problem:
I know how to open a file for input/output, either by creating a ostream object, or by using fopen with fscan.
However, what I don't know how is to scan a file for a string and then be able to store the data after the string into a float.
You can use an istringstream to convert a string to pretty much anything. It works just like any other istream, so you can use the >> operator. Here's a quick demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
	float x = 0.0;
	string s = "4.5";
	istringstream iss(s);
	iss >> x;

	cout << x << endl;

	return 0;
}
Last edited on
I have a few questions if this really is not a homework assignment. Please don't take any offense. I am just trying to understand your situation.

A) Why do you think C++ is is the right language for this project?
B) Why are you storing pricing information in a flat file rather than a database?
C) Why are you, someone asking programming questions on a beginner forum, writing this application?
Indeed. For this kind of low performance application an interpreted language would be more suitable. Less concern for low-level details and access to more built-in features. Python is the interpreted language of the now, so I'm going to suggest it.
And I'll suggest Perl, since I know Perl. ;) It could probably be done in about 5 lines!

OP> Anyway, my days of doing homework problems are long gone

Clearly these weren't programming homework problems.
I'm with Helios on this one. Heck, you could do a nice job using Django with very little programming at all, and have a nice web front-end for the user maintaining the prices and those checking them.
Geez, peeps, why not just give some help?

troyjp80
You'll have to read the file, line by line, until you find a line that begins with the part number. A simple string match should do that. Then you can extract the price from the line using the stringstream that Tevsky mentioned. Remember also you can use the substr() method of the string class to get the price by itself when you convert it using the stringstream...

Good luck!
Thanks for all the help, I got it to do what I needed

I chose C++ since it is the only language I know. The first two and a half years of college I studied computer science where they taught us C++. Then I switched to mathematics. That was about 7 years ago. Now I'm working at my dad's hydraulic repair shop doing misc stuff and I thought it would be a nifty idea to be able to price out a hose assembly with just inputting hose number.

The first part was to take the input as a string and break it down into the components and store them as strings, as such:

Enter Hose Assembly Number
>>F3020639060808-12
302-8 (Hose type & size)
10643-6-8 (hose end 1)
13943-8-8 (hose end 1)


The next part was the part that I was stuck on. I wanted to search a simple text file for the components and store the price next to it as a float. I wanted to use a text file just for the fact it was simple. I could copy & past the data from our database into the text file.

But thanks to find() was able to scan the file and locate the component, then with substr() I was able to extract the data I needed, and then with istringstream I converted the price from a string to a float.

Here's the code I came up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    string line;
    string price_str;
    float price = 0.0;
    string hose_end ("10643-8-8") //just to test it;
    ifstream hose_file("hose.txt");
    if (hose_file.is_open())
    {
        while (! hose_file.eof() )
        {
            getline (hose_file,line);
            if(line.find(hose_end) != string::npos)
            {
                cout << line << endl;
                price_str = line.substr(hose_end.length(),line.length()-hose_end.length());
                istringstream iss(price_str);
                iss >> price;
                cout << price;
            }
        }
        hose_file.close();
    }
    else cout << "Unable to open file";


Once again, thank for those who helped
Topic archived. No new replies allowed.