Search code examples
c++debuggingsyntaxredeclaration

c++ fix syntax debug with *lec=fopen();


I am working on some C++ code with a friend that know better than I do, and the code has a bug, I'd like help to fix this, since I couldn't figure out how...

EDIT The compiler stops at line 59 where: FILE *ecr("result.txt","wt"); is written.

There are many other things to fix, I fixed until 49 (59 now ;) ) I am blocked AGAIN...

Thanks!

EDIT (again, sorry) With the message:

C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp||In function 'int main()':|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|59|error: expression list treated as compound expression in initializer [-fpermissive]|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|59|error: cannot convert 'const char*' to 'FILE* {aka _iobuf*}' in initialization|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|82|error: expected initializer before '<' token|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|82|error: expected ';' before '<' token|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|82|error: expected primary-expression before '<' token|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|84|error: expected ')' before ']' token|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|84|error: expected ')' before ']' token|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|84|error: expected primary-expression before ']' token|
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|84|error: expected ';' before ']' token|
||=== Build finished: 9 errors, 0 warnings (0 minutes, 0 seconds) ===|

If somebody could figure how to fix this, that would be great!

Code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;


int main() {

int COL = 0;
int LIN = 0;

cout << endl << "columns?" << endl;
cin  >> COL;
cout << "lines?" << endl;
cin  >> LIN;

int CELL = LIN*COL;

cout << CELL << " cells" << endl;

//1- reading the file priority.csv
char Coul[COL][LIN];
for (int i=0; i<COL; i++) Coul[i][0]='\0';

FILE *lec=fopen("priority.csv","rt");

if (lec == NULL) {
        printf("Error");
        return(0);
}

char ligne[120];
int N;

for (int i=0; i<COL; i++)
{
  char nom[8];
  fgets(ligne,15,lec);
  sscanf(ligne,"%d,%s",&N,nom);
  if (N == i) strcpy(Coul[i], nom);

  else {printf("Error");
  break;
  }
}

fclose(lec);

//2- reading the file solution.txt and writing the result.
lec=fopen("solution.txt","rt"); // FIXED HERE

if (lec == NULL) {printf("Error");
    return(0);
    }

FILE *ecr=fopen("result.txt","wt"); //STOPS HERE
for (int i=1; i<CELL; i++)
{
  char c[4][8];  // 4 color names
  float v[4]; // 4 values
  fgets(ligne,119,lec);
  sscanf(ligne,"%d,%s,%f,%s,%f,%s,%f,%s,%f",
              &N, c[0], &v[0], c[1], &v[1], c[2], &v[2], c[3], &v[3]);
  if (N == i)
  {
    if (strlen(c[0]) == 0)  // la ligne est vide
    {
      fprintf(ecr,"%d ",i);
      for (int i=0; i<COL; i++) fprintf(ecr,"0 ");
      fprintf(ecr,"\n");
    }
    else
    {
      fprintf(ecr,"%d ",i);
      int r[4];
// we search the rang of c[ordre]
      for (int ordre=0; ordre<4; ordre++)
      {
        for (int ir=0, ir<COL; ir++)
        {
          if (strcmp(c(ordre], Coul[ir]) == 0) r[ordre]=ir;
        }
      }
      for (int ir=0; ir<r[0]; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"%d ",v[0]);
      for (int ir=r[0]+1; ir<r[1]; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"%d ",v[1]);
      for (int ir=r[1]+1; ir<r[2]; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"%d ",v[2]);
      for (int ir=r[2]+1; ir<r[3]; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"%d ",v[3]);
      for (int ir=r[3]+1; ir<57; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"\n");
    }
  }
  else {printf("Error"); break;}
}
fclose (ecr);
fclose(lec);
}

Solution

  • lec seems to be declared twice, so the second call should be

    lec=fopen("solution.txt","rt");
    

    since you already have FILE* lec above