从const char到char - Vowel Removal的无效转换 [英] Invalid conversion from const char to char - Vowel Removal

查看:254
本文介绍了从const char到char - Vowel Removal的无效转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从文本文件中删除元音,我有一些麻烦。我收到第6行中的编译器错误,表示

 无效的从const char转换为char 

我确定这与我在代码中设置文件流的方式有关。我使用fstream,因为它用于读写,但我没有包括任何开放模式,因为我想我可以写和读取文件没有他们(我很确定你必须使用一个,我只是不知道哪一个。)另外,我不知道如果我设置equals运算符是合法或不是(它将读它,如果s [i]等于a或e或i或o或u)。



代码:

  #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.\\\
;
string filename;
cin>>文件名;
fstream f(filename.c_str());

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

keep_window_open();
}


解决方案

const char * char



<您必须指定一个字符串,而不是另一个字符串:

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

和左手侧用于每个比较。您不能在C ++中将一个左侧应用于多个右侧。

  if(s [i] ==' a'|| s [i] =='e'|| s [i] =='i'|| s [i] =='o'|| s [i] =='u')
s [i] ='';


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();
}

解决方案

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] = ' ';

这篇关于从const char到char - Vowel Removal的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