PLEASE help! Passing arrays through functions

Okay. i have been struggling all week with this program and i was finally able to get everything to work in the main as i needed it to (on my own)but separating it into functions is a whole other story. the idea is bringing employee names and hours in from a text file into arrays into one function, sorting using bubble sort in another, and displaying them in the last one. this is what i have so far and i know they are crazy wrong but i need some help PLEASE! and thank you.

//THIS IS AN EXAMPLE OF A TEXT FILE
5
Jones,Frank 2 3 8 3 6 3 5
Smith,Tiny 8 8 3 0 8 2 0
Turk,Tom 9 10 4 7 0 0 0
Jackson,Jim 5 6 5 6 5 6 5
Catan,Doug 7 3 8 7 2 5 7


//THIS IS WHAT I HAVE SO FAR WITH A LOT OF ERROR WORK.

#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<iomanip>
#include<sstream>
using namespace std;
const int DAY = 8;

const int TTL_HRS = 7;
const int DAY = 8;
void getData(string[], int[][DAY], int row);
void bubblesort(int arr[TTL_HRS], int size);
void displayempdat(int[][DAY], int row);

int main() {
get data();
bubblesort();
displayempdat();
}

void getData(string[], int[][DAY], int row)
{
ifstream fin;

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

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

int arr[50][8];
string name[50];

int total = 0;

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

total = 0;

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

}

fin.close();
}
}
void bubblesort(int arr[TTL_HRS], int size)
{
int arr[TTL_HRS] = 7;
int i, j;
int temp = 0;
int num;

srand(time(NULL));
for (i = 0; i < 7; i++)
{

for (j = 0; j < 7 - 1; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}


for (i = 0; i < 6; i++)
{
printf("%d\n", arr[i]);
}

}

void displayempdat(int[][DAY], int row);
{
cout << setw(3) << "Employee week hours " << endl;
cout << setw(3) << "Name:" << set(9) << "\n"
<< " S M T W T F S \n";

cout << setw(3) << arr[row][day] << endl;
cout << total;

}

//THIS IS WHAT THE STRUCTURE IS SUPPOSED TO LOOK LIKE WHEN THE DATA IS RETURNED.

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
Last edited on
Okay. i have been struggling all week with this program and i was finally able to get everything to work in the main as i needed it to (on my own)but separating it into functions is a whole other story.

I recommend you go back to this "working" program. To convert this "working" program to using functions start by declaring your functions and implementing blank functions that you can call from main(). Once you get this small part done, start moving lines of your code a few lines at a time.

The way you have approached converting your program to using functions has created so many problems that you will probably never figure out all of the problems.

By the way you really need to review your textbook and class notes on how to prototype and implement functions, you've seemed to have missed several key requirements, like naming your parameters in your implementation.

You also need to decide if you're writing a C or C++ program. For example don't mix C style IO with C++ streams.



Last edited on
I took your advice. I tend to get a little frazzled when I am coding and I get stuck which tends to undo my whole code and make my work useless. OKAY. so this is where i am at.

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


void getData(int arr2[][8], int length);


int main() {


getData({}, {});

system("pause");
}

void getData(int arr2[][8], int length) {

ifstream fin;

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

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

int arr[50][8];
string name[50];

int total = 0;

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

total = 0;

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


cout << setw(3) << arr[row][day];

}

cout << setw(3) << total << endl;
cout << endl;

}
fin.close();

}


that gives me an output of
Cat,Bill 9 3 7 5 8 0 0 32

Snake,Frank 2 3 8 3 6 3 5 30

Dog,Chet 8 8 3 0 8 2 0 29

Mouse,Mickey 9 10 4 7 0 0 0 30

Duck,Daffy 5 6 5 6 5 6 5 38

Mouse,Minnie 7 3 8 7 2 5 7 39

Yosimitee,Sam 2 5 3 0 4 9 4 27

Press any key to continue . . .

i have it set to give me my total in the 8th element ([7]). my next step is taking that and putting it into the sort function for my second function. my struggle here is I'm not sure how to do this. i know the idea of a bubble sort but im not sure how to make my total hours come out of the array to be used in the next function. i have researched this so if not wiling to just tell me please at least point me to where i can read it because it is not in the book we are using. thank you
Is using the arrays a mandatory part of this assignment? Or can you use std::vector instead?

Please explain what you think the following line is doing?

 
getData({}, {});


I would recommend opening the file stream in main(), checking that it opened properly, and then passing that open stream to your getData() function. Or another option would be to change your function to return a value to indicate success of failure, using exit() in a C++ program should be avoided whenever possible since it doesn't properly call C++ destructors.

Next look at this snippet:
1
2
3
4
5
6
7
8
9
10
ifstream fin;

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

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

1.) You really should get into the habit of using class constructors to open your files instead of calling open.
2.) You should really consider using meaningful variable names, what is the purpose of that variable named i?
3.) You try to read from the file before you insure the file actually opened, does that make sense?
4.) Again avoid the C function exit(). In this case you have other options, return an error code to the calling function or throw an exception for example.
5.) How does the calling function know how many records your file contained?
6.) Have you considered a vector/array of a structure or class to hold the record information?


Please use code tags when posting code!
Topic archived. No new replies allowed.