我的程序出了什么问题??基础C++

What is wrong with my program?? Basic C++

本文关键字:基础 C++ 问题 什么 程序 我的      更新时间:2023-10-16

我的任务是:

---通过将计算分组为函数来重写程序。特别是,您应该至少引入以下功能:

---一个名为toLowerCase的函数,它将单个字符作为输入参数并返回一个字符。除非输入是大写字母,否则返回值应与输入相同,在这种情况下,返回值应为与该字母等效的小写字母。

---另一个名为toLowerCase的函数,这个函数将字符串作为输入参数并返回字符串。除了所有大写字母都已转换为小写字母外,返回的字符串应与输入的字符串相同。

----一个名为readText的函数,它将一个字符串作为输出参数(没有返回值(,并从cin读取多行输入,直到到达输入的末尾或遇到空行。(注意:readText不应该向cout写入任何内容。(

----一个名为countCharacter的函数,它接受两个参数作为输入并返回一个整数。第一个输入参数是字符串,第二个是字符。返回的值应该是该字符在字符串中出现的时间(如果该字符不在字符串中,则为零(。该功能应适用于所有法定字符(即,即使该程序仅使用它来计算小写字母,它也应适用于小写字母、大写字母、标点符号等(在介绍每个函数时,请根据需要通过调用新函数来替换main((中的代码。

我不断得到错误:字符前需要主表达式

#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
 void readText(string& text);
 void toLowercase(string& text);
 void countcharacter(string& text, char []);
 char ToLowerCase();

int main (int argc, char** argv)
{
  string userinput; //Initialize string name userinput
  cout << "Enter text to be analyzed, ending with an empty line or end-of-input:" <<     endl;
 readText(userinput); //Read text from user
cout << "You've entered: n" << userinput << 'n'; // Read input, line by line, until end of input or an empty line
toLowercase(userinput); //Output user input and if upper-case, convert to lower-case
 cout << "Lower case version of what you said: n" << userinput << 'n';
 countcharacter(userinput); //Count characters in userinput
 ToLowerCase();
  return 0;
}

void readText(string& text)
{
 string line;
 getline (cin, line);

while (cin && line != "")
{
  text = text + line + "n";
  getline (cin, line);
}
 /*for(std::string line; getline(std::cin, line) && !'n' ; )
    input += line + 'n'; */
}
void toLowercase(string& text)
{
  const int ucLcOffset = 'a' - 'A';
  for (int i = 0; i < text.size(); ++i)
{
  char c = text[i];
  if (c >= 'A' && c <= 'Z')
text[i] = text[i] + ucLcOffset;
}
}
void countcharacter(string& text, char c)
{
// Count and report on each alphabetic character
  int totalCount = 0;
  for (c = 'a'; c <= 'z'; ++c)
    {
  // Count how many times c occurs in the text
      int charCount = 0;
          for (int i = 0; i < text.size(); ++i)
       {
         if (text[i] == c)
         ++charCount;
       }
  // report on character c
      cout << c << ":" << charCount << " " << flush;
      if ((c - 'a') % 10 == 9)
    cout << "n";
      totalCount = totalCount + charCount;
    }
 // How many characters are left over?
     cout << "nother:" << text.size() - totalCount << endl;

}
char ToLowerCase()
{
    
 char c = 'A';
 char Conversion;
 const int ucLcOffset = 'a' - 'A';
     if (c >= 'A' && c <= 'Z')
    Conversion = c + ucLcOffset;
    cout << "Your conversion for character A is: " << Conversion << 'n';
} 

第28行错误。'countcharacter(用户输入(//计算用户输入的中的字符数

行:

#inlcude<iostream>

应为:

#include <iostream>

语法高亮显示是你的朋友。

此外。。。countcharacter()有两个参数:

void countcharacter(string& text, char []);

但你只提供一个:

countcharacter(userinput);
#inlcude<iostream>

你拼错了include

 countcharacter(userinput); //Count characters in userinput

countcharacter()采用两个参数,而不是一个。

您似乎想要计算每个小写字符('a'-'z'(的出现次数。在这种情况下,您不需要向countcharacter()传递第二个参数。更改:

void countcharacter(string& text, char []);

void countcharacter(string& text);

void countcharacter(string& text, char []);

void countcharacter(string& text);

您还必须在countcharacter()中声明char c

似乎还应该将char ToLowerCase()更改为void ToLowerCase(),因为您似乎没有返回任何内容。

我不是C++大师,而是

void countcharacter(string& text, char []);

似乎缺少参数名称。

发布您提供给编译器的确切代码。对于您发布的内容,您不会得到"error:在char之前需要的主表达式",您会得到"error:invalid preprocessing directive#inlcude"。

复制粘贴,不要重新键入。

编辑:修复错误消息