-4

Roger 210

Mike 150

Wayne 500

Conner 240

Ford 456

Fire 160

Robert 320

Andrew 480

The above is my text file (ignore the extra lines between the words) and I would like to read the .txt file into a cpp file into one int array and one string array. How would I do this?

The arrays are int[vote] and string[name].

Currently I have this:

string name[ASIZE]; int vote[ASIZE];

string s;
ifstream infile("input.txt");

for (int i=0; i<ASIZE; i++)
{
    infile >> vote[i];
    infile.ignore();
    getline(infile, name[i]);
    infile >> name[i] >> vote[i];
}

Please help!! I am a beginner and having trouble understanding arrays and we are required to use two different ones.

mexihacks
  • 1
  • 1
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Dec 04 '17 at 14:51
  • have a look at [ifstream](http://www.cplusplus.com/reference/fstream/ifstream/). If you have a more specific problem later on, you can always ask about that. – Zinki Dec 04 '17 at 14:52
  • Also, and more related to your problem, unless you have a requirement to use two arrays (school assignments can be weird sometimes) consider using *structures* and a single array (or possibly a `std::vector` if allowed). – Some programmer dude Dec 04 '17 at 14:52
  • Lake a look at this answer. You'll have to modify it a bit for your application.https://stackoverflow.com/questions/7868936/read-file-line-by-line – bruceceng Dec 04 '17 at 14:53
  • You read an `int,` call `ignore`, get a line, then get a string and an `int`? You seem to be doing a lot of things unrelated to reading a string and an int per line of your file. – crashmstr Dec 04 '17 at 14:54
  • You could always search the internet before posting. Here are some keywords: "StackOverflow C++ read file number text" – Thomas Matthews Dec 04 '17 at 15:36

1 Answers1

0

You will have to include:

#include<fstream>

Then, declare a ifstream variable as follows:

ifstream myFile;

From there you can explore all the methods contained in ifstream and build whatever you need. You will need two arrays, and read whenever you encounter a string, and a integer, and then store it in the corresponding array.

ferpujol
  • 11
  • 3
  • Would it be like this? ifstream myfile("input.txt"); for (int i=0; i> vote[i]; getline(cin, name[i]); myfile >> name[i] >> vote[i]; } – mexihacks Dec 04 '17 at 15:10
  • Make sure to get each piece of data instead of the whole line becuase in the same line you have your string part and your integer part. – ferpujol Dec 04 '17 at 17:59