|
1鱼币
#include <iostream>
#include <fstream>
#include <windows.h> // 为了使用Sleep()函数
struct FishOil
{
std::string name;
std::string uid;
char sex;
};
bool InitFishC();
bool ReadFishC();
void RecordFishC();
bool WriteFishC(FishOil *OilData);
int main()
{
int i;
InitFishC(); // 初始化数据。
while( 1 )
{
std::cout << "请选择需要进行的操作: \n";
std::cout << "1. 打印数据到屏幕\n";
std::cout << "2. 录入数据\n";
std::cout << "3. 退出程序\n";
std::cin >> i;
switch( i )
{
case 1:
if( ReadFishC() )
std::cout << "成功读取文件^_^\n\n";
else
std::cout << "读取文件失败T_T\n\n";
break;
case 2:
RecordFishC();
break;
case 3:
return 0;
}
}
std::cout << "初始化失败T_T......\n\n";
return 0;
}
bool InitFishC()
{
FishOil OilInit = {"小甲鱼", "fishc00001", 'M'};
if( WriteFishC(&OilInit) == false )
std::cout << "初始化失败T_T\n";
}
bool ReadFishC()
{
std::string temp;
std::ifstream fileInput("FishC.txt");
if( fileInput.is_open() )
{
std::cout << "\n正在输出记录数据...... ";
for( int i=0; i <= 100; i++ ) // 打印百分比
{
std::cout.width(3);
std::cout << i << "%";
Sleep(50);
std::cout << "\b\b\b\b";
}
std::cout << "\n\n";
std::cout << " 姓名 " << " 身份证 " << " 性别 " << "\n\n";
while( std::getline( fileInput, temp ) )
{
std::cout << temp << " ";
std::cout << "\n";
}
std::cout << "\n\n";
return true;
}
else
return false;
}
void RecordFishC()
{
char goon, Save;
FishOil OilData;
FishOil *pOilData;
goon = 'Y';
while( 'Y' == goon )
{
std::cout << "请输入鱼C账号: ";
std::cin >> OilData.name;
std::cout << "请输入鱼C身份证:";
std::cin >> OilData.uid;
std::cout << "请输入性别:";
std::cin >> OilData.sex;
std::cout << "录入成功, 请问需要保存吗?(Y/N)";
std::cin >> Save;
if( 'Y' == Save )
{
pOilData = &OilData;
if( WriteFishC( pOilData ) )
std::cout << "成功写入文件^_^\n";
else
std::cout << "写入文件失败T_T\n";
}
else
{
return;
}
std::cout << "/n请问需要再次录入吗?(Y/N)";
std::cin >> goon;
}
}
bool WriteFishC( FishOil *pOilData )
{
std::ofstream fileOutput("FishC.txt", std::ios::app);
// std::ios::app用来说明在老数据追加新数据
if( fileOutput.is_open() )
{
fileOutput << pOilData->name << " ";
fileOutput << pOilData->uid << " ";
fileOutput << pOilData->sex << "\n";
fileOutput.close();
std::cout << "数据成功保存到FishC.txt\n\n";
}
else
std::cout << "保存失败T_T\n";
}
--------------------Configuration: fishoil - Win32 Debug--------------------
Compiling...
fishoil.cpp
G:\实验程序\c程序\C++\fishoil\fishoil.cpp(54) : error C2552: 'OilInit' : non-aggregates cannot be initialized with initializer list
G:\实验程序\c程序\C++\fishoil\fishoil.cpp(80) : error C2039: 'getline' : is not a member of 'std'
G:\实验程序\c程序\C++\fishoil\fishoil.cpp(80) : error C2065: 'getline' : undeclared identifier
G:\实验程序\c程序\C++\fishoil\fishoil.cpp(82) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accepta
ble conversion)
G:\实验程序\c程序\C++\fishoil\fishoil.cpp(103) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accept
able conversion)
G:\实验程序\c程序\C++\fishoil\fishoil.cpp(105) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accept
able conversion)
G:\实验程序\c程序\C++\fishoil\fishoil.cpp(135) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accept
able conversion)
G:\实验程序\c程序\C++\fishoil\fishoil.cpp(136) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no accept
able conversion)
Error executing cl.exe.
fishoil.obj - 8 error(s), 0 warning(s)
怎么解决??????????? |
最佳答案
查看完整内容
看懂程序还是比较简单的,
定义FishOil类(C里面的结构体),创建对象,然后有初始化、读、写、保存四个方法(函数)
感觉那个进度百分比有点鸡肋,虽然定义的是sleep50ms为计算机处理等待的最小时间(最好不要小于50。。)。。。。
我用VC6.0编译也出错了。。然后就用C-free编译的通过。。至于为什么VC出错了,我就不知道茑
|