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

not getting any output from my letter count program

本文关键字:程序 输出 任何 我的      更新时间:2023-10-16

我正在开发一个计算文本中字母数的程序,我似乎无法使其工作。有人能指出我做错了什么吗。

#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 ...
  • 循环动作(又称事后思考)

换句话说,for (INIT; CONTINUATION; AFTERTHOUGHT) { BODY }直接翻译为:

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

反转你的中间条件,它应该是i!=inputxt.length()。这同样适用于每隔一个for循环。

for循环中,使用的是==而不是<。例如:

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

应该是:

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