C ++函数从字符串中误读了字符

时间:2018-12-03 19:18:50

标签: c++ string class methods

我正在研究一个项目,以习惯于在C ++中使用OOP。该程序应从“ input.txt”中获取一个任意字符串,并将其输出到翻译成莫尔斯电码的“ output.txt”中。它将起作用,除了某些字符被解释为“。”。而不是应该的。我认为显示代码会更有意义。

运行时输出:

Debug, untranslated: T
Debug, translated: -
Debug, untranslated: h
Debug, translated: ....
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: e
Debug, translated: .
Debug, untranslated:  
Debug, translated:  
Debug, untranslated: r
Debug, translated: .-.
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: e
Debug, translated: .
Debug, untranslated: d
Debug, translated: -..
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated:  
Debug, translated:  
Debug, untranslated: f
Debug, translated: ..-.
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: o
Debug, translated: ---
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: x
Debug, translated: -..-
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated:  
Debug, translated:  
Debug, untranslated: j
Debug, translated: .---
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: u
Debug, translated: ..-
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: m
Debug, translated: --
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: p
Debug, translated: .--.
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: s
Debug, translated: ...
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated:  
Debug, translated:  
Debug, untranslated: o
Debug, translated: ---
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: v
Debug, translated: ...-
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: e
Debug, translated: .
Debug, untranslated: r
Debug, translated: .-.
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated:  
Debug, translated:  
Debug, untranslated: t
Debug, translated: -
Debug, untranslated: h
Debug, translated: ....
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: e
Debug, translated: .
Debug, untranslated:  
Debug, translated:  
Debug, untranslated: b
Debug, translated: -...
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: r
Debug, translated: .-.
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: o
Debug, translated: ---
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: w
Debug, translated: .--
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: n
Debug, translated: -.
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated:  
Debug, translated:  
Debug, untranslated: d
Debug, translated: -..
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: o
Debug, translated: ---
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: g
Debug, translated: --.
Debug, untranslated: -
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: .
Debug, translated: ?
Debug, untranslated: 

Debug, translated: ?

input.txt:

The red fox jumps over the brown dog.

output.txt(运行后填写):

-.???. .??.-?? .???-??-??? .???.??-?.???.?? -??.???..?? -.???. -???.??-??.??-? -??-??-????

main.cpp:

#include <iostream>
#include <string>
#include <fstream>
#include <unistd.h>
#include <string>
#include <sstream>
#include "morseTranslator.cpp"

using namespace std;

ifstream input("/Users/michaelsimanski/Documents/Projects C++/morse-coded/input.txt"); //init input controller
ofstream output("/Users/michaelsimanski/Documents/Projects C++/morse-coded/output.txt"); //init output controller

int main(int argc, char const *argv[])
{
  if (!(input.is_open() && input.good()))
  {
    cout << "ifstream is FUBAR, exiting";
  }
  else
  {
    cout << "ifstream is open and good, proceeding" << "\n"; //it is right and truly good

    std::stringstream fileContents; //init message, that will be filled by input.txt
    fileContents << input.rdbuf();

    output << morseTranslator::translate(fileContents.str());

    return 0;
  }

  return 0;
}

morseTranslator.cpp:

#include <iostream>
#include "morseTranslator.h"

using namespace std;

string morseTranslator::convertChar(char x) //converts a char to morse equivalent
{
  switch(x)
  {
    case ' ': return " ";
    case 'A':
    case 'a': return ".-";
    case 'B':
    case 'b': return "-...";
    case 'C':
    case 'c': return "-.-.";
    case 'D':
    case 'd': return "-..";
    case 'E':
    case 'e': return ".";
    case 'F':
    case 'f': return "..-.";
    case 'G':
    case 'g': return "--.";
    case 'H':
    case 'h': return "....";
    case 'I':
    case 'i': return "..";
    case 'J':
    case 'j': return ".---";
    case 'K':
    case 'k': return "-.-";
    case 'L':
    case 'l': return ".-..";
    case 'M':
    case 'm': return "--";
    case 'N':
    case 'n': return "-.";
    case 'O':
    case 'o': return "---";
    case 'P':
    case 'p': return ".--.";
    case 'Q':
    case 'q': return "--.-";
    case 'R':
    case 'r': return ".-.";
    case 'S':
    case 's': return "...";
    case 'T':
    case 't': return "-";
    case 'U':
    case 'u': return "..-";
    case 'V':
    case 'v': return "...-";
    case 'W':
    case 'w': return ".--";
    case 'X':
    case 'x': return "-..-";
    case 'Y':
    case 'y': return "-.--";
    case 'Z':
    case 'z': return "--..";
    default: return "?";
  }
}

string morseTranslator::translate(string toBeTranslated)
{
  string translatedText = ""; //string form of what is being translated

  for (int i = 0; i < toBeTranslated.length(); i++)
  {
    cout << "Debug, untranslated: " << toBeTranslated.at(i) << endl;
    cout << "Debug, translated: " << morseTranslator::convertChar(toBeTranslated.at(i)) << endl;
    toBeTranslated.replace(i, 1, morseTranslator::convertChar(toBeTranslated.at(i)));
  }
  return toBeTranslated;
}

morseTranslator.h:

#include <iostream>

using namespace std;

class morseTranslator
{
public:
  static string convertChar(char x);
  static string translate(string toBeTranslated);
};

2 个答案:

答案 0 :(得分:2)

toBeTranslated.replace(i, 1, morseTranslator::convertChar(toBeTranslated.at(i)));

将在迭代时更改字符串,并且convertChar在插入的点和破折号上跳闸。

最好添加并返回translatedText。看起来您有时正沿着这条路线前进。

答案 1 :(得分:2)

您的问题来自

toBeTranslated.replace(i, 1, morseTranslator::convertChar(toBeTranslated.at(i)));

您将以此遍历整个字符串,因为每次下一次迭代都将解析刚替换为字符串的内容。您应该做的是在循环中使用translatedText,例如

translatedText += morseTranslator::convertChar(toBeTranslated.at(i));

,然后从函数返回translatedText。否则,您需要增加i morseTranslator::convertChar(toBeTranslated.at(i))到字符串中的字符数,以便尝试转换已转换的内容。