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

时间:2014-07-15 05:42:36

标签: c++

所以我应该在“发件人”下打开一个包含四封电子邮件的文件,“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';
    }
}

1 个答案:

答案 0 :(得分:0)

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

此外,您不能在C ++中使用名为class的变量,因为它是关键字。