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??
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.
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
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.
}
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.
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.
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.
#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.