I/O stream hw problem

Hey everyone, i currently having a homework assignment and i'm a little stumped. The assignment calls for us to take this line:


This is the input file for Programming Project 6 on p. 366.



and transfer it to an output file without any of the extra spaces in between the words. The code i have so far is:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;

void transfer(ifstream& fin, ofstream& fout);

int main()
{
ifstream fin;
ofstream fout;

cout << "Transfering input file 'indata.txt' to output file 'outdata.txt'..." << endl;

fin.open("indata.txt");
if(fin.fail())
{
cout << "Input file opening failed";
exit(1);
}

fout.open("outdata.txt");
if(fin.fail())
{
cout << "Output file opening failed";
exit(1);
}

transfer(fin, fout);

cout << "Transfer complete" << endl;

return 0;

}

void transfer(ifstream& fin, ofstream& fout)
{
char next;
fin.get(next);
while (! fin.eof())
{
if(next == ' ')
fout << ' ';
else
fout << next;
fin.get(next);
}
}


i can't figure out how to erase the spaces between the words. What i have now just transfers it without doing anything. Any help would be appreciated
You almost have it. Look at your transfer function when you are outputting to the file.
Well when i look at it seems right. Within my if statement i have my char variable set to equal two blank characters and when this happens it should only print one but it doesn't. Plus this process would be a little tedious considering the sentence i have to use has between 1 to 3 spaces between each of the words, which means i would have to use multiple if statements which i don't think my prof wants (unless that's the only way how)
Topic archived. No new replies allowed.