gpt4 book ai didi

c++ - 没有从我的字母计数程序中得到任何输出

转载 作者:行者123 更新时间:2023-11-30 02:50:18 26 4
gpt4 key购买 nike

我正在开发计算一段文本中的字母的程序,我似乎无法让它工作。有人可以指出我做错了什么。

#include <iostream>
#include <string>
using namespace std;

int main()
{
string inputxt;
cout << "enter txt:\n";
getline(cin, inputxt);

char charcheck[ ]={'a', 'b', 'c', 'd'};
int charsize=sizeof(charcheck)/sizeof(char);
int count[charsize];
cout << charsize;

for (int i=0; i==inputxt.length(); i++){
for(int a=0; a==charsize; a++){
if (inputxt[i]==charcheck[a])
++count[a];
}
}

for (int b=0; b==charsize; b++){
cout << "number of " << charcheck[charsize] << ": " << count[charsize];
cout << endl;
}
return 0;
}

请注意,我没有输入所有字符来检查文本。谢谢。

最佳答案

for (int i=0; i==inputxt.length(); i++){

for 结构有 3 个参数:

  • 初始化
  • 继续条件(而不是像您那样的终止条件)。将其作为 while ...
  • 阅读
  • 循环 Action (又名。事后)

也就是说,for (INIT; CONTINUATION; AFTERTHOUGHT) { BODY }直接翻译为:

INIT;
while (CONTINUATION) { BODY; AFTERTHOUGHT; }

将你的中间条件反转,应该是i!=inputtxt.length()。这同样适用于所有其他 for 循环。

关于c++ - 没有从我的字母计数程序中得到任何输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20578162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com