-4

So. I have not found good documention online about my question. I have a file, "Item Documentation.txt" Ignore the // comments, those will not be there later

0001 //Item ID
5.67 //Item Price
Eggs 12x //Item Name

0002
3.12
Milk Whole Gallon

I want to make a program to search for the Item ID, and then grab the following item price as a double and item name as a string. How could this be done.

I need to preface that I am very much still learning c++.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • 2
    *I have not found good documention online about my question* -- What documentation were you looking for? You write a program using `std::ifstream`, read each line into a string, and figure out what that line of data you just read denotes. You will not find on the Internet "ready-made" programs that perfectly fits your focused description. – PaulMcKenzie Sep 16 '22 at 15:36
  • Refer to a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Jason Sep 16 '22 at 15:39
  • Start at opening the file. Then work on reading the file. Searching should be a more advanced topic after you have mastered more basic code. – drescherjm Sep 16 '22 at 16:30
  • I know how files work. You are all completely missing the point. "search for the Item ID, and _then grab the following item price as a double and item name as a string_" – Cyrus Bellefeuille Sep 17 '22 at 01:00
  • You didn't show any code so we can't help you fix it or give you hints on how to continue. Please add your [mcve] and explain the problem you have with the code that you have written. We don't just write your code for you. Also you need your question to focus on a single problem. StackOverflow is not a user support forum. Instead its a Q&A and the main purpose of your question is to help future readers solve the same 1 problem. – drescherjm Sep 17 '22 at 12:58

1 Answers1

0

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.

konray
  • 1
  • 1