Make sum of separat the points in different conditions

Can you please help me with this !

void code()
{

   ofstream outp;
   outp.open("result.txt");

   ifstream inp;
   inp.open("adding.txt");

   double xo=0, yo=0, x=0,y=0;

   while (inp >> x >> y) {
      if (x==xo) {
         yo = yo+y;
      } else {
        outp << xo << " " << yo << endl;
        xo = x;
        yo = y;
      }
   }
   outp << xo << " " << yo << endl;
   inp.close();
   outp.close();
}
1 Like

code2.C (731 Bytes)

If you check the results of your code, you will find mistake only in the first value
can you check please !

JIJI.C (363 Bytes)
file0.txt (149.9 KB)

I was not claiming this code was bug free. That was just an idea showing how to proceed. You are free to change it as you need. I will look away way.

try this:

{
   ofstream outp;
   outp.open("result.txt");

   double xo, yo, x, y;

   ifstream inp;
   inp.open("file0.txt");
   inp >> xo >> yo;
   yo = 0;
   inp.close();

   inp.open("file0.txt");
   while (inp >> x >> y) {
      if (x==xo) {
         yo = yo+y;
      } else {
        outp << xo << " " << yo << endl;
        xo = x;
        yo = y;
      }
   }
   outp << xo << " " << yo << endl;
   inp.close();
   outp.close();
}

1 Like

Thank you , β€œβ€ intelligent answer"):smiley:

to open file we uses β€”> inputfile.open();
to close file we use β€”> inputfile.close();
to delete file we use -->> ??

http://www.cplusplus.com/reference/cstdio/remove/

1 Like

remove(β€œfile.txt”);
:slightly_smiling_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.