Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,989 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,348 people online right now. Registration is fast and FREE... Join Now!




Filling array from a file and sort array by first and second column

 
Reply to this topicStart new topic

Filling array from a file and sort array by first and second column

coolindienc
3 Dec, 2006 - 05:36 PM
Post #1

New D.I.C Head
*

Joined: 2 Dec, 2006
Posts: 1


My Contributions
I tried to fill array from a file and an array by first column and then by second column. However, I am receiving several errors when I try to compile program. Please help. I am getting all my errors in my "fillArray" function. Please someone help me to find what is the problem. All my errors are basically expect me to make change in syntex and constructor and deconstructor. I also put line number in my code where I receive errors. However, I do not believe that I can be making these many mistakes for syntex. I must be missing something simple. My text file and my code are as below:

Thanks a million in advance!!!!

Text file
QUOTE

2 5 1050
5 17 1200
3 12 1225
2 23 1300
3 6 1140
3 3 1110
2 4 1420
2 1 1360
5 14 1225
5 11 1380
5 10 1470
2 2 1565
3 8 1445
5 7 1387
3 9 1128
2 16 1488


CODE

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;
//void sortem()
void fillarray()
    short employees[100][3];       //Line 13
    int row, int col, int i, int sizeoffile;     //Line 14
    ifstream inFile;
    
    inFile.open ("DataSheet.txt");   //Line 17
    sizeoffile = 0;                         //Line 18
    for (row=0; row<100; row++) //Line 19
    {
        for (col=0; col<3; col++)
        inFile >>(employees[row][col]);
        if(inFile.fail())break;
        sizeoffile++;
    }
    inFile.close();                       //Line 26
}                                            //Line 27

void sortem(short employees[100][3], int elems)
{
     ifstream inFile;
     inFile.open ("DataSheet.txt");
     int i, minIndex, minValue;
     for (i=0; i<(elems-1); i++)
     minIndex = i;
     minValue = (employees[i][]);
     for (int index=i+1;index<elems; index++)
     {
         if (employees[index][] < minValue)
         {
             minValue = employees[index][];
             minIndex = index;
         }
     }
     employees[minIndex][] = employees[index][];
     employees[i][]=minValue;
            
}


int main ()

{
    int values[size1][size2] = employees[100][3];
    
    fillarray (values, size1, size2);
    sortem(values, size1, size2);
    fillarray (values, size1, size2);

    system ("pause");
    return 0;
}

User is offlineProfile CardPM
+Quote Post

UMTopSpinC7
RE: Filling Array From A File And Sort Array By First And Second Column
4 Dec, 2006 - 11:14 AM
Post #2

New D.I.C Head
*

Joined: 20 Oct, 2006
Posts: 47


My Contributions
QUOTE(coolindienc @ 3 Dec, 2006 - 06:36 PM) *

I tried to fill array from a file and an array by first column and then by second column. However, I am receiving several errors when I try to compile program. Please help. I am getting all my errors in my "fillArray" function. Please someone help me to find what is the problem. All my errors are basically expect me to make change in syntex and constructor and deconstructor. I also put line number in my code where I receive errors. However, I do not believe that I can be making these many mistakes for syntex. I must be missing something simple. My text file and my code are as below:

Thanks a million in advance!!!!

Text file
QUOTE

2 5 1050
5 17 1200
3 12 1225
2 23 1300
3 6 1140
3 3 1110
2 4 1420
2 1 1360
5 14 1225
5 11 1380
5 10 1470
2 2 1565
3 8 1445
5 7 1387
3 9 1128
2 16 1488


CODE

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;
//void sortem()
void fillarray()
    short employees[100][3];       //Line 13
    int row, int col, int i, int sizeoffile;     //Line 14
    ifstream inFile;
    
    inFile.open ("DataSheet.txt");   //Line 17
    sizeoffile = 0;                         //Line 18
    for (row=0; row<100; row++) //Line 19
    {
        for (col=0; col<3; col++)
        inFile >>(employees[row][col]);
        if(inFile.fail())break;
        sizeoffile++;
    }
    inFile.close();                       //Line 26
}                                            //Line 27

void sortem(short employees[100][3], int elems)
{
     ifstream inFile;
     inFile.open ("DataSheet.txt");
     int i, minIndex, minValue;
     for (i=0; i<(elems-1); i++)
     minIndex = i;
     minValue = (employees[i][]);
     for (int index=i+1;index<elems; index++)
     {
         if (employees[index][] < minValue)
         {
             minValue = employees[index][];
             minIndex = index;
         }
     }
     employees[minIndex][] = employees[index][];
     employees[i][]=minValue;
            
}


int main ()

{
    int values[size1][size2] = employees[100][3];
    
    fillarray (values, size1, size2);
    sortem(values, size1, size2);
    fillarray (values, size1, size2);

    system ("pause");
    return 0;
}


First of all, you should post the error messages too. But you should fix this first:

CODE
void fillarray()
    short employees[100][3];       //Line 13
    int row, int col, int i, int sizeoffile;     //Line 14
    ifstream inFile;

this should be

CODE
void fillarray() {
    short employees[100][3];       //Line 13
    int row, col, i, sizeoffile;     //Line 14
    ifstream inFile;

You forgot a { and the extra int's on line 14 should be taken out. Make those changes and then post any other errors and line numbers you get.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 07:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month