Storing int and strings from a file into arrays

I am trying to take int values and strings, which are both in the same file, and store them in an array to be used later on. How do I store the int and strings into arrays from a txt file??

This is the txt file:
1998  Tennessee Volunteers
1999  FSU Seminoles
2000  Oklahoma Sooners
2001  Miami Hurricanes
2002  OSU Buckeyes
2003  LSU Tigers
2004  USC Trojans
2005  Texas Longhorns
2006  Florida Gators
2007  LSU Tigers
2008  Florida Gators
2009  Alabama Crimson Tide
2010  Auburn Tigers
2011  Alabama Crimson Tide
2012  Alabama Crimson Tide
2013  FSU Seminoles
2014  OSU Buckeyes
2015  Alabama Crimson Tide
2016  Clemson Tigers
2017  Alabama Crimson Tide
2018  Clemson Tigers
2019  LSU Tigers


Last edited on
Since each line is an integer year, followed by a multi-word string, you can use cin's >> operator for the year, and then use getline for the rest of the line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

struct TopRankInfo {
    int year;
    std::string name;
};

int main()
{
    using namespace std;

    vector<TopRankInfo> infos;
    {
        ifstream fin("input.txt");
        if (!fin)
            cout << "Error: Cannot open input.txt\n";
        
        int year;
        string name;

        while (fin >> year && getline(fin >> std::ws, name))
        {
            infos.push_back( {year, name} );
        }
    }
    
    for (const auto& info : infos)
    {
        cout << info.year << " " << info.name << '\n';
    }
}

I used vector instead of array, but if you need to use an array, you'd declare something like,
1
2
3
4
5
TopRankInfo info[1000];

// ...

info[i] = TopRankInfo{year, name};

and check to make sure i < 1000.
Last edited on
if you know struct or class, put an int/string in a little struct and read into a vector of those.
or if you must use arrays..

struct filedata
{
int year; string team;
};
filedata winnerz[1000];
textfile >> winnerz[i].year >> winnerz[i].team; //etc

if you cannot use struct yet (it is the better approach), then you can also make 2 arrays and have them tightly coupled by the index:
int year[1000];
int team[1000];

[i] for both arrays means the same row from the file, eg [3] is 2001 Miami Hurricanes
Last edited on
Others suggested a struct to hold the data, containing an int and a std::string.

Another solution is to use std::pair (or std::tuple) to hold the year and team name data. I'd use a std::vector for holding data, though using a regular array is not that difficult.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <utility>  // std::pair
#include <vector>

int main()
{
   // a regular array
   std::pair<int, std::string> arr_data[1000];

   // a vector
   std::vector<std::pair<int, std::string>> vec_data;

   // rest of code to read the data from the file, etc.
}


https://en.cppreference.com/w/cpp/utility/pair
https://en.cppreference.com/w/cpp/utility/tuple (requires C++11 or later)
greenway, we've now given you more than enough information to work off of. You shouldn't be spoonfed more than this. Show your attempt at a solution w/ arrays and then ask more questions here instead of making duplicate threads.

http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
I'm sorry but I am still lost. So if I had one array called "int ayear[22]" and another called "string aname[22]" how do i get the info from the txt file to be stored in the arrays, with one holding the years and the other holding college names?
Use an ifstream to open your input file,
e.g.
ifstream fin("input.txt");

Then, in a loop, call something like:
1
2
3
4
5
6
7
8
9
int year;
fin >> year; // get int from file
ayear[i] = year; // put value in year array

string name;
getline(fin, name); // get rest of the line of from the file a a string
aname[i] = name; // put line in name array

i++;


If you know you have 22 values, perhaps have the loop be:
1
2
3
for (int i = 0; i < 22; i++) {
    // ...
}

Otherwise, loop on the success of extracting data, like I do in my example. Hope that helps.
Last edited on
I'm sorry but I am still lost.
I think we can see what's coming now as this thread gets up to page 11 and beyond.
Hello greenway,

Instead of trying to comprehend everything at once. Start simple and work your way up.

First thing you need to do is open the file and check that it opened, (Part 1 below), then compile and test the program. When the program prints line 25 to the screen you will know it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

#include <fstream>

int main()
{
    // <--- Part 1.
    int year{};
    int sizeUsed{};  // <--- Holds the number for the amount of the array used. For later use. If needed.
    std::string name;

    const std::string inFileName{ "Input File.txt" };

	std::ifstream inFile(inFileName);

	if (!inFile)
	{
		//std::cerr << "\n File " << std::quoted(inFileName) << " did not open" << std::endl;
        std::cerr << "\n     File \"" << inFileName << "\" did not open" << std::endl;

        return 1;
	}

    std::cout << "\n    File is open!\n"; // <--- Used for testing. Comment or remove when finished.

    //<--- Part 2. Read 1 year and name from file.

    inFile.close();   // <--- Not required, as the file stream will close when the function looses scope.

    return 0;  // <--- Not required, but makes a good break point.
}

Once you have this working. Add to Part 2 to read the first 2 parts of line 1 and then print it to the screen.

When that works then you can change Part 2 to read the whole file and even store it into 2 parallel arrays if that is what you know.

If you have studies structs this would be good practice using a struct. If not do no worries.

When you get that far, Parts 1 and 2, post your code and let everyone know what you can use or need to use. Arrays, vectors, std:pair or std::tuple? It would also help to mention what IDE/compiler you are using.

Andy
Awesome, I figured out how to get the ints and string values stored in two arrays, thank you.
Last edited on
Topic archived. No new replies allowed.