Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been working on this code, everything's been fine until the compiler gave me an error C:\Users\dcst\Desktop\file\main.cpp|108|error: no match for 'operator<<' in 'collection << ctr'| and I can't continue working on the other things if I can't do this method right.



C++
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

struct students{
    string studentID;
    string surname;
    string firstname;
    string birthdate;
    string sex;
};

int main()
{
    fstream collection;
    string filename;
    short choice;



    do{
        int ctr=1;
        system("cls");
        if(collection.is_open()){
                cout<<"Active File: ["<<filename<<"]"<<endl;
        }else{
                cout<<"Active File|: [None opened]"<<endl;
        }

        cout<<"[1] Create new file"<<endl;
        cout<<"[2] Open existing file"<<endl;
        cout<<"[3] Manage data"<<endl;
        cout<<"[4] Exit"<<endl;
        cout<<"Enter operation index: ";
        cin>>choice;
        switch(choice){
        default:
            cout<<"Invalid input."<<endl;
            system("pause");
            choice=4;
            break;
        case 1:
            cout<<"Enter file name: ";
            cin>>filename;
            collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
            collection<<"------------------------------------------------------------------------------"<<endl;
            collection<<"Rec \t Student ID \t Surname \t Firstname \t Birthdate \t Sex \t"<<endl;
            collection<<"------------------------------------------------------------------------------"<<endl;
            collection.close();
            collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
            break;
        case 2:
            cout<<"Enter file name: ";
            cin>>filename;
            collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
            break;
        case 3:
            string lines;
            char menu;
            students student[10];

            do{
                if(collection.is_open()){
                    cout<<"Active File: ["<<filename<<"]";
                    system("cls");
                    while(getline(collection,lines)){
                    cout<<lines<<endl;
                    }
                }
                collection.close();
                collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);


                cout<<"[A]dd  [E]dit [D]elete  [S]ort  [F]ilter  Sa[V]e  e[X]it"<<endl;
                cout<<"Enter operation: ";
                cin>>menu;

                if(menu=='A'){
                    string lines2;
                    int one;
                    int two;
                    collection.open(filename,ios::app);
                    system("cls");

                    ifstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
                        if(collection.is_open()){
                            while(getline(collection,lines)){
                                cout<<lines<<endl;
                            }
                        }
                    collection.close();

                    cout<<endl<<"Adding data to "<<filename<<endl;
                    cout<<"Student ID: ";
                    cin>>student[ctr].studentID;
                    cout<<"Surname: ";
                    cin>>student[ctr].surname;
                    cout<<"Firstname: ";
                    cin>>student[ctr].firstname;
                    cout<<"Birthdate: ";
                    cin>>student[ctr].birthdate;
                    cout<<"Sex: ";
                    cin>>student[ctr].sex;

                    collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
                    collection<<ctr<<"\t"<<student[ctr].studentID<<"\t"<<student[ctr].surname<<"\t"<<student[ctr].firstname<<"\t"<<student[ctr].birthdate<<"\t"<<student[ctr].sex<<endl;
                    collection.close();
                    system("pause");
                    ctr++;
                    collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);

                }else if(menu=='E'){

                }else if(menu=='D'){

                }else if(menu=='F'){

                }else if(menu=='V'){
                    cout<<"Saving file..."<<endl;
                    collection.close();
                    cout<<"File saved."<<endl;
                    system("pause");
                }else{
                    system("pause");
                };
            }while(menu!='X');
            break;
        }
    }while(choice!=4);

    }


What I have tried:

I've tried to do this

collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
collection<<ctr<<"\t"<<student[ctr].studentID<<"\t"<<student[ctr].surname<<"\t"<<student[ctr].firstname<<"\t"<<student[ctr].birthdate<<"\t"<<student[ctr].sex<<endl;
collection.close();

but I keep getting that error
Posted
Updated 17-Aug-16 21:23pm

1 solution

You are trying to write to an input stream using the << operator which is not present (the base class istream does not have this operator):
ifstream collection(filename, std::fstream::in | std::fstream::out | std::fstream::app);
// ...
collection.close();
// ...
collection.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
// Output operator << not present with istream based classes
collection<<ctr; // ...

To perform input and output with one stream use the fstream class. But in your case it might be better to use a new output stream object for writing.
 
Share this answer
 
Comments
mariaclara 20-Aug-16 6:25am    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900