0

I am a beginner in c++ and i'm trying to figure how the in/output in a file works. This program is supposed to write an array of integers into a file,then read it from file, sort it,get the number of square integers and then write the results back into the file.However,when i try to compile it , it tells me that " "<<" operand is illegal for class". Could anyone please tell me what is the matter? I'm trying to avoid using .get and .put.

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

void sorting(int *p);
int squares(int *p);
int main() {
   int i, arr[10], *p = &arr[0], sq;
   char name[50];
   cout << "Give the file's name:";
   cin >> name;
   ofstream myfile(name, ios::out);
   cout << "Give the elements:";
   for (i = 0; i < 10; i++)
      myfile << *(p + i);
   myfile.close();

   ifstream myfile(name, ios::in);
   for (i = 0; i < 10; i++)
      myfile >> *(p + i);
   sq = squares(arr);
   sorting(arr);
   myfile.close();
   ofstream myfile(name, ios::app);
   for (i = 0; i < 10; i++) {
      myfile << "The sorted array is:";
      myfile << *(p + i);
   }
   myfile << "The number of square numbers is: " << sq;
   myfile.close();
   return 0;
}
void sorting(int *p) {
   int temp;
   for (int i = 0; i < 9; i++)
      for (int j = i + 1; j < 10; j++)
         if (*(p + i)>*(p + j)) {
            temp = *(p + i);
            *(p + i) = *(p + j);
            *(p + j) = temp;
         }
}
int squares(int *p) {
   int count = 0;
   for (int i = 0; i < 10; i++)
      if ((*(p + i) % 2) == 0)
         count++;

   return count;
}
2
  • Added the C++ tag for you. Do something about the indentation. Commented Jan 18, 2014 at 8:12
  • Well, first of all, you redefine myfile several times. Instead, every time you use ifstream myfile(name, ios::in); append a number. So ifstream myfile1(name, ios::in);, ifstream myfile2(name, ios::in);, and ifstream myfile3(name, ios::in);. Commented Jan 18, 2014 at 8:19

1 Answer 1

1

You are redefining myfile several times. Change main() so that each declaration is unique. This compiles on GCC

int main() {
   int i, arr[10], *p = &arr[0], sq;
   char name[50];
   cout << "Give the file's name:";
   cin >> name;
   ofstream myfile1(name, ios::out);
   cout << "Give the elements:";
   for (i = 0; i < 10; i++)
      myfile1 << *(p + i);
   myfile1.close();

   ifstream myfile2(name, ios::in);
   for (i = 0; i < 10; i++)
      myfile2 >> *(p + i);
   sq = squares(arr);
   sorting(arr);
   myfile2.close();
   ofstream myfile3(name, ios::app);
   for (i = 0; i < 10; i++) {
      myfile3 << "The sorted array is:";
      myfile3 << *(p + i);
   }
   myfile3 << "The number of square numbers is: " << sq;
   myfile3.close();
   return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

But now it doesn't let me give the numbers from console
I think you need more << cin then. You only ask the user for one variable, the file name.
The problem was solved.The only question left would be: if for example i use fstream stream_name(filename, ios::in | ios::out),would i be able to read from and write to the file using only that one stream?
@Georgiana std::basic_fstream<...> is a bidirectional file stream, so yes, you have both read and write functionality. But note that the explicit specification of the ::out and ::in open modes is unnecessary as that is how it is opened by default. You can simply open it using std::fstream f(filename) if no other open modes are needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.