Question
I am reading a text file and trying to store the information into an array so I can use this in different parts of my
I am reading a text file and trying to store the information into an array so I can use this in different parts of my program. So a dynamic array. So far I have been able to retrieve the information from the text file and organize it by category (User name,User ID, User password)
I am having troubles allocating an array and storing the information correctly. Since I have different data types (int, string) do I need to create a different array for each data type? Or do I create a stuct data type array? Here is my code so far.
#include
using namespace std;
// Checks the to see how many users on the list
int list_check(){ int num_users; ifstream user_Data ("users-1.txt"); // Txt file name if(!user_Data.is_open()){ cout << "Unable to open file" << endl; return 1; } if(user_Data.is_open()){ string line; getline(user_Data, line, ' '); num_users = stoi(line); cout << "Number of users: " << line << endl; } user_Data.close(); return num_users; }
// Prints the text file to the screen
void array_populate(int a){ ifstream user_Data ("users-1.txt"); if(user_Data.is_open()){ Database identity; string line, dummy_hold; for(int i = 0; i < a; i++){ getline(user_Data, dummy_hold, ' '); getline(user_Data, line, ' '); identity.name = line; cout << "User name: " << line << endl;
// Store array.name[I] ??? Something like this??
getline(user_Data, line, ' '); identity.id = stoi(line); cout << "User ID: " << line << endl;
// Store array.id[I] ??? Something like this??
getline(user_Data, line, ' '); identity.password = (line); cout << "User password: " << line << endl;
// Store array.password[I] ??? Something like this?? } } user_Data.close(); }
int main() { int num_users; num_users = list_check(); array_populate(num_users);
return 0;
}
// Header file for struct and declarations for arrays?
#ifndef USER_INFO_H #define USER_INFO_H
#include
using namespace std;
struct Database{ int id; string name; string password; }; #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started