Question

I am suppose to write a program that will read from a text file and store what is inside the text file using structures and regroup and print out the information in the text file. But I have encountered problems with getline. I have try to write getline like this

getline(infile, info.name)

but it doesn't work. I have also include <string> and <cstring> but I still encounter errors like

error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'int' to 'char *'

and

error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 1 from 'char [10][80]' to 'char *'

The text file that I am suppose to print out is the below text

Isabella Chan
Republic of Singapore
Libra 23 - 10 - 1993
7
I wish to be good in c++
I wish that christina grimmie will win the voice
I wish that ......

Sorry for the noob question and thanks in advance!

   #include <iomanip>
   #include <iostream>
   #include <cstdlib>
   #include <ctime>
   #include <cctype>
   #include <fstream>
   #include <string>

   using namespace std;

   const int MAX = 80;
   const int MAXNO = 10;
   enum Zodiac 
   {
            Aquarius, Pisces, Aries, Taurus,
            Gemini, Cancer, Leo, Virgo,
            Libra, Scorpio, Sagittarius, Capricorn
   };
   struct Date
   {
       Zodiac sign;
       int day;
       int month;
       int year;
   };
   struct Student
   {
           char name [MAX];
       char nationality [MAX];
       Date birthDate;
       int no; // no of other messages
       char wishMessage [MAXNO][MAX];
       // Feel free to add in more features
   };

   void myInfo (fstream&, char [], Student&);
   // The above function reads information from the text file
   // and store the information in a structure reference parameter

   void printOut(Student);

   int main()
   {
       fstream infile;
       char fName[MAX];
       Student info;
       cout << "Enter your info file name: "
       cin  >> fName; 
       cout << endl;

       myInfo(infile, fName, info);
       printOut(info);

   }

   void myInfo (fstream& infile, char fName[], Student& info)
   {
          infile.open(fName, ios::in);

      if(!infile)
      {
           cout << "Error file not found!" << endl;
           exit(0);
      }
       infile.getline(info.name, MAX);
       infile.getline(info.nationality,MAX);
       infile  << info.birthDate.sign
           << info.birthDate.day
           << info.birthDate.month
           << info.birthDate.year;
       infile.getline(info.no, MAX);
       infile.getline(info.wishMessage, MAXNO, MAX);

       infile.close();
       cout << "Successfully readed!" << endl;

   }

   void printOut(Student info)
   {
       cout << "My name is " << info.name << endl;
       cout << "My nationality is " << info.nationality << endl;
       cout << "My date of birth is " << info.birthDate.day 
            << " " << info.birthDate.month << " " 
        << info.birthDate.year << endl;
       cout << "I am " << info.birthDate.sign << endl;
       cout << "\n I have " << info.no << " wishes:" << endl;

   }
Was it helpful?

Solution

It seems that you are trying to read into non-strings with getline, whereas it is reading into strings as per documentation.

Extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character).

Here are two offending lines:

infile.getline(info.no, MAX);

and

infile.getline(info.wishMessage, MAXNO, MAX);

The former is reading into int, the latter is into a string array.

You will need to first read the strings in, and then make the corresponding conversion operations as desired.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top