0

My question is concerning how I can get the portion where I'm input streaming from the txt file to properly loop through the text file and compare the integers and then get the next line the txt file looks something like this. I'm supposed to create this as a function and use the return values in another code. However, I can't figure out to loop through the txt file properly as my code currently returns the product code once properly in the format needed but then returns the else statement every time after.

323
notebook
987
sweater
123 
water bottle
#include <iostream>
#include <fstream>
#include <string>
#include "inventory.hpp"

using namespace std;


string locateProduct (int num)
{

  int line;
  string productName;
  ifstream myfile;
  myfile.open("inventory.dat");


  if(!myfile)
  cout << "file failed to open";


  while(myfile >> line)
  {
    if(line == num)
    {
      cin.ignore();
      getline(myfile,productName);
      cout << "Product code " << num << ": " << productName << endl;
      myfile.close();
      return productName;
    }
    else
    {
      cout << "Code not found, try again." << endl;
      myfile.close();
      return "NOTFOUND";
    }
  }

}

This where I'm calling the function in another file:

int productcode;
do
  {
    cout << "Enter product code (-1 to end): ";
    cin >> productcode;
    cout << endl;
    if (productcode != -1)
    {
        locateProduct(productcode);
    }
  } while(productcode != -1);

output =
Enter product code (-1 to end): 209

Code not found, try again.
Enter product code (-1 to end): 323

Product code 323:
Enter product code (-1 to end):

However, 209 is a valid number and 323 should print out notebook.

Ted Klein Bergman
  • 7,245
  • 3
  • 20
  • 40
Sangergtx
  • 3
  • 3
  • 3
    Your mistakes: 1. [c++ - Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong? - Stack Overflow](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) 2. [c++ - Why does std::getline() skip input after a formatted extraction? - Stack Overflow](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – MikeCAT Dec 06 '20 at 00:56

1 Answers1

0
  • Using while(!myfile.eof()) is not a good idea. Instead of that, you should check if readings are successful and exit from the loop when it failed to read.
  • Using std::getline after >> operator of ifstream will cause trouble because >> will leave newline character after things read and std::getline will stop at that.
  • You should read (and drop) strings after numbers even when the numbers are different from what is wanted.
  • You should return "NOTFOUND" only after checking all data in the file instead of finding that the first number is not what is wanted.

Try this:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;


string locateProduct (int num)
{

  int line;
  string lineStr, productName;
  ifstream myfile;
  myfile.open("inventory.dat");


  if(!myfile)
  cout << "file failed to open";


  while(getline(myfile,lineStr) && getline(myfile,productName))
  {
    std::stringstream ss(lineStr);
    ss >> line;
    if(line == num)
    {
      cout << "Product code " << num << ": " << productName << endl;
      myfile.close();
      return productName;
    }
  }
  cout << "Code not found, try again." << endl;
  myfile.close();
  return "NOTFOUND";

}
MikeCAT
  • 61,086
  • 10
  • 41
  • 58
  • sorry I edited the post I'm new to stack overflow! however, I will try this solution and see if I can get it working! thanks! – Sangergtx Dec 06 '20 at 01:12