我的程序出了什么问题??基础C++
What is wrong with my program?? Basic C++
我的任务是:
---通过将计算分组为函数来重写程序。特别是,您应该至少引入以下功能:
---一个名为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"。
复制粘贴,不要重新键入。
编辑:修复错误消息
- G锁定铸造到基础上会释放模拟行为
- 在C++中如何在没有pow的情况下进行基础计算
- 我可以把基础班提升为儿童班吗
- 是否有内置方法可以强制转换为不同的基础类型,但保留常量限定符?
- 将作用域枚举转换为基础类型
- 为什么static_cast基础类型的枚举类int8_t获得意外值?
- 使用范围 v3 操作基础范围
- 如何在工厂方法中返回指向基于基础操作系统的派生类的有效指针
- 与C++哈希表的基础知识混淆
- 枚举类的 C 样式强制转换到基础类型 char 的引用
- 无法获得 std::from_chars 自动检测基础
- Arduino 基础 2 计算器无法C++工作
- 如何解决此问题(基础模板和继承)
- C ++基础私有方法在将自身转换为派生类后可以访问吗?
- C++ 相同基础类型的变体
- C/C++中全局调用堆栈的基础
- 外部基础类型未按请求对齐时的atomic_ref
- C++中同一基础对象的多个键
- 测试模板化基础
- 我正在尝试解决英特尔架构并行基础课程中关于 Coursera 的一项作业