-1

i got a problem where you need to process data from the provided file.

The data in the file is formatted like this:

video_title#video_creator#viewers

so you need to sort the data based on the viewer, and if the video has the same amount of viewer it will be sorted based on the title alphabetically.

i solved this problem with C so i tried to solve it with C++: the problem occurs when my C++ code got "Wrong_Answer(10)" it means my code is wrong with certain test cases from the system.

this is my C code:

int main (){

FILE *pT;

data dat[101];
pT = fopen("testdata.in","r");
int i=0;
while(!feof(pT)){
    
    fscanf(pT, "%[^#]#%[^#]#%d\n",dat[i].vid,
                                  dat[i].cret,
                                  &dat[i].viw);
        i++;
}
sort(dat,i);
for(int j=0;j<i;j++){
    printf("%s by %s - %d\n",dat[j].vid,
                             dat[j].cret,
                             dat[j].viw);
}

fclose(pT);

return 0; 
}


void sort(data dat[], int size){
data temp;
for(int i=0;i<size-1;i++){
    for(int j=0;j<size-i-1;j++){
        if(dat[j].viw<dat[j+1].viw){
            temp = dat[j];
            dat[j] = dat[j+1];
            dat[j+1] = temp;
        }
        if(dat[j].viw==dat[j+1].viw){
            if(strcmp(dat[j].vid,dat[j+1].vid)>0){
                temp = dat[j];
                dat[j] = dat[j+1];
                dat[j+1] = temp;
            }
        }
        
    }
}
}

and this is my C++ code:

int main() {

ifstream my_file;

my_file.open("../testdata.in");

string line;
int idx = 0;
struct data dat[101];


while (getline(my_file, line)) {
    
    size_t pos1 = line.find("#");
    if (pos1 != string::npos) {

        size_t pos2 = line.find("#", pos1 + 1);

        if (pos2 != string::npos) {

            dat[idx].vid = line.substr(0, pos1);
            dat[idx].crt = line.substr(pos1 + 1, pos2 - pos1 - 1);
            dat[idx].view = stoi(line.substr(pos2 + 1));

            idx++;

        }
    }
    
}
sort(dat, idx);

for (int i = 0; i < idx; i++)
    cout << dat[i].vid << " by " << dat[i].crt << " - "<< dat[i].view << endl;

my_file.close();
return 0;
}

void sort(struct data dat[], int size) {
struct data temp;
for (int i = 0; i < size - 1; i++) {
    for (int j = 0; j < size - i - 1; j++) {
        if (dat[j].view < dat[j + 1].view) {
            temp = dat[j];
            dat[j] = dat[j + 1];
            dat[j + 1] = temp;
        }
        if (dat[j].view == dat[j + 1].view) {
            if (dat[j].vid.compare(dat[j+1].vid) > 0) {
                temp = dat[j];
                dat[j] = dat[j + 1];
                dat[j + 1] = temp;
            }
        }

    }
}
}

im using the exact same sorting algorithm, so my assumption is there must be a problem with the way i collect and process the data with C++, but i have no idea whats wrong with it, so i need some inside, is my code really wrong or its just the problem from the online judge system..

btw im just starting using C++ for about a week, so im not really familiar with the language, any help will be appreciated thank you.

penguinn
  • 9
  • 2
  • *i solved this problem with C* Are you sure about that? Read [**Why is “while( !feof(file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Andrew Henle Aug 15 '23 at 10:10
  • Please add the declaration of `struct data` to both code samples... – Fe2O3 Aug 15 '23 at 10:19
  • The code samples assume different locations for the file, don't they? And in C++, you should always make sure you actually opened the file. In this case, I'm pretty sure you didn't open the file. – sweenish Aug 15 '23 at 11:37

0 Answers0