Question

I am reading a text file and trying to store the information into an array so...

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 <iostream>
#include <fstream>
#include <string>
#include "user_info.h"

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, '\n');
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, '\n');
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, '\n');
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 <iostream>
#include <string>

using namespace std;

struct Database{
int id;
string name;
string password;
};
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include <string>

#include "user_info.h"

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()){
       user_Data>>num_users;
      
       cout << "Number of users: " << num_users << endl;
   }
  
   user_Data.close();
  
   return num_users;
}

// Prints the text file to the screen

void array_populate(Database *d,int a){
  
   ifstream user_Data ("users-1.txt");
  
   if(user_Data.is_open()){
      
       int number;
       user_Data>>number;
      
       for(int i = 0; i < a; i++){
          
           string user;
           user_Data>>user;
          
           cout << "User name: " << user << endl;
          
           int id;
           user_Data>>id;
          
           cout << "User ID: " << id << endl;
          
           string password;
           user_Data>>password;
          
           cout << "User password: " << password<< endl;
          
           d[i].name=user;
           d[i].id=id;
           d[i].password=password;
          
       }
   }
   user_Data.close();
}

int main() {
   int num_users;
   num_users = list_check();
  

// dynamic array of Database
   Database *database=new Database[num_users];
  
   array_populate(database,num_users);

   return 0;

}

//////////////////////////////////

// Header file for struct and declarations for arrays?

#ifndef USER_INFO_H
#define USER_INFO_H

#include <iostream>
#include <string>

using namespace std;

struct Database{
int id;
string name;
string password;
};
#endif

//////////////

Add a comment
Know the answer?
Add Answer to:
I am reading a text file and trying to store the information into an array so...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am having trouble trying to output my file Lab13.txt. It will say that everything is...

    I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • Hi! 1. I need some help with sorting string in a text file. My goal is...

    Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • My code doesn't output correctly using a .txt file. How do I clean this up so...

    My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly? ----------------------------------------------- CODE ---------------------------------------------- #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <fstream> using namespace std; struct Customer {    int accountNumber;    string customerFullName;    string customerEmail;    double accountBalance; }; void sortDesc(Customer* customerArray, int size); void print(Customer customerArray[], int size); void print(Customer customerArray[], int size) {    cout << fixed << setprecision(2);    for (int i = 0; i...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • I am trying to modify this program so that it stores the employee's name in a...

    I am trying to modify this program so that it stores the employee's name in a c-string and can handle up to 100 employees (so it would mostly be a partially filled array), but I cannot get it to work. It works with two files that it is suppose to read input from. Then it prints output to an output file. Employee Info File (first file read, M/F should be ignored): 5    Christine Kim 30.00   2       1    F 15    Ray...

  • my code deosn't run and it doesn't show any error. what's wrong about it !! it...

    my code deosn't run and it doesn't show any error. what's wrong about it !! it should loads data from a text file into an array of structs and prints the array to the screen here is the code: #include <iostream> #include <string> #include <fstream> using namespace std; struct Company {    string Departmentname;    int Departmentnum; }; const int MAX = 20; int main() { ifstream File;    Company arrayofdepartment[MAX];    int Departmentcount ;      File.open("comapny.txt");       if...

  • 1. Suppose the file mystery.txt contains the text below: The whole thing starts about twelve, fourteen...

    1. Suppose the file mystery.txt contains the text below: The whole thing starts about twelve, fourteen or seventeen. #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string word; int i 0; ifstream read("mystery.txt"); while(!read.eof()) { read>>word; if(i < word. length()) cout<<word[i]; i++; } cout<< endl; } seventeen teen

  • i have two issues that i need help. 1- that in case three and two only...

    i have two issues that i need help. 1- that in case three and two only one line is being read from the file 2- my case 4 is not working correctly as it should as the output is only " Enter 1 for Trump, 2 for Warren:" #include <iostream> #include <cctype> // For the letter checking functions #include <fstream> // For file input #include <iomanip> // For setw #include <ctime> #include <cstdlib> // For exit and abs #include <errno.h>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT