I've just learned about Dynamic Arrays and now I'm trying to read from a txt file into a dynamically allocated array. I was thinking I could include the number of records as metadata in the first line of the file in order to accomplish this, but I'm not sure how to do that exactly.
Example of txt file contents
Iron Man 487
Captain America 568
The Hulk 17
Black Widow 542
Spider Man 309
Scarlet Witch 184
Winter Soldier 237
Just Thor 491
The Falcon 319
I understand how to put numbers into a dynamic array from a file, but can't find anything on reading names AND numbers from a file into a dynamic array and using variables for both afterward for storing.
I've included some rough beginning code below just to show what I know so far without finishing it.
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
int main() {
vector<int> numbers;
ifstream in ("names.txt");
int number; //variable to hold numbers
}
Based on your file content you could create two std::vectors, one that contains std::string to hold the names with another that holds int.
Read each line from your file into a std::string.
http://www.cplusplus.com/reference/string/string/getline/
Construct a stringsream from your string input, extracting into a std::string and int.
http://www.cplusplus.com/reference/sstream/basic_istringstream/basic_istringstream/
http://www.cplusplus.com/forum/beginner/13117/#msg63116
When you have your separated name and number push the std::string back into the std::string vector and the int into the int vector.
http://www.cplusplus.com/reference/vector/vector/push_back/
One problem is you lose "linkage" between the name and number. They are no longer associated together as they were in the file.
There is a C++ "container", std::pair, that could keep the name and number together.
http://www.cplusplus.com/reference/utility/pair/
You'd construct a single vector that holds a pair, the pair holding a std::string and int: std::vector<std::pair<std::string, int>> test;
You want to print out the 3rd element (test[2]) in the vector? std::cout << test[2].first << ' ' << test[2].second << '\n';
Dynamic array of what? int? char? string? Are you trying to store the entire contents of the text file in a single array of char?
@ Repeater
I want to store/read the entire text file into a dynamic array
The output would ideally and eventually be listing all of the names with the numbers (lets say they're votes) after and then I'll have it make a percentage for each persons votes and output that with a "winner"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
usingnamespace std;
int main() {
vector<int> numbers; //dynamic array to hold votes
ifstream in("candidates.txt"); //input file stream
int number; //variable to hold votes while being read
while (in >> number) {
numbers.push_back(number);
}
in.close();
{
vector<string> names; //dynamic array to hold names
ifstream in("candidates.txt"); //input file stream
string name; //variable to hold names while being read
while (in >> name) {
names.push_back(name);
}
}
in.close();
}
So next my practice would be to "cout" the information with the percentage of "votes" each hero got either after or beside somehow?, not sure how that would work yet and then I want to display the "winner"
vector<int> numbers; //dynamic array to hold votes
A vector isn't an array. Do you actually want to use an array, or when you say "dynamic array" do you actually mean "some kind of sensible, appropriate storage class"?
@Repeater
In this context I was meaning "some kind of sensible, appropriate storage class".
I would love to read the information from the txt file into a dynamically allocated array, but I'm having too much trouble figuring out, so I went with vector.