我有一个程序,我必须使用哈希编写,我不熟悉并且不太懂。现在我无法计算我正在读的单词之间的空格,如果它是一个字母,它被一个空格分隔,一旦代码生成一个单词,它在被读入时被两个空格隔开。我的解码函数工作正常,但我不知道如何计算空间。这是我尝试过的一件事,但它没有用,任何帮助都会很棒。
string tmp;
MORSE TABLE[20];
int j = 0, f = 0, k = 0;
char x;
while(!infile.eof())
{
infile >> tmp;
infile.get(x);
if(x == ' ') //check if x is a space
{
infile.get(x); //read next character
if(x == ' ') //check and see if next char is a space
{
TABLE[k].insert(' '); //insert space into char array for printing
}
}
if(j >= f) //if true create new object
{
k++; f+=100;
}
TABLE[k].decode(tmp);//pass in string to decode
j++;
}//end while loop
任何帮助将不胜感激。我不是一个非常高级的C ++人,只是一个新手。
答案 0 :(得分:0)
默认情况下,Streams跳过空格,您可以使用flag控制空格。但是,我建议您使用基于行的方法,否则您还需要处理换行符:
string line;
while(getline(infile, line))
{ ...use line... }
顺便说一句:
while(!in.eof())
并不保证您可以从流中读取。尝试阅读并检查是否成功。以上就是这样。