1

I am trying to read from a input file for a bookstore the books information. I have used getline for reading the file, however, some books have two authors and two different formats, and this is where i am stuck. I need my output to have both names listed as authors and both formats correct, my output is fine until I reach the authors point and also my formats arent correct. Below is the txt file and code I have so far. Some declared variables are for the rest of the program.

**C++ Code:**

string str;
string title;
string authors[4];
string publisher;
string ISBN;
string filename;
string Format;
double cost;
int yearpublished;
int copies;
int numOfcopies;
int numOfauthors;
int numFormats;
char userChoice;
int count = 0;
int num = 0;
int input = 0;

ifstream infile;
int i;

cout << "please enter the name of the file you wish to input:" << endl;
getline(cin, filename);

infile.open(filename.c_str());

if (!infile) {
   cout << "file not found!!!" << endl;
   exit(0);
}
while (!infile.eof()) {
getline(infile, title);
cout << "Title: " << title <<endl;
getline(infile, ISBN);
cout << "ISBN: " << ISBN << endl;
getline(infile, publisher);
cout << "publisher:" << publisher << endl;
string yearpublished;
getline(infile, yearpublished);
cout << "Year published: " << yearpublished << endl;
string cost;
getline(infile, cost);
cout << "Price:" << cost << endl;
string numOfcopies;
getline(infile, numOfcopies);
cout << "Copies in stock:" << numOfcopies << endl;
string numOfauthors;
getline(infile, numOfauthors);
cout << "Number of authors: " << numOfauthors << endl;
getline(infile, authors[4]);
cout << "Book authors:" << authors[4] << endl;
string numFormats;
getline(infile, numFormats);
cout << "Number of formats:" << numFormats << endl;
getline(infile, Format);
cout << "Book format:" << Format << endl;
string NumPages;
getline(infile, NumPages);
cout << "Number of Pages:" << NumPages << endl;
og69nova
  • 17
  • 3
  • 1
    And when you used your debugger to run your program, what did you see? This is precisely what a debugger is for. If you don't know how to use a debugger this is a good opportunity to learn how to use it to run your program one line at a time, monitor all variables and their values as they change, and analyse your program's logical execution flow. Knowing how to use a debugger is a required skill for every C++ developer, no exceptions. With your debugger's help you should be able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. – Sam Varshavchik Nov 13 '20 at 23:12
  • Welcome! Please start with the [tour] and read [ask]. Concerning your question, you should probably provide a [mcve] as well. – Ulrich Eckhardt Nov 13 '20 at 23:13
  • `while (!infile.eof()) {` is a common mistake that can cause you to have the last line of input 2 times: [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Nov 13 '20 at 23:15
  • `infile.open(filename.c_str());` since `c++11` the 2011 standard which I hope you are using by now `infile.open(filename);` is sufficent. – drescherjm Nov 13 '20 at 23:17
  • `getline(infile, authors[4]);` attempts to read the next line into the 5th element of an array of size 4 causing undefined behavior because you are 1 past the end of the array. This will not attempt to read an array. – drescherjm Nov 13 '20 at 23:22

1 Answers1

2

Look into std::vector. This allows you to have a variable sized array. After you have retrieved the amount of authors, you can use a for loop with getline to get the correct amount of authors. Or you put them all on one line with a separator like a comma to split them like demonstrated here:

Parse (split) a string in C++ using string delimiter (standard C++)

You can push the individual names into the vector.

JMRC
  • 1,336
  • 16
  • 34
  • Hi, I tried to follow this method, however now my ouput doesnt print past where my for loop begins. Here is my updated code in the section. string authors[4]; for (int i = 0; i < 4; ++i) getline(std::cin, authors[i]); cout << "Book authors:" << authors[4] << endl; string numFormats; getline(infile, numFormats); cout << "Number of formats:" << numFormats << endl; string Format[4]; for (int i = 0; i < 2; ++i) getline(std::cin, Format[i]); cout << "Book format:" << Format[2] << endl; – og69nova Nov 14 '20 at 03:29
  • Here is how i declared my vectors. vector authors[4]; vector Format[2]; – og69nova Nov 14 '20 at 03:34
  • @og69nova Just use vector authors. No square brackets as you don't need to know the amount of authors at compile time, otherwise you create an array of vectors, but you only need one as vector is itself an array. You can loop from `i = 0` to `i < numOfAuthors`. You can use `push_back` to insert into the `vector`. Have a look at the documentation. It's in general the most used container anyway. – JMRC Nov 20 '20 at 18:00