C++在制作一个翻译成Pig拉丁文和从Pig拉丁文翻译过来的程序时出错

C++ errors in making a program to translate to and from Pig Latin

本文关键字:Pig 翻译 拉丁文 出错 程序 一个 C++      更新时间:2023-10-16

我目前正在用C++完成一项学校作业。该程序的目的是将长度未知的字符串翻译成Pig拉丁文或从Pig拉丁文翻译过来。教授将从Unix命令行编译并运行它,因此他将通过shell将字符串提供给程序。当我开始用Java编程时,我不得不为这门课程学习C++。

在这里编译以下代码后:

#include <iostream>
#include <string>
#include <ctype>   // used for isalnum() and isupper()
#include <cstring>
using namespace std;
// function prototyping for custom functions
string toPigLatin(string);
string fromPigLatin(string);
string turnWordToPigLatin(string);
string fromPigLatinWordTranslation(string);
bool isAvowel(char);
bool isPunctuation(char);
int main(int argc, char *argv[]) // this is how command line args get passed in C++
{
enum { TOPIG, FROMPIG } mode;
if (argc == 1)
{ // note one argument: the name of the program is in argv[0]
    mode = TOPIG;
}
else if (argc != 2)
{
    // this is an error case. Print some kind of message
    cout << "Warning! Program started with too many command line arguments! To run this program,
                             you must include one of the two" << endl;
    cout << "argument TOPIG or FROMPIG so it will function correctly. If no arguments are
 supplied, this program will default to" << endl;
    cout << "operating in TOPIG mode."  << endl;
    return 1;
}
else
{
    string userArg (argv[1]); // turn the C string argument into a C++ string
    if (userArg =="topig")
    {
        mode = TOPIG;
    }
    else if (userArg =="frompig")
    {
        mode = FROMPIG;
    }
    else
    {
        // print some error!
        return 1;
    }
}
string toSend = "";       // check for incoming text to translate. If no text, give warning
                          // message
                          // else build a string to pass to translation functions
if ( cin.getline() = null)
{
     cout << "Warning! No text given to translate! Exitting program!" << endl;
}
else
{
    while (cin.getline() != null)
    {
          toSend.append( cin.getline() );
    }
}
// decide where to send the built string
if (mode = TOPIG)
    {
        string received = toPigLatin(toSend);
    }
else
    {
        string received = fromPigLatin(toSend);
    }
cout << received << endl;
return 0;
}
//function list; double check prototyping above.
string toPigLatin(string notPigLatin)
{
   string notPigLatin;
   string emptyString="";
   string isPigLatin="";
   string wordToConvert ="";
   char *charpointer = notPigLatin;            // points to beginning of the string
   char letterHeldInPointer = charpointer;     // holds the first letter in the string to convert
   while(&letterHeldInPointer != '')         // iterate through the string to examine letters
                                               // ending at the null terminator.
   {
       if ((&letterHeldInPointer = ' ') || (&letterHeldInPointer = '-'))     //
       {
          turnWordToPigLatin(*emptystring);
          strcpy(isPigLatin, emptyString);     // copies word over to isPigLatin string
          emptyString="";                      // clears out the empty string for further
          strcpy(isPigLatin, &letterHeldInPointer);
          *charpointer++;                      // move pointer forward one position
                                               // after copying spaces in
       }
       else
       {
           strcpy(emptyString, &letterHeldInPointer);
       }
       *charpointer++;                         // move pointer forward one position
   }
}
string turnWordToPigLatin(stringToCheck)
{
string stringToCheck;
int i = 0
string beganWithAVowel = "way";
string didNotStartWithVowel = "ay";
string tempString;
if ( stringToCheck[i]==isAvowel(stringToCheck, i) )
   { 
   strcat(stringToCheck, beganWithAVowel);
   }
else
    {
    while ( stringToCheck[i] != isAvowel(stringToCheck, i) )
          {
            strcat( tempString, stringToCheck[i]);
            i++;
          }
    strcat( tempString, didNotStartWithVowel);       // builds the end of the word, ie the
                                                     //    constanants plus
                                                     // the string literal "ay"
    stringToCheck.erase(0, i);                       // this removes the above from the original
                                                     // string to the first vowel
    strcat(stringToCheck, tempString)                // this is where the modifed tempstring gets
                                                     // moved to the end of the
                                                     // original string
    tempString.erase(0, tempString.length() );       // clears out tempstring to avoid hiccups if
                                                     // this is iterated through
                                                     // again in the future. Also, frees up
                                                     // memory.
    }
return stringToCheck;
}
bool isAvowel(&stringToCheck)
{     switch(stringToCheck[i])
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
      case 'A':
      case 'E':
      case 'I':
      case 'O':
      case 'U':
           return true;
           break;
      default:
           return false;
           break;
}
bool isPunctuation(&stringToCheck)
{     switch(stringToCheck)
      case '.':
      case '?':
      case '!':
           return true;
           break;
      default:
           return false;
           break;
}
string fromPigLatin(string isPigLatin)
{
   string isPigLatin;
   string wordConvertedFromPigLatin;
   string word;                   // this is to be used to translate words from Pig Latin to
                                  // normal English.
   string modifiedstring;         // this is the final translation of the input which will be
                                    // returned to main
   char *i = &isPigLatin;         // used to cycle through the isPigLatin string, initialized to
                                    // the beginning of it
   while (i != '')       // start at the beginning and works until the end of the entire string
   {
         if(&i == ' ')         // if the character in i is a space, add it to the modified string
         {
               strcpy(modifiedstring, isPigLatin[i]);
               i++;
         }
         else if(isPunctuation(&isPigLatin[i]))
         {
               strcpy(modifiedstring, isPigLatin[i]);
               i++;
         }
         while(&i != ' ') && (&i != isPunctuation(&isPigLatin[i]))
         {
                  strcpy(word, isPigLatin[i]);        // builds a word out of the string which
                                                      // gets translated below
                  i++;
         }
         strcpy(modifiedstring, fromPigLatinWordTranslation(word));
   }
   return modifiedString;
}
string fromPigLatinWordTranslation(word)
{
   string fenster;                  // It's a Usual Suspects joke/reference. The word being 
                                    // translated gets put here.
   string inEnglishPlease;          // Don't worry if you've never seen it. inEnglishPlease is
                                    // where the piglatin word
                                    // ends up after trasnlation
   int i;
   char tempholder;                // used to hold the last character in Fenster
   if ( fenster[(fenster.length()-3)] =='w')        // This if else statement is used to figure
                                                    // out of the word being
   {                                             // converted began with a vowel or not.
       i = 3;
   }
   else
   {
       i = 2;
   }
   for (i; i <0; i--)                               // and here is where the above if/else
                                                    // statement comes into play
   {
       fenster.pop_back();                              // The .pop_back() function peels back the
                                                    // last letter in a string
   }
   tempholder = &fenster[(fenster.length()-1)];
   if ( isupper(fenster[0]) )                       // If the word being translated originally
                                                    // began with an uppercase
   {                                                // letter, this segment makes it lowercase, 
                                                    // and capitializes the proper
        toupper(tempholder);                        // first character of the word.
        tolower(fenster[0]);
   }
   fenster.pop_back();
   strcpy(inEnglishPlease, tempholder);
   strcpy(inEnglishPlease, fenster);
   return inEnglishPlease;
}

