c++文件读取问题
name.txt中的数据没有存到bop数组里面#include "stdafx.h"
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<array>
#include<ctime>
#include<fstream>
#include<cstdlib>
#include<cctype>
using namespace std;
const int strsize = 20;
struct bop
{
string fullname;
string title;
string bopname;
int preference;
};
int main()
{
ifstream inFile;
inFile.open("name.txt");
if (!inFile.is_open())
{
exit(EXIT_FAILURE);
}
bop n[5];
char ch;
for(int i = 0;i < 5;i++)
{
getline(inFile,n[i].fullname);
getline(inFile,n[i].title);
getline(inFile,n[i].bopname);
inFile>>n[i].preference;
}
cout << "Benevolent Order of Programmers Report" << endl;
cout << "a.display by name b.display by title" << endl;
cout << "c.display by bopname d.display by preference" << endl;
cout << "q.quit" << endl;
cout << "Enter your choice:";
cin >> ch;
while(1)
{
while(ch!='a'&&ch!='b'&&ch!='c'&&ch!='d'&&ch!='q')
{
cout << "reinput:";
cin >> ch;
}
switch (ch)
{
case 'a':
for (int i=0; i < 5; i++)
cout << n[i].fullname << endl; break;
case 'b':
for (int i=0; i < 5; i++)
cout << n[i].title << endl; break;
case 'c':
for (int i=0; i < 5; i++)
cout << n[i].bopname << endl; break;
case 'd':
for(int i = 0;i < 5;i++)
{
switch (n[i].preference)
{
case 0:cout << n[i].fullname << endl; break;
case 1:cout << n[i].title << endl; break;
case 2:cout << n[i].bopname << endl; break;
}}break;
case 'q':cout << "Bye!" << endl; break;
}
if (ch == 'q')break;
cout << "Next choice:";
cin >> ch;
}
return 0;
}
name.txt文件
wang yuncong
wyc
wang zong
0
wang licai
wlc
wang gong
1
zhang laiyun
zly
zhang gong
2
zhang hanzhe
zhz
zhi zhang
2
chun hei
ch
hei ye
0