2

I have a little question. I'm currently doing a school assignment in c++ and the task is to have something similar to a small library, where the user can ask to look at a book and then the program will print out the release year, author, how many pages etc etc. The assignment is focused on vectors and arrays, but I thought a smart way of doing it could be to have the release years in a text file and then save those years in an array. When I first it, everything was saved in characters, (meaning "1","8","8","5"), when I'd actually like it to save every line in the text document as a string in the array, or something similar (like this: "1885",). I couldn't really figure out how to split them up into strings then. I then talked a bit to a friend and this is where I am with my code now, it's not really working and at the moment I have no idea how I am supposed to solve it. Main problem is I don't know how to read and save every line in the text document as a string, however I am grateful for any help that would make me be able to print out a single year from the text document, in any other way.

This is what my code looks like:

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

using namespace std;

void book();
void readFile(int input);
void oddEven();
void stringLiner();
void factorial();
int main()
{

int input;
while (input != 0)
{
cout << "Hello. Welcome to the first, truly big, assignment in this programming course." << endl;
cout << "Which part do you wish to access?" << endl;
cout << "1. Book program" << endl;
cout << "2. 2 arrays - One EVEN ~ One ODD" << endl;
cout << "3. The one at a time string" << endl;
cout << "4. Factorial array" << endl;
cout << "0. Exit " << endl;
cin >> input;
switch (input)
{
    case 1:
    book();
    break;

}


}
}

void book() //This is the function used to do the book thing
{
cout << string( 100, '\n' );

int input;

string year[5] = {"1883"/*Treasure Island*/ }; //Array for the years the  books were written
string author[5] = {"Robert Louis Stevenson"/*Treasure Island*/, "yollo"}; //Array for the authors
string pages[5] =  {"304"/*Treasure Island*/,"420" }; //Array for the number of pages
string books[5] = {"Treasure Island", "Swagolo" }; //Array for the name of the books
cout << "You have chosen to look at books." << endl;
cout << "These are the books in the library. Pick one to see what year it was written in, what author wrote it and how many pages it contains. " << endl;
cout << "These are the books in the library: " << endl;
for (int i = 0; i<5; i++)   //Loop to display all the books + what number to press to access them.
{
    cout << i+1 << " " << books[i] << endl;
};
cout << "Please type a number to look at that book. " << endl; 
cin >> input;
int TresIsl = input-1;
switch (input)  //Switch case to chose which book to look at.
{
    case 1: //Info about Treasure Island

    cout << "This is " << books[TresIsl] << " and this is some info. " << endl << endl;
    cout << books[TresIsl] << " was released in " ;
    readFile(input);
    cout << " and it was written by " << author[TresIsl] << ". ";
    cout << "This book contains " << pages[TresIsl] << " pages. " << endl;
    break;

    case 2:
    cout << "This is " << books[TresIsl] << " and this is some info. " << endl << endl;
    cout << books[TresIsl] << " was released in " ;
    readFile(input);
    cout << " and it was written by " << author[TresIsl] << ". ";
    cout << "This book contains " << pages[TresIsl] << " pages. " << endl;
    break;


}
}

void readFile(int input)
{
ifstream file("year.txt");
int numlines = 0;
int numMaxLines = 5;
vector<string> lines (numMaxLines);
while(numlines < numMaxLines && !file.eof())
{
    getline(file, lines);
    numlines++;
}
cout << lines[input];

}

The other void functions are for other tasks in this assignment which I didn't include now, I just copy pasted the code where they were included. Also please don't mind the slightly childish stuff in the code.

Also I am very sorry if this breaks any rules for the forum or something similar. I tried to find another topic like this for c++, but I couldn't find anything helpful.

KungKranium
  • 31
  • 1
  • 6
  • Not the source of your problems, but [your reading loop is faulty](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – molbdnilo Jan 13 '16 at 09:23
  • You're already doing better than about 75% of first time posters. You have shown us what you have got so far, and it is even a complete program. – Martin Bonner supports Monica Jan 13 '16 at 09:30
  • 1
    I suspect that you recently covered structured data (structs and classes) and that you're expected to apply that to this problem by using a structure called "Book". But that's just a hunch. – molbdnilo Jan 13 '16 at 09:42
  • @molbdnilo We're actually going through classes after this... – KungKranium Jan 13 '16 at 09:45
  • @KungKranium That's an interesting approach. In that case, don't be surprised if you get assignments very similar to this one. (I'm kind of in favour of the "deliberately cause problems in order to later demonstrate why X is a good thing" approach, but it can lead to less relevant answers on here.) – molbdnilo Jan 13 '16 at 09:50

2 Answers2

5

It's not clear what exactly your problem is, but assuming that you want to read a file line-by-line and get a vector of those lines, something like this would do it:

std::vector<std::string> readLines(const std::string& filename)
{
    std::vector<std::string> lines;
    std::ifstream input(filename);
    std::string line;
    while (std::getline(input, line))
    {
        lines.push_back(line);
    }
    return lines;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • 1
    Then instead of writing `string years[5] = {...};` you write `std::vector years = readLines("years.txt");`. (Get into the habit of not writing `using namespace std`.) Actually, I would write that as `const auto years = readLines("years.txt");` – Martin Bonner supports Monica Jan 13 '16 at 09:35
  • May I ask why I should use `using namespace std` ? My teacher told me to use it so I guess I've just never questioned it. – KungKranium Jan 13 '16 at 09:40
  • 1
    @KungKranium [This question/answer](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) answers that. – Floris Velleman Jan 13 '16 at 09:43
  • @KungKranium It's possible that your teacher mostly wanted to save you some tedious typing. In small programs like this, it doesn't really matter. – molbdnilo Jan 13 '16 at 09:46
0

if any one's still got a question, a friend and me discussed it and he helped me a bit, and we got a code that works in my case at least, so I thought I'd show it to you:

void readFile(int input)
{
ifstream file("year.txt");
string in;
vector<string> lines;
if (file.is_open())
{

    while ( getline (file, in) )
    {
        lines.push_back(in);
    }
    cout << in;

}
file.close();
cout<<lines[input-1]<<endl;
}

The cout in the end I guess is unnecessary in some cases, but this worked for me and my homework. Thanks for everyone's help anyways.

KungKranium
  • 31
  • 1
  • 6