简体   繁体   中英

Converting istream operator >> to istream getline

I'm attempting to read lines from a file using istream& getline instead of using istream& operator .

result cpp

#include "Result.h"

Result::Result()
{
    Coursename[0] = '\0';
}

Result::Result( const char * nam, unsigned scor )
{
    strncpy( Coursename, nam, CourseNameSiz );

    score = scor;
}
//istream& getline( istream & input, Result & Re, ',' )
istream & operator >>( istream & input, Result & Re )
{
  //getline(Re.Coursename, input, ',');
  input >> Re.Coursename >> Re.score;
  return input;
}

ostream & operator <<( ostream & os, const Result & Re )
{
  os << "  Unit Name:  " << Re.Coursename << '\n'
     << "  Result: " << Re.score << '\n';
  return os;
}

int Result::GetScore() const
{
  return score;
}

inline void Result::SetScore( unsigned scor )
{
  score = scor;
}

result header

#ifndef RESULT_H
#define RESULT_H

#include <iostream>
#include <string.h>
#include <string>

using namespace std;

const unsigned CourseNameSiz = 10;

class Result
{
    public:

        Result();

        Result( const char * nam, unsigned scor );

        int GetScore() const;
        // Get the number of s.

        void SetScore( unsigned scor );
        // Set the number of score.
        //string Result:GetCourseName() const;

        friend ostream & operator <<( ostream & os, const Result & Re );
        //friend istream & getline( istream & input, Result & Re, ',' );
        friend istream & operator >>( istream & input, Result & Re );

    private:
        char Coursename[CourseNameSiz];
        //string Coursename[CourseNameSiz]; // Unit name, C style string
        int  score; // number of scores
        string str;

}; 
#endif // RESULT_H

This all works exactly how I want it to but I need to change it so instead of reading in a word at a time, I want to use istream& getline with a delimiter. Most of the comments in the code is my attempt on changing to the getline method. I made an array of strings and as a test tried to put the first item in read file into it but obviously didnt work.

Im just looking for a little guidance here. Thanks in advance

EDIT:: Input File: rinput.txt

31525 1 4
JPN_101 3 Japanese, 90,
ICT 4 example1, 91,
TIC 4 example2, 92,
CIT 4 example3, 93

So where it says example1, and 91, those are the two items Im attempting to use getline for with a delimiter because I want to use spaces in my read file

The other information is read in different classes which I will change to getline method when I get this one to work.

Output file: routput.txt

Student ID: 31525
Semester:   1
  Unit:  JPN_101
  Credits: 3
  Unit Name:  Japanese
  Result: 90

  Unit:  ICT
  Credits: 4
  Unit Name:  example1
  Result: 91

  Unit:  TIC
  Credits: 4
  Unit Name:  example2
  Result: 92

  Unit:  CIT
  Credits: 4
  Unit Name:  example3
  Result: 93

Number of courses = 4
Total credits     = 15

This is the output of when im using the operator method with the array chars.

include\Result.h|28|error: expected identifier before ','|

include\Result.h|28|error: expected ',' or '...' before ','|

||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

which points to the header file line 28 which is friend istream & getline( istream & input, Result & Re, ',' );

I Believe I just dont understand why just changing it to array of strings ( string Coursename[CourseNameSiz]; ) then just changing the operator method to a getline method doesnt just make it return the input like it does for the operator method.

I hope I explained that right, Im having someone translate this a little to help me word it better.

Thankyou

This line

friend istream & getline( istream & input, Result & Re, ',' );

won't compile because you have a constant as the third parameter instead of a parameter declaration.

In your case, because you hard code ',' in the call to getline inside the method, you don't need to pass the comma in at all.

friend istream & getline( istream & input, Result & Re);

Similarly, when you define the function, you don't need the ',' in the function definition:

istream& getline( istream & input, Result & Re)
{
  getline(Re.Coursename, input, ',');
  return input;
}

Note that you still need the comma in the internal call to getline .

This should fix up the compiler errors you have listed, you will still need to debug your code if it doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM