txt file into multi dim array table





#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<sstream>
using namespace std;


int const TTL_HRS = 7;

int main() {

ifstream fin;

fin.open("empdata2.txt");
int i;
fin >> i;

if (fin.fail()) {

cout << " Your file was unable to open.\n\n";
exit(1);
}

while (!fin.eof()) {

int total = 0;
for (int row = 0; row < i; ++row) {
fin >> name[row];
total = 0;

for (int day = 0; day < 7; day++) {
fin >> arr[row][day];
}
fin.close();

}

this is the closest i have gotten it to even display one name.




Last edited on
Can you post some data from empdata2.txt to show how the data is formatted in that file?
dear gunner bro, I am c++ beginner, its my semester end I have a project to submit would u plz help me out with?
If you reply to my question I shall try to help you
dear gunner bro, I am c++ beginner, its my semester end I have a project to submit would u plz help me out with?

Please ask for help in your own thread instead of hijacking one started by someone else.
I apologize, I am new to this forum. I thought it would notify me when I got a reply on my thread. yes. empdata2 is currently like this...

7
Cat,Bill 9 3 7 5 8 0 0
Snake,Frank 2 3 8 3 6 3 5
Dog,Chet 8 8 3 0 8 2 0
Mouse,Mickey 9 10 4 7 0 0 0
Duck,Daffy 5 6 5 6 5 6 5
Mouse,Minnie 7 3 8 7 2 5 7
Yosimitee,Sam 2 5 3 0 4 9 4


//Task Details:

Create a 50x8 int array to hold the employee’s data, and a size 50 string array to hold the employee names.
Read all the employee data into arrays
Total number of hours worked in the week and store it in the int array at either location 0 or 7, whichever make sense to you. Create a const variable to make it obvious in your code what you are doing. Example: hour[ i ] [ TTL_HRS ] = total;

Sort the employees by the total number of hours they worked for the week. The sort should be in descending order (most hours to least)
Write out the information to the console



the output is supposed to look like this.

Employee Weekly Hours:
Name: S M T W T F S TTL
Kirk,James 10 10 0 10 12 0 0 42
McCoy,Lenard 0 4 0 8 4 0 2 18
Scott,Annis 1 6 2 0 0 1 0 10

by bringing them into arrays but i can't figure out how to read in the files into 2 arrays with different data types on separate lines with one being a 2d array and the other being string. And assigning the hours to specific names. I've looked everywhere...and I'm stuck
obviously with the names of the file you read in ( i was just showing format... and the totals are going to be in descending order which i will be using bubble sort for in a separate function) i only am SUPER stuck in the array section. thank you for any help you can provide.
Create a 50x8 int array to hold the employee’s data, and a size 50 string array to hold the employee names.
Read all the employee data into arrays
Total number of hours worked in the week and store it in the int array at either location 0 or 7, whichever make sense to you. Create a const variable to make it obvious in your code what you are doing. Example: hour[ i ] [ TTL_HRS ] = total;

Have you learned struct and classes?
No, I'm only in programming 1 and we are only halfway through the quarter
@safin
Create a 50x8 int array to hold the employee’s data, and a size 50 string array to hold the employee names. Read all the employee data into arrays

Firstly, write a program that fulfills this requirement (Only this requirement). Then show us your effort and we will be correcting your mistakes if any.
okay so the problem is... i know how to create arrays. haven't even the SLIGHTEST clue how to read it in with this set up.


in this case i think it would be

string name;
string namearr[50];
int hours;
int hrarr[50][8]; // creates the arrays i will need (keep in mind i am VERY amateur with arrays.

and i used this code for reading in int only on a mini code exercise but i tried to alter things for 3 days to figure it out for this and all i could get it to do was to read back one name. and the code below doesn't even work at all for string so i just got stressed and here i am

while((cnt < ARRAY_SIZE) && (fin >> currentInput))
{ nums[cnt] = currentInput;
++cnt; }






Topic archived. No new replies allowed.