打开txt文件并创建值返回函数的代码出错
Error in code for opening txt file and creating a value returning function
所以我应该打开一个包含四封电子邮件的文件在"发件人"下,"类型"是"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
的变量,因为那是一个关键字。
相关文章:
- 如何将 c++ get 函数代码转换为 opencv 算法中使用的 python
- 修改链表主函数代码,用户将在其中输入节点的索引和数据以及正确的消息
- 是否可以像这样编写c malloc函数代码
- 函数范围的静态变量如何导致与共享库中函数代码的未来使用不兼容
- 如何将C++ lambda 函数代码转换为 C#?
- 该构造函数代码如何将指针作为参数
- 调用C++具有复杂参数和复杂返回类型的函数 C 代码
- "_FCbuild"不能用作函数 - C/C++ 代码错误
- 复制赋值和复制构造函数(代码C++的差异)
- 反向链表的递归函数(代码段说明)
- 当满足条件时,是否可以在 GLSL 着色器中回调 C/C++ 函数/代码
- 在构造函数代码之前禁用默认类成员初始化
- 以下移动构造函数代码安全吗?
- C++构造函数代码..这叫什么
- 如何更改SWIG生成的CSHARP文件中的构造函数代码
- 找不到当前函数(代码::块)C++的边界
- 使用模板进行 C++ 函数代码泛化
- 编写此指针函数代码的长格式方法是什么
- C++某些构造函数代码之间的区别
- 错误C2668从站点对重载函数代码的调用不明确http://h264bitstream.sourceforge.net/