#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct INFO
{
char type_;
string name_;
};
struct list
{
int code;
string name;
string description;
float price;
};
bool infile(string code,INFO &info);
void displaymenu();
void list();
int main()
{
cout<<"Welcome to the CP1200 POS System"<<endl;
cout<<"Created by Student Name:"<<"Ma Qiannan"<<endl;
cout<<"please enter your security code: "<<endl;
string c;
string x;
int i;
cin>>x;
while (true)
{
cin>>x;
if (x.length()!=4)
cout<<"Invalid length security code";
else
{
INFO info;
bool b = infile(x,info);
if (b)
{
displaymenu ();
int choice;
cin>>choice;
while (choice>0&&choice<=5){
if(choice==1) list();
}
}
else
cout<<"Not found"<<endl;
}
}
return 0;
}
bool infile(string code,INFO &info)
{
ifstream fin;
fin.open("d:\\security.txt");
if (fin.is_open()==false)
{
cout<<"error file"<<endl;
return false;
}
else{
string code_;
int i;
while (fin>>code_){
if (code == code_)
{
for (i=0;i<=4;i++)
{
if ()
fin>>info.type_;
fin>>info.name_;
return true;
}
}
char skiptype_;
string skipname_;
fin>>skiptype_>>skipname_;
return false;
}
}
}
void displaymenu()
{
cout<<"1, List items for sale"<<endl;
cout<<"2,*Add item"<<endl;
cout<<"3,Purchase items"<<endl;
cout<<"4,Logout"<<endl;
cout<<"5,Quit"<<endl;
cout<<"enter choice:";
}
void list()
{
cout<<"list all,or only within prince range?"<<endl;
char c;
cin>>c;
if (c=='a'){
ifstream fin1;
fin1.open("d:\\data.txt");
if (fin1.is_open()==false)
{
cout<<"error file"<<endl;
}
else{
while(fin1.eof()==false)
{
string ch;
int i;
cin>>ch;
for (i=0;i<=5;i++)
{
getline(fin1,ch);
cout<<ch<<endl;
}
}
}
}
}
就是读文件的时候有问题,
文档内容
1234 a Lindsay
1730 a Eugene
1030 c Phil
5844 c Bruce
可是只能找到1234
其余的密码均显示找不到
高手求教啊
#include <fstream>
#include <string>
using namespace std;
struct INFO
{
char type_;
string name_;
};
struct list
{
int code;
string name;
string description;
float price;
};
bool infile(string code,INFO &info);
void displaymenu();
void list();
int main()
{
cout<<"Welcome to the CP1200 POS System"<<endl;
cout<<"Created by Student Name:"<<"Ma Qiannan"<<endl;
cout<<"please enter your security code: "<<endl;
string c;
string x;
int i;
cin>>x;
while (true)
{
cin>>x;
if (x.length()!=4)
cout<<"Invalid length security code";
else
{
INFO info;
bool b = infile(x,info);
if (b)
{
displaymenu ();
int choice;
cin>>choice;
while (choice>0&&choice<=5){
if(choice==1) list();
}
}
else
cout<<"Not found"<<endl;
}
}
return 0;
}
bool infile(string code,INFO &info)
{
ifstream fin;
fin.open("d:\\security.txt");
if (fin.is_open()==false)
{
cout<<"error file"<<endl;
return false;
}
else{
string code_;
int i;
while (fin>>code_){
if (code == code_)
{
for (i=0;i<=4;i++)
{
if ()
fin>>info.type_;
fin>>info.name_;
return true;
}
}
char skiptype_;
string skipname_;
fin>>skiptype_>>skipname_;
return false;
}
}
}
void displaymenu()
{
cout<<"1, List items for sale"<<endl;
cout<<"2,*Add item"<<endl;
cout<<"3,Purchase items"<<endl;
cout<<"4,Logout"<<endl;
cout<<"5,Quit"<<endl;
cout<<"enter choice:";
}
void list()
{
cout<<"list all,or only within prince range?"<<endl;
char c;
cin>>c;
if (c=='a'){
ifstream fin1;
fin1.open("d:\\data.txt");
if (fin1.is_open()==false)
{
cout<<"error file"<<endl;
}
else{
while(fin1.eof()==false)
{
string ch;
int i;
cin>>ch;
for (i=0;i<=5;i++)
{
getline(fin1,ch);
cout<<ch<<endl;
}
}
}
}
}
就是读文件的时候有问题,
文档内容
1234 a Lindsay
1730 a Eugene
1030 c Phil
5844 c Bruce
可是只能找到1234
其余的密码均显示找不到
高手求教啊