0

I've tried the types string and char with no success.

First post sorry if I did anything incorrectly.

image

if (inputFile.is_open())
{
    bool data = getline(inputFile, line, '$');
    while (data)
    {
        name = line;
        getline(inputFile, course, '$');
        getline(inputFile, line, '$');
        clearGrading = line == "1" ? true : false;
        getline(inputFile, line, '$');
        goodFeedback = line == "1" ? true : false;
        getline(inputFile, line, '$');
        caring = line == "1" ? true : false;
        getline(inputFile, line, '$');
        reachable = line == "1" ? true : false;
        getline(inputFile, line, '$');
        toughGrader = line == "1" ? true : false;
        getline(inputFile, line, '$');
        lectureHeavy = line == "1" ? true : false;
        getline(inputFile, line, '$');
        attendance = line == "1" ? true : false;
        data = getline(inputFile, line, '$');
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Welcome to Stack Overflow! Please post your code as text, not images. For people that can't see images for one reason or another your question is unanswerable. – NathanOliver Dec 07 '20 at 19:20
  • 1
    Please post your code as text, not an image of your IDE. – Cory Kramer Dec 07 '20 at 19:20
  • image of program when run https://imgur.com/gallery/zCIATHj and File being read https://imgur.com/gallery/FAxrbtb – Toby Aderounmu Dec 07 '20 at 19:22
  • Don't write code like this: `clearGrading = line == "1" ? true : false;`. Just say: `clearGrading = (line == "1");` – selbie Dec 07 '20 at 19:28

2 Answers2

0

This code:

bool data = getline(inputFile, line, '$');
while (data)
{

The return value of getline() is not a bool, it's the original input stream (the first param to getline()) - likely such chaining can be done or so that it can be invoked in a loop

Easy fix is simply this:

while (getline(inputFile, line, '$'))
{

Two other issues. Don't have code that explicitly converts a boolean expression to true/false. Just let the boolean expression be itself.

Instead of this:

caring = line == "1" ? true : false;

This:

caring = (line == "1");

Finally, I would recommend you check the stream state after every call to getline(). You aren't doing that consistently. That is, instead of this:

name = line;
getline(inputFile, course, '$');
getline(inputFile, line, '$');

Do this:

name = line;
if (inputfile)
    getline(inputFile, course, '$');
if (inputfile)
    getline(inputFile, line, '$');
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
selbie
  • 100,020
  • 15
  • 103
  • 173
0

std::getline() returns a reference to the input istream it is given. The stream has a bool conversion operator defined to test the stream state in a boolean context. However, since C++11, that operator is marked as explicit, which means it can't be used to initialize a bool variable without an explicit type-cast, eg:

bool data = (bool) getline(inputFile, line, '$');

Or:

bool data = static_cast<bool>(getline(inputFile, line, '$'));

However, explicit operators are allowed to be used without type-cast in certain contexts, which includes while statements (see When can I use explicit operator bool without a cast?), eg:

while (getline(inputFile, line, '$'))
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770