Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I would appreciate if someone could help me finalise a code.

I need to read in people's info (such as ID, age, etc.) from a text file into different arrays. Records are like this

2012317109 Jamie Carles Smith Male 65

(different bits of info are separated by TAB, lines ending with newline)

However, regarding the ID numbers, I am told to use extraction operator (<<) to get the ID number as an integer and not as a string.

Then I must sort these records by alphabetical string order and then put these in an output file.

So far, I have the following, including a function that works to sort alphabetically. How should I proceed? I am NOT to use structures, maps or vectors, I need to use arrays.

#include <iostream>
#include <fstream>
using namespace std;

void  selection_sort( double x[],  int  length)
{   
    int  i,  j,  k;
    double  t;

    for  (i=0;  i<length-1;  ++i)  {        
        k = i;       //find next smallest elm, x[k]

        for  (j=i+1;  j< length;  ++j)
            if  (x[k] > x[j])  k = j;

        //swap x[i] with x[k] 
        t = x[i];   x[i] = x[k];  x[k] = t;   
    }
}


int main () {

    ifstream fin;
    ofstream fout;

    fin.open("input.txt");
    fout.open("output.txt");

    if (fin.fail()) {
        cout << "Fail to open inout.txt" << endl;
        exit(1);
    }

    struct row{string ID, name, rest;};

    int x; 

    fout << x << endl;

    row *rows=new row[x];

    for (int i=0; i<x; ++i) {
        getline (fin, rows[i].ID,   '\t'); // ID
        getline (fin, rows[i].name, '\t'); // name
        getline (fin, rows[i].rest      ); 
    }

    selection_sort (row[], x); 
//complier error this line: expected primary expression before [ token.

}

Basically, I would need help with:

  • extracting the data from input file into the arrays making sure that sorting will apply to every line not just individual arrays (so whole lines will be sorted)

for this one, I was suggested to use this but not sure how to insert it.

if (score[i] > score[j])
{
     swap(ID[i], ID[j]);
     swap(name[i], name[j]);
     swap(gender[i], gender[j]);
     swap(score[i], score[j]);
}
  • ensure the array declarations are legal

  • finding a way to make getline work for int arrays

Proposed solution, also, how do I insert it?

cin >> ID[y]; 
getline(cin, name[y], '\t');
... 
cin >> score[y];
  • Make the program work by using arrays and without using structures, vectors, maps.
  • Also, not sure if I can declare the length of an array as x int variable.

I've been trying to do this code for weeks now when I know it should not be that complicated. I would appreciate any help!

share|improve this question
1  
As in the FAQ this site is for reviewing working code, not helping create code. You might want to try StackOverflow. – Jeff Vanzella Apr 23 at 5:05

closed as off topic by Jeff Vanzella, Yuushi, mnhg, palacsint, Glenn Rogers Apr 23 at 6:40

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

Browse other questions tagged or ask your own question.