自从我开始学习Java以来,我已经习惯了访问JavaDocs以供参考或清除任何错误。我还没有找到这样一个简单的C++参考库。所以,当我编译时,我得到了以下错误。有人能帮我解码和解开以下错误吗:

   *piglatin.cpp:9:55: error: ctype: No such file or directory piglatin.cpp: In function 'int
main(int, char**)':
piglatin.cpp:56: error: no matching function for call to 'std::basic_istream<char,
std::char_traits<char> >::getline()' /usr/lib/gcc/x86_64-redhat-
linux/4.4.7/../../../../include/c++/4.4.7/istream:593: note: candidates are:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*,
std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:405: note:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*,
std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
piglatin.cpp:56: error: 'null' was not declared in this scope
piglatin.cpp:62: error: no matching function for call to 'std::basic_istream<char,
std::char_traits<char> >::getline()'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:593: note: candidates
are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*,
std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:405: note:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*,
std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
piglatin.cpp:64: error: no matching function for call to 'std::basic_istream<char,
std::char_traits<char> >::getline()'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:593: note: candidates 
are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*,
std::streamsize, _CharT) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/istream:405: note:
std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*,
std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]
piglatin.cpp:78: error: 'received' was not declared in this scope
piglatin.cpp: In function 'std::string toPigLatin(std::string)':
piglatin.cpp:85: error: declaration of 'std::string notPigLatin' shadows a parameter
piglatin.cpp:89: error: cannot convert 'std::string' to 'char*' in initialization
piglatin.cpp:90: error: invalid conversion from 'char*' to 'char'
piglatin.cpp:94: error: lvalue required as left operand of assignment
piglatin.cpp:94: error: lvalue required as left operand of assignment
piglatin.cpp:96: error: 'emptystring' was not declared in this scope
piglatin.cpp:97: error: cannot convert 'std::string' to 'char*' for argument '1' to 'char*
strcpy(char*, const char*)'
piglatin.cpp:99: error: cannot convert 'std::string' to 'char*' for argument '1' to 'char*
strcpy(char*, const char*)'
piglatin.cpp:104: error: cannot convert 'std::string' to 'char*' for argument '1' to 'char*
strcpy(char*, const char*)'
piglatin.cpp: At global scope:
piglatin.cpp:110: error: 'std::string turnWordToPigLatin' redeclared as different kind of symbol
piglatin.cpp:17: error: previous declaration of 'std::string turnWordToPigLatin(std::string)'
piglatin.cpp:110: error: 'stringToCheck' was not declared in this scope*

您在std::strings上使用的是像strcat这样的C函数,但它们是在C字符串(也称为char*)上操作的。这就是大多数错误的原因。您绝对应该查阅一本C++基础知识的初学者书籍,比如char*string之间的区别,以及使用标准库设施的第一步。也可以试试http://cppreference.com/以获取标准库参考。