简而言之,我真的很纠结语法,有人可以帮助我解决这个问题

In short I really struggle with syntax, can somebody help me fix this?

本文关键字:帮助 解决 问题 真的 简而言之 语法      更新时间:2023-10-16

所以我需要在文件中读取,然后使用数组为每次字符出现创建字数统计和字符数统计。 每个单词都以空格、逗号、句点等结尾。 此外,我还需要放置一个 tolower 和一个方程,以使用 x-'a' 函数或类似的东西将字母设置为正确的数组。

来自 puTTy 的错误列表(我知道蹩脚的程序,但这是必需的)

project8.cpp: 在函数 âint main()â
: Project8.cpp:17:错误:未在此范围内
声明"文件1" Project8.cpp:18:错误:预期"在一段时间
之前" Project8.cpp:36:错误:输入结束时的预期

#include <iostream>
#include <string>
using namespace std;
    int in_word = false;
    int word_count = 0;
    char ch;
    char low_case;
    int char_count[26];
    int i;
int main()
{
    for (i=0; i<26; i++)
    char_count[i]=0;
cin.get(file1.txt)
while('n' !=(ch=cin.get(file1.txt)))
{
if (' ' == ch || 'n' == ch || 't' == ch)
    in_word = false;
else if (in_word == false)
    {
    in_word=true;
    word_count++;
    }
else low_case=tolower(ch);
    char_count[int(low_case)-int('a')]++;
}
cout << file1.txt;
cout << words << " words" << endl;
for (i=0; i<26; i++)
    if(count[i] !=0)
    cout << count[i] << " " << char(i+'a') << endl;
}

第一个问题是你没有声明file1 .目前还不清楚file1.txt的真正含义:它的编写方式,它似乎是一个类型的对象,其成员称为,txt类型为 char*char[N](具有常量N)。从外观上看,您实际上想打开一个名为 file1.txt 的文件。这看起来像这样:

std::ifstream in("file1.txt");

之后,您当然会使用 in 而不是 std::cin 从文件中读取。例如,您可以使用

for (char c; in.get(c); ) {
    // ...
}

读取文件的每个字符并对其进行适当处理。

让我们玩编译器吧!

不能将变量命名file1.txt,请将其命名为file1

另外,您忘记了行尾的分号;,所以

cin.get(file1.txt)

应该是

cin.get(file1);

我不太知道你在哪里定义这个变量,所以你可能缺少一个声明,

比如
const char* file1="file1.txt";

此外,您在此处的 for 循环之后开始尝试访问一些变量count

count[i]

你的意思是使用char_count吗?