Hi! i am writing a program in which i am reading a text file named "studentinfo". I have made a notepad file as "studentinfo" and then wrote a program to read this file. but there is an error occuring : " invalid operand of types 'char[20]'and '<unknown type>' to binary 'operator<<' ".
the above error is occuring in line no 19. but i couldn't understand this error.Please explain me.
The code of my program is following:

#include<iostream.h>
#include<fstream.h>
main()
{
   char name[30];
   char rollno[20];
   char program[20];
   ifstream myfile;                         // handle to file.
   char inputfilename[]="studentinfo";
   myfile.open(inputfilename);
   if(!myfile)
   {
      cout<<"file"<<inputfilename<<"cant open";
      exit(1);
   }         
   while(!myfile.eof())
   {
          myfile>>name>>rollno>>program ; 
          cout<<name<<"\t"<<rollno<<"\t"<program<<endl;  // this is line no 19.
   }
    myfile.close();                  
    system("pause");
}     

Recommended Answers

All 4 Replies

You are missing a < in between "\t" and program. Change line 19 to cout<<name<<"\t"<<rollno<<"\t"<<program<<endl; and you should be okay. Now all that's left to do is make your code standard compliant. What compiler are you using?

Your code is full of non-standard C++ code. And then, there is a typo with "\t"<program which should be "\t" << program. Correcting those basic problems, we get this:

// NOTE: standard C++ headers don't have the .h ending:
#include <iostream>
#include <fstream>

// NOTE: standard C++ components are in the 'std' namespace:
using namespace std;

// NOTE: the main function must have 'int' as return value:
int main()
{
   char name[30];
   char rollno[20];
   char program[20];
   ifstream myfile;                         // handle to file.
   // NOTE: the filename is a 'const' char array
   const char inputfilename[]="studentinfo";
   myfile.open(inputfilename);
   if(!myfile)
   {
      // NOTE: it's better style to leave spaces between operators:
      cout << "file " << inputfilename << " cant open" << endl;
      return 1; //<- NOTE: return 1 from main instead of 'exit(1)'
   }         
   while(!myfile.eof())
   {
      myfile >> name >> rollno >> program; 
      cout << name << "\t" << rollno << "\t" << program << endl;
   }
   myfile.close();
   // NOTE: don't use 'system("pause")'
   return 0; //<- NOTE: return 0 from main means 'everything went OK'
}

The above works fine, but the use of char arrays in this way is not recommended because operations like myfile >> name are not safe. For example, if the user enters a name that is longer than 29 characters, you will have a buffer overflow (writing passed the end of the char array). There are ways to deal with char arrays safely, but they are just more trouble for no reason. You should use the std::string class instead, like this:

// NOTE: standard C++ headers don't have the .h ending:
#include <iostream>
#include <fstream>
#include <string>

// NOTE: standard C++ components are in the 'std' namespace:
using namespace std;

// NOTE: the main function must have 'int' as return value:
int main()
{
   string name, rollno, program;
   ifstream myfile;
   const string inputfilename = "studentinfo";
   myfile.open(inputfilename.c_str());
   if(!myfile)
   {
      cout << "file " << inputfilename << " cant open" << endl;
      return 1;
   }
   while(!myfile.eof())
   {
      myfile >> name >> rollno >> program; 
      cout << name << "\t" << rollno << "\t" << program << endl;
   }
   myfile.close();
   // NOTE: don't use 'system("pause")'
   return 0; //<- NOTE: return 0 from main means 'everything went OK'
}

which is almost identical, but much safer.

commented: Great read as always .. +4

Thankyou Nathan Oliver. the fault that You caught, was actually the faulst. my problem is solved now.
Also thanks to mike for guideline. But one thing i want to tell you that the version of compiler I use must required '.h' at the end of headerfile. My compiler name isDevC++ and version is 4.9.9.2.

thanx to all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.