You will want to use std::getline(some_open_file, some_string)
. Assuming you do not care for efficiency, you can simply check every line in the file against your item ID, then output the following line.
#include <fstream>
using string = std::string;
int main() {
string item_id;
string some_string;
double item_price;
string item_name;
std::ifstream some_file("Item Documentation.txt");
while (std::getline(some_file, some_string)) { // place the input into a string
if (some_string == item_id) {
std::getline(some_file, some_string); // grab the item price following it
item_price = std::stod(some_string); // std::stod will extract a double from your string
std::getline(some_file, some_string); // pulls the item name following that
item_name = some_string;
break;
}
}
// then simply process your data here, outside the loop
//
return 0;
}
This works because every time getline()
is called, it goes to the next line in the file.
Alternatively, you could place all your items into some data structure using the same while loop without the if statement, then simply use the builtin comparators to check for your item ID.