Help with comparing strings and substrings?

Hello, I am trying to write a code in which the user enters a normal number like 1 or 4 and the program searches a text file for the corresponding the number. The file looks like this:
number#value
1#23400
4#17000
5#21000
6#12600
9#26700

How do I get the program to read a line only up until the # and recognize that that is the corresponding number and then cout the value?
So here's how I'd do it:

1
2
3
4
5
6
ifstream inputFile;
inputFile.open("filename.txt");

string getData;
getline(inputFile, getData); // This gets the entire line and stores it as string in string getData
string num = getData.substr(2,5); // This extrats chars from index 2 (first digit) until 5 spaces   (which is last digit of you number) 


http://www.cplusplus.com/reference/string/string/substr/

You can put that into a loop and do this for each line (i.e each number).
If you want to convert the extracted number from string type to int, you can do so by using stoi
function. http://www.cplusplus.com/reference/string/stoi/

Hope this helps!
To get two numbers, you could use
1
2
3
4
while (cin >> x) {
	cin.ignore(256, '#');
	cin >> y;
}


It may be best to put those into a vector or map & search through that. I'm not too familiar with searching in files.
What is the code before the '#' is two digits and same with the number after#.
This is what I have so far

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

int main()
{




int code;



ifstream inputfile;
inputfile.open("Payroll.txt");




do
{


cout << "Hello this program will display a salary with its corresponding code number." << endl;
cout << "Please enter your code number with '#' at the end of it. ";
cin >> code;

while (!inputfile.eof())
{
string getdata;
string coderead;
getline(inputfile, getdata);
coderead = getdata.substr(0,2);
cout << coderead;

}
}
while( code !=0);


}
Last edited on
Here's something I wrote for you quickly, but I hope this gives you a better understanding on how you could do something like this in the future. Please ask any questions if needed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream> // for ifstream
#include <string> // for getline

int main()
{
	const char INDEX_SYMBOL = '#';
	std::ifstream file("Payroll.txt", std::ifstream::in);
	std::string tempString = "";
	
	char index = '0';
	bool indexFound = false;

	if (file.is_open() == true)
	{
		std::cout << "Hello this program will display a salary with its corresponding code number.\n";
		std::cout << "Please enter your code number: ";
		index = getchar();

		while (file.eof() == false && indexFound == false)
		{
			// Read file line
			std::getline(file, tempString);

			// Create unique index string
			std::string uniqueIndex = "";
			uniqueIndex += index;
			uniqueIndex += INDEX_SYMBOL;

			// Check if the line contained the unique index
			// If the index was found display the data
			// and end the program
			if (tempString.find(uniqueIndex) != std::string::npos && file.good() == true)
			{
				std::cout << "\nSalary:\n";
				std::cout << tempString.substr(2, tempString.size() - 2) << std::endl;
				indexFound = true;
				break;
			}
		}

		file.close();
	}
	else
	{
		std::cout << "Error: Couldn't open file\n";
		return 0;
	}

	if (indexFound == false)
		std::cout << "\nThe index did not exist in the file!\n";

	return 0;
}
Topic archived. No new replies allowed.