为什么文本文件会输出乱码,还一半是对的,一半却没有按程序输出呢,求大家帮忙看一下

weixin_45190038 2019-06-08 07:20:31
#include<iostream>

#include<iomanip>

#include<fstream>

#include<string>

using namespace std;

class staff //职工信息类定义
{
protected:
char name[10]; //职工姓名
int salary; //职工工资
public:
char *getname()
{return name;}
int getsalary()
{return salary;}
};
class jsjl:public staff //技术经理
{
protected:
int level; //业务等级
public:
int getlevel()
{return level;}
void input1() //录入技术经理信息
{
cout<<"技术经理姓名:";
cin>>name;
cout<<"业务等级:";
cin>>level;
salary=5000+1000*level;
cout<<salary<<'\n';
}
};
class xsjl:public staff //销售经理
{
protected:
int profit; //总经额
public:
int getprofit()
{return profit;}
void input2() //录入销售经理信息
{
cout<<"销售经理姓名:";
cin>>name;
cout<<"总经额:";
cin>>profit;
salary=5000+(profit*3)/1000;
cout<<salary<<'\n';
}
};
class jsry:public staff //技术人员
{
protected:
int time; //工作时间
public:
int gettime()
{return time;}
void input3() //录入技术人员信息
{
cout<<"技术人员姓名:";
cin>>name;
cout<<"工作时间:";
cin>>time;
salary=40*time;
cout<<salary<<'\n';
}
};
class xsry:public staff //销售人员
{
protected:
int sale; //销售总量
public:
int getsale()
{return sale;}
void input4() //录入销售人员信息
{
cout<<"销售人员姓名:";
cin>>name;
cout<<"销售总量:";
cin>>sale;
salary=(5*sale)/100;
cout<<salary<<'\n';
}
};
class wm:public staff //文秘
{
protected:
int sale; //奖金
public:
int getsale()
{return sale;}
void input5() //录入文秘信息
{
cout<<"文秘姓名:";
cin>>name;
cout<<"奖金:";
cin>>sale;
salary=4000+sale;
cout<<salary<<'\n';
}
};
jsjl a[2];
xsjl b[2];
jsry c[6];
xsry d[6];
wm e[4];
void use(); //使用介绍
void input(); //录入职工信息函数声明
void show(); //显示职工信息函数声明
void save(); //存档
void getsave(); //打开存档
void account(); //计算当月公司总工资
int main()
{
int select;
while(1)
{
system("cls");
cout<<"\t ******************欢迎使用******************\n";
cout<<"\t ****************工资管理系统****************\n";
cout<<"\t *__________________________________________*\n";
cout<<"\t *1使用介绍   *\n";
cout<<"\t *2录入职工信息 *\n";
cout<<"\t *3显示职工信息 *\n";
cout<<"\t *4存档    *\n";
cout<<"\t *5打开存档  *\n";
cout<<"\t *6退出 *\n";
cout<<"\t *__________________________________________*\n";
cout<<"\t 你要输入的编号是(1--6):";
cin>>select;
if(select==0) break;
switch(select){
case 1:
system("cls");
use(); //使用介绍
system("pause");
break;
case 2:
system("cls");
input(); //录入职员信息
system("pause");
break;
case 3:
system("cls");
show(); //调用show函数查询职工信息
system("pause");
break;
case 4:
system("cls");
save(); //调用save函数保存职工信息
system("pause");
break;
case 5:
system("cls");
getsave(); // 调用del函数删除职工信息
system("pause");
break;
default:
system("cls");
cout<<"没有此选项,请重新选择!"<<endl;
}

}
return 0;
}

void input() //录入职工信息的函数
{
for(int i=0;i<2;i++)a[i].input1();
for(i=0;i<2;i++)b[i].input2();
for(i=0;i<6;i++)c[i].input3();
for(i=0;i<6;i++)d[i].input4();
for(i=0;i<4;i++) e[i].input5();
account();
}
void show() //显示所有职工信息函数
{
cout<<left;//设置输出左对齐
cout<<setw(20)<<"技术经理姓名"<<setw(20)<<"业绩等级"<<setw(12)<<"当月工资"<<'\n';
for(int j=0;j<2;j++)
{
cout<<setw(20)<<a[j].getname()<<setw(20)<<a[j].getlevel()<<setw(12)<<a[j].getsalary()<<'\n';
}
cout<<'\n';
cout<<setw(20)<<"销售经理姓名"<<setw(20)<<"总经额"<<setw(12)<<"当月工资"<<'\n';
for(int n=0;n<2;n++)
{
cout<<setw(20)<<b[n].getname()<<setw(20)<<b[n].getprofit()<<setw(12)<<b[n].getsalary()<<'\n';
}
cout<<'\n';
cout<<setw(20)<<"技术人员姓名"<<setw(20)<<"工作时间"<<setw(12)<<"当月工资"<<'\n';
for(int k=0;k<6;k++)
{
cout<<setw(20)<<c[k].getname()<<setw(20)<<c[k].gettime()<<setw(12)<<c[k].getsalary()<<'\n';
}
cout<<'\n';
cout<<setw(20)<<"销售人员姓名"<<setw(20)<<"销售总量"<<setw(12)<<"当月工资"<<'\n';
for(int s=0;s<6;s++)
{
cout<<setw(20)<<d[s].getname()<<setw(20)<<d[s].getsale()<<setw(12)<<d[s].getsalary()<<'\n';
}
cout<<'\n';
cout<<setw(20)<<"文秘姓名:"<<setw(20)<<"奖金:"<<setw(12)<<"当月工资:"<<'\n';
for(int m=0;m<4;m++)
{
cout<<setw(20)<<e[m].getname()<<setw(20)<<e[m].getsale()<<setw(12)<<e[m].getsalary()<<'\n';
}
cout<<'\n';
account();
}
void use()
{
cout<<"该公司有技术经理2名、销售经理2名、技术人员6名、销售人员6名和文秘4名,共20名职工。录入职工信息时需要逐一全部录入。录入相关业绩即可显示其当月工资。只有当存档以后才能打开存档。只要存档过,以后每次打开此系统都可以打开存档。"<<'\n';
}

