tags:

views:

193

answers:

2

I'm trying to remove vowels from a text file and am having some trouble. I'm receiving a compiler error in line 6 saying

invalid conversion from const char to char

I'm pretty sure this has to do with the way I'm setting up the file stream in my code. I'm using fstream since it's used for reading and writing, but I didn't include any of the open modes because I thought I could write and read to/from the file without them (I'm pretty sure you have to use one, I'm just not sure which one.) Also, I'm not sure if the way I set up the equals operator is legal or not(will it read it as if s[i] is equal to a or e or i or o or u).

Code:

#include "std_lib_facilities.h"

void vowel_removal(string& s)
{
     for(int i = 0; i < s.length(); ++i)
             if(s[i] == ('a' || 'e' || 'i' || 'o' || 'u')) s[i] = " ";
}

int main()
{
    cout << "Enter file name.\n";
    string filename;
    cin >> filename;
    fstream f(filename.c_str());

    string word;
    while(f>>word){
                   vowel_removal(word);
                   f << word;
                   }

    keep_window_open();
}
+6  A: 

I think you mean invalid conversion from const char * to char

When you index a string you must assign a char not another string:

std::string s = "tie";
s[0] = 'l';
assert(s == "lie");
//s[0] = "l"; <--- not valid const char * to char

Also you must have both a right hand side and a left hand side for each comparison. You can't apply one left hand side to multiple right hand sides in C++.

if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
  s[i] = ' ';
Brian R. Bondy
Ah I see thank you. Would there be a shorter way to make the comparison?
trikker
The code compiles, but the text file doesn't change. I'm not sure why, but I'm guessing it has something to do with missing modes in the fstream argument.
trikker
You could do some other things like see if a char is in a string of different characters, but I think this is the most clear. If you are going to do more than 1 comparison like that with all the vowels just make it into a function call something like bool isVowel(char c)
Brian R. Bondy
You should create a separate file for your output as your input.
Brian R. Bondy
As in create a separate file stream, or create a separate text file. Because I want to edit the actual file, not create a new one with the changes.
trikker
Note: This question conversation is continued at http://stackoverflow.com/questions/1191349/why-doesnt-this-change-the-txt-file .
Brian
A: 

The reason the file doesn't change because you don't flush the stream

phsr