打开txt文件并创建值返回函数的代码出错

Error in code for opening txt file and creating a value returning function

本文关键字:函数 代码 出错 返回 txt 文件 创建 打开      更新时间:2023-10-16

所以我应该打开一个包含四封电子邮件的文件在"发件人"下,"类型"是"C"或"W",大小,它的分类是小,中,或大应该由一个值确定函数

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
string classification(int size);
int main ()
{
    ifstream infile;
    int size1, total_sender = 0, total_size = 0, total_small = 0;
    char type;
    string class, sender;
infile.open("eMail.txt");
    if(!infile) {
        cout << "Error opening file!";
        return 0;
    }
    cout << "TYPE" << "SENDER" << "SIZE" << "CLASSIFICATION" << endl;
    infile >> type >> sender >> size1;
    while (!infile.eof())
    {
        class = classification(size1);
        cout << type << sender << size1 << class << endl;
        total_sender += sender;
        total_size += size1;
        if (class == "SMALL")
            total_small++;
    }
    cout << "Total emails: " << total_sender << endl;
    cout << "Total KBs: " << total_size << endl;
    cout << "TOtal SMALL emails: " << total_small << endl;
    return 0;
}

我实际上应该返回'L', 'M'或'S',但选择这样做,这可能是一个主要问题吗?

string classification(int size)
{
    if (size < 5) {
        cout << "SMALL";
    else if (size >=5 && size <= 20)
        cout << "MEDIUM";
    else
        cout << "LARGE";
    }
}

代码不工作,我需要一些帮助!

提前谢谢你。

我将定义编辑为

char classification(int size)
{
    if (size < 5) {
        return 'S';
    else if (size >=5 && size <= 20)
        return 'M';
    else
        return 'L';
    }
}

使用cout将字符串打印到屏幕上与将其返回给调用者不同。您的classification函数需要使用return关键字返回一个值。

另外,在c++中不能有一个名为class的变量,因为那是一个关键字。