0

So I am writing a c++ program for a class and I got it to work out just fine but it is adding an extra . to the end of the output file. I don't know why it's doing this or what I have done wrong, any pointers?

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

void conv(char& s);

void conv(char& s)
{
    s = toupper(s);
}

int main()
{
    ifstream inFile;
    inFile.open("message.txt");

    if (!inFile.is_open())
    {
        cout << "failed to access file!" << endl;
        exit(EXIT_FAILURE);
    }

    ofstream outFile;
    outFile.open("upper.txt");

    if (!outFile.is_open())
    {
        cout << "failed to access file!" << endl;
        exit(EXIT_FAILURE);
    }

    char next;
    while (!inFile.eof())
    {
        inFile.get(next);
        if (!isupper(next))
        {
            conv(next);
            outFile << next;
        }
        else
        {
            outFile << next;
        }

    }
    inFile.close();
    outFile.close();
    system("pause");
    return 0;
}

the input is from a file that says, "Today Bill Smith ate 3 apples and 2 pears."

the output comes out looking like this , "TODAY BILL SMITH ATE 3 APPLES AND 2 PEARS.."

Matt
  • 17
  • 1
  • The other question and it's answers do not answer my question, also I was required by my teacher to write the program with .eof in the loop. So just telling me it's not the proper format to use is not helpful as I'm required to use it. – Matt Oct 08 '18 at 00:12
  • I agree, I don't see how this is a duplicate of that one, which is talking about reading a _multi-byte_ type (int) from a stream. I can understand why _that's_ not valid after finding `eof() == false`, but here we're talking about reading a single byte. A comment over there even says the situation with get(char) is different, without going into further detail... – Ian Oct 08 '18 at 00:29
  • I think the answer to this question is more helpful in this case: [Why does ifstream.eof() not return true after reading the last line?](https://softwareengineering.stackexchange.com/questions/318081/why-does-ifstream-eof-not-return-true-after-reading-th) – Ian Oct 08 '18 at 01:15

0 Answers0