tags:

views:

61

answers:

1

I'm writing a program in c++ and I need to ask the user for a file name, print the file and then reprint it with every 5th word missing. I've gotten as far as to asking them file name, checking for errors and printing out the file, I'm just completely lost on how to reprint it with every 5th word missing, any help?

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
using namespace std;

int main()

{   

const int SIZE = 51;

    char fileName[SIZE];

char ch;

ifstream inFile;

bool lastWasLetter = true;

const string UNDERLINE  = "__________";

cout << "Please enter a file name: ";

cin >> fileName;

inFile.open (fileName, ios::in);
while (!inFile)
{
    cout << fileName << " could not be opened, please enter new file name: ";
    cin >> fileName ;
    inFile.open (fileName, ios::in);
}

inFile.clear();               // reset read pointer to very beginning of file
    inFile.seekg(0L, ios::beg); 

    std::string word;
int count = 0;

while (inFile >> word)
{
 count++;
 if (count % 5 != 0)
      cout << word << endl;
}
}

And yes this a project I'm working on for my programming class.

A: 

Just keep track of the number of words you've read and skip printing every 5th word, i.e. whenever the word count is a multiple of 5. For example:

std::string word;
int count = 0;

while (inFile >> word) {
  count++;
  if (count % 5 != 0)
    cout << word << endl;
}
casablanca
it's telling me no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
JMcCabe
error C2678: binary '>>'
JMcCabe
Did you `#include <string>`?
casablanca
Yes i have string
JMcCabe
Okay, so there's no build errors anymore, but now it won't print out my file at all.
JMcCabe
Oops, I really meant `inFile >> word` where I had `cin >> word`. Corrected now.
casablanca
Hmmm, still doesn't seem to want to work.
JMcCabe
Maybe you should update the code in your question so we can see what's going wrong.
casablanca
Sorry! I updated it
JMcCabe
I tried the exact code that you posted and it works fine for me. Are you sure you compiled and ran the latest version of your program?
casablanca
It's printing out the message but each word is on a new line and it's not giving me the origional message.
JMcCabe
Remove the `endl` so that it doesn't print each word on a new line. You probably want to actually replace the `endl` with a space.
Evan Huddleston