0

As the title says I'm trying to read a file into two vectors.

The file would look like this

1 hello

2 how

3 are

4 you

Right now I have this, but it doesn't seem to work

int number;
string word;
std::vector<int> first;
std::vector<string> second;

ifstream inFile(File);
if (inFile.is_open()) {

  while (inFile >> number >> word) {
    first.push_back(number);
    second.push_back(word);

  }
}
  • 1
    You can't push a `string` into a vector of `int`. Either make `first` a vector of string, or do a string-to-int conversion. – M.M May 07 '14 at 04:56

2 Answers2

3

Change the type of number to int.

Also the is_open() is superfluous (unless you have an else statement that handles the case when the file can't be opened) as the while loop will fail anyway if the file cannot be opened

user657267
  • 20,568
  • 5
  • 58
  • 77
2

you could use the atoi() function i believe in the header of the std library. it will convert an ascii string to and integer. so...

#include<string>
string number, word;
std::vector<int> first;
std::vector<string> second;

ifstream inFile(File);
if (inFile.is_open()) {

while (inFile >> number >> word) {
  first.push_back(atoi(number));
  second.push_back(word);

 }
}

You may need to check to make sure that atoi() did not fail before you push onto the vector but this may work for your situation.

Good Luck

EDIT: based on the comment below stating that atoi() may be a bad choice i will amend my answer. See this link. It's accepted answer recommends using std::stoi() so to amend my answer...

#include<string>
string number, word;
std::vector<int> first;
std::vector<string> second;

ifstream inFile(File);
if (inFile.is_open()) {

while (inFile >> number >> word) {
  first.push_back(std::stoi(number));//changed this line
  second.push_back(word);

 }
}
Community
  • 1
  • 1
Alex Zywicki
  • 2,263
  • 1
  • 19
  • 34
  • You should generally avoid `atoi` in C++ as it make error handling hard -- see also http://stackoverflow.com/questions/1640720/how-do-i-tell-if-the-c-function-atoi-failed-or-if-it-was-a-string-of-zeros/1640804#1640804 – Soren May 07 '14 at 05:02
  • @Soren I have amended my answer to take this into account. thank you for your input. – Alex Zywicki May 07 '14 at 05:10
  • .. for reference: some really good discussion on the virtues of `std::atoi` vs `std::stoi` can be found here; http://stackoverflow.com/questions/20583945/what-is-the-difference-between-stdatoi-and-stdstoi – Soren May 07 '14 at 05:18
  • I've made the changes, and now its gives me an error: invalid conversion from 'int' to 'const char*' – user3396692 May 07 '14 at 05:19
  • @user3396692 Did you change number to an `int`? that would probably cause the error. For my answer number will be a `string` not an `int`. my answer will not work in conjunction with the answer from @user657267 – Alex Zywicki May 07 '14 at 05:29
  • I see that, sorry my mistake on the type. yes the intention is for number to be int – user3396692 May 07 '14 at 05:39
  • @user3396692 Were you able to fix the issue? What i meant was that if you are going to go in the direction i proposed that the variable `number` would need to be a `string` and that it would be converted to and `int` by `std::stoi(number)` in the line `first.push_back(std::stoi(number));` – Alex Zywicki May 07 '14 at 05:42