void save()
{
ofstream outfile("data.txt",ios::out);
if(!outfile)
{
cout<<"不能打开目的文件:"<<'\n';
exit(1);
}
cout<<"存档中......"<<'\n';

outfile<<"----------------------------------------------------------------------------------------"<<'\n';
outfile<<"***技术经理的资料统计***"<<'\n';
outfile<<setw(10)<<"技术经理姓名"<<setw(10)<<"业绩等级"<<setw(10)<<"当月工资"<<'\n';
for(int i=0;i<2;i++)
{
outfile<<a[i].getname()<<setw(10)<<a[i].getlevel()<<setw(10)<<a[i].getsalary()<<'\n';
}
outfile<<"---------------------------------------------------------------------------------------"<<'\n';
outfile<<"***销售经理的资料统计***"<<'\n';
outfile<<setw(10)<<"销售经理姓名"<<setw(10)<<"总经额"<<setw(10)<<"当月工资"<<'\n';
for(int j=0;j<2;j++)
{
outfile<<setw(10)<<b[j].getname()<<setw(10)<<b[j].getprofit()<<setw(10)<<b[j].getsalary()<<'\n';
}
outfile<<"---------------------------------------------------------------------------------------"<<'\n';
outfile<<"***技术人员的资料统计***"<<'\n';
outfile<<"技术人员姓名:"<<setw(10)<<"工作时间:"<<setw(10)<<"当月工资"<<setw(10)<<'\n';
for(int k=0;i<6;k++)
{
outfile<<setw(10)<<c[k].getname()<<setw(10)<<c[k].gettime()<<setw(10)<<c[k].getsalary()<<'\n';
}
outfile<<"---------------------------------------------------------------------------------------"<<'\n';
outfile<<"***销售人员的资料统计***"<<'\n';
outfile<<setw(10)<<"销售人员姓名"<<setw(10)<<"销售总量"<<setw(10)<<"当月工资"<<'\n';
for(j=0;j<6;j++)
{
outfile<<setw(10)<<d[j].getname()<<setw(10)<<d[j].getsale()<<setw(10)<<d[j].getsalary()<<'\n';
}
outfile<<"---------------------------------------------------------------------------------------"<<'\n';
outfile<<"***文秘的资料统计***"<<'\n';
outfile<<setw(10)<<"文秘姓名"<<setw(10)<<"奖金"<<setw(10)<<"当月工资"<<'\n';
for(j=0;j<4;j++)
{
outfile<<setw(10)<<e[j].getname()<<setw(10)<<e[j].getsale()<<setw(10)<<e[j].getsalary()<<'\n';
}
outfile.close();//关闭磁盘文件“data.txt”
cout<<"存档完毕!"<<'\n';
}//向磁盘文件“data.txt”输出数据


void getsave()
{
char *filename;
cout<<"自己完善删除函数"<<endl;
}

void account()
{
int allsalary=0;
for(int i=0;i<2;i++)
allsalary+=a[i].getsalary();
for(int j=0;j<2;j++)
allsalary+=b[j].getsalary();
for(int k=0;k<6;k++)
allsalary+=c[k].getsalary();
for(int m=0;m<6;m++)
allsalary+=d[m].getsalary();
for(int n=0;n<4;n++)
allsalary+=e[n].getsalary();
cout<<'\n'<<"\t公司当月总工资(元):"<<allsalary<<'\n';
}





















...全文
92 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
天边一只鼠 2019-06-10
  • 打赏
  • 举报
回复
字符集问题,在windows,控制台使用按GBK字符集输出,VS的创建控制台项目默认多字节字符应该也就是GBK,源文件的保存和编译也是按多字节字符集处理,一般写在源码里的中文都能正常显示出来。 问题出在字符数据保存到文本文件中,反正不知道是系统还是编译器,默认处理都是存为utf8字符集,再从文本文件读取出来已经是utf8的字符集。不经过处理控制台默认按GBK输出当然显示不正常。

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