无法从文件中读取数据从而输出到屏幕 不知道读取功能错在哪 写入功能是对的
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<fstream>
#include <ctype.h>
#include<conio.h>
#include <iomanip>
using namespace std;
int N;//课程总数目
struct Course
{
int Number;//课程编号
char Name[10];//课程名称
char Nature[10];//课程性质
float Time;//学时
float TeachingTime;//授课学时
float TextTime;//实验或上机学时
float CourseCredit;//学分
char Semester[10];//开课学期
}Subject[10];
void in();//课程信息录入
void del();//课程信息删除
void modify();//课程信息修改
void seek();//课程信息查询
void show();//课程信息显示
int Menu();//主菜单
void menuchoose();//菜单选择
void quit();//退出系统
int search(int); //根据账号找到用户数据对应的下标
void readData(); //开始前从文件中读数据,存在数组中
void writeData(); //程序结束前,将数组中的数据写入到文件中
/*菜单*/
int Menu()
{
int i;
while (1)
{
cout << endl;
cout << "\t|######################################|" << endl;
cout << "\t|----------课程信息管理系统------------|" << endl;
cout << "\t|———————————————————|" << endl;
cout << "\t|***** 课程信息录入 请按 1 *****|" << endl;
cout << "\t|***** 课程信息删除 请按 2 *****|" << endl;
cout << "\t|***** 课程信息修改 请按 3 *****|" << endl;
cout << "\t|***** 课程信息查询 请按 4 *****|" << endl;
cout << "\t|***** 课程信息显示 请按 5 *****|" << endl;
cout << "\t|***** 退出 请按 0 *****|" << endl;
cout << "\t|######################################|" << endl;
cout << "\t 请选择你的操作(0-5):";
cin >> i;
cout << endl;
if (i >= 0 && i <= 5)
break;
else
cout << "输入有误,请重新选择!" << endl;
}
return i;
}
/*菜单选择*/
void menuchoose()
{
int Choice;/*用户选择功能*/
do
{
Choice = Menu();
switch (Choice)
{
case 1:
in();//录入
break;
case 2:
del();//删除
break;
case 3:
modify();//修改
break;
case 4:
seek();//查询
break;
case 5:
show();//显示
break;
case 0:
cout << "正在退出." << endl;
}
} while (Choice);
}
/*课程信息录入*/
void in()//录入
{
system("cls");
Subject[N].Number = N + 1;
cout << "课程编号:" << Subject[N].Number << endl;
cout << "请输入课程的名称:";
cin >> Subject[N].Name;
cout << "请输入课程的性质:";
cin >> Subject[N].Nature;
cout << "请输入课程的开课学期:";
cin >> Subject[N].Semester;
cout << "请输入课程的学时:";
cin >> Subject[N].Time;
cout << "请输入课程的授课学时:";
cin >> Subject[N].TeachingTime;
cout << "请输入课程的上机学时:";
cin >> Subject[N].TextTime;
cout<<"请输入课程的学分:";
cin >> Subject[N].CourseCredit;
N++;//更新课程总数目
cout << "录入成功!" << endl;
system("pause");
}
/*课程信息删除*/
void del()
{
int id; //用于输入的编号
int i; //查找到该编号在数组中对应的下标
cout << "待删除课程编号:";
cin >> id;
i = search(id); //根据编号查询课程,返回课程的下标
if (i < 0) //说明课程不存在
{
cout << "该课程不存在,删除失败!" << endl;
}
else
{
int choice;
cout << "确认删除课程信息?(1.确认/0.取消)" << endl;
cin >> choice;
if (choice == 1)
{
for (i; i < N; i++)
{
Subject[i + 1].Number--;
Subject[i] = Subject[i + 1];
}
N--;
cout << "删除成功!" << endl;
}
else
{
cout << "你取消了操作,删除失败!" << endl;
}
}
system("pause");
return;
}
/*课程信息修改*/
void modify()//修改
{
int id; //用于输入的编号
int i; //查找到该编号在数组中对应的下标
cout << "请输入要修改的课程的课程编号:";
cin >> id;
i = search(id); //根据编号查询课程,返回课程的下标
if (i < 0) //说明课程不存在
{
cout << "该课程不存在,修改失败!" << endl;
}
else
{
Subject[i].Number = id;
cout << "请输入新的课程名称:";
cin >> Subject[i].Name;
cout << "请输入新的课程的性质:";
cin >> Subject[i].Nature;
cout << "请输入新的课程的开课学期:";
cin >> Subject[i].Semester;
cout << "请输入新的课程的学时:";
cin >> Subject[i].Time;
cout << "请输入新的课程的授课学时:";
cin >> Subject[i].TeachingTime;
cout << "请输入新的课程的上机学时:";
cin >> Subject[i].TextTime;
cout << "请输入新的课程所占学分:";
cin >> Subject[i].CourseCredit;
cout << "修改成功." << endl;
}
getch();
return;
}
/*课程信息查询*/
void seek()
{
int i, k = 0;
float a;
cout << "请输入想查询的课程学分:";
cin >> a;
cout << "-----------------------------------------------------------------------------" << endl;
cout << "课程编号 课程名称 课程性质 开课学期 学时 授课学时 上机学时 学分" << endl;
cout << "-----------------------------------------------------------------------------" << endl;
for (i = 0; i < N; i++)
{
if (Subject[i].CourseCredit == a)
{
k = 1;
cout << setw(4) << Subject[i].Number << setw(13) << Subject[i].Name << setw(11) << Subject[i].Nature << setw(12) << Subject[i].Semester << setw(8) << Subject[i].Time << setw(9) << Subject[i].TeachingTime << setw(11) << Subject[i].TextTime << setw(8) << Subject[i].CourseCredit;
cout << endl;
}
}
if (k == 0) {
cout << "课程不存在!" << endl;
cout << endl;
}
system("pause");
return;
}
/*课程信息显示*/
void show()
{
int i;
cout << "-----------------------------------------------------------------------------" << endl;
cout << "课程编号 课程名称 课程性质 开课学期 学时 授课学时 上机学时 学分" << endl;
cout << "-----------------------------------------------------------------------------" << endl;
for (i = 0; i < N; i++)
{
cout << setw(4) << Subject[i].Number << setw(13) << Subject[i].Name << setw(11) << Subject[i].Nature << setw(12) << Subject[i].Semester << setw(8) << Subject[i].Time << setw(9) << Subject[i].TeachingTime << setw(11) << Subject[i].TextTime << setw(8) << Subject[i].CourseCredit;
cout << endl;
}
getch();
}
/*根据编号查询课程,返回课程的下标*/
int search(int id)
{
int index = -1, i;
for (i = 0; i < N; i++)
{
if (Subject[i].Number == id)
{
index = i;
break;
}
}
return index; //若找到,其值在0~N-1间,否则,保持-1
}
void writeData() {
int i = 0;
ofstream fout("student.txt");
for (i = 0; i < N; i++) {
fout << setw(4) << Subject[i].Number << setw(13) << Subject[i].Name << setw(11) << Subject[i].Nature << setw(12) << Subject[i].Semester << setw(8) << Subject[i].Time << setw(9) << Subject[i].TeachingTime << setw(11) << Subject[i].TextTime << setw(8) << Subject[i].CourseCredit << endl;
}
fout.close();
}
void readData() {
int i = 0;
ifstream fin("student.txt", ios::in);
if (fin.is_open() == false) { cout << "打开文件失败\n"; }
else {
for (i = 0; i < N; i++) {
fin >> Subject[i].Number >> Subject[i].Name >> Subject[i].Nature >> Subject[i].Semester >> Subject[i].Time >> Subject[i].TeachingTime >> Subject[i].TextTime >> Subject[i].CourseCredit;
cout << setw(4) << Subject[i].Number << setw(13) << Subject[i].Name << setw(11) << Subject[i].Nature << setw(12) << Subject[i].Semester << setw(8) << Subject[i].Time << setw(9) << Subject[i].TeachingTime << setw(11) << Subject[i].TextTime << setw(8) << Subject[i].CourseCredit << endl;
}
}
fin.close();
}
/*主函数:*/
int main()
{
system("color 02");
readData();
menuchoose();
writeData();
return 0;
}
```