如何使用文件中的文本,例如变量
How can I use the text from a file like variables
我正在制作一个小的ATM银行系统。
我问如何从文本文件(数据库(中获取值并将其用于某些变量。
我已经可以将:name
、age
、money
、card number
和pin
存储到文本文件中,但是当我尝试验证.txt文件中的名称和引脚时,我遇到了麻烦。
这是代码。该程序有2个版本:英语和罗马尼亚语。我只做了"ro"部分("en"部分只是介绍(,它只是读/写到文件部分:
void ro_utilizator_nou()
{
system ("cls");
string nume;
int ziuac, lunac, anc, ziuan, lunan, ann, varsta, anbisect, bani=0;
time_t t = time(NULL);
tm* timePtr = localtime(&t);
ziuac=timePtr->tm_mday;
lunac=timePtr->tm_mon+1;
anc=timePtr->tm_year+1900;
nume_gresit:
cout<<"Introduceti numele: "<<endl;
cin>>nume;
cin.sync();
if (!string_litere(nume)){system("cls");goto nume_gresit;}
data_gresita:
cout<<"Va rugam sa va introduceti data nasterii."<<endl;
cout<<"Introduceti ziua nasterii:"<<endl;
cin>>ziuan; cin.clear(); cin.ignore();
if (ziuan<1 || ziuan>31) {system("cls"); goto data_gresita;}
cout<<"Introduceti luna nasterii:"<<endl;
cin>>lunan; cin.clear(); cin.ignore();
if (lunan<1 || lunan>12) {system("cls"); goto data_gresita;}
cout<<"Introduceti anul nasterii:"<<endl;
cin>>ann; cin.clear(); cin.ignore();
if (ann>2018 || ann<1900) {system("cls"); goto data_gresita;}
if ((ann % 4 == 0 && ann % 100 != 0 ) || ann % 400 == 0) anbisect = 1; else anbisect = 0; //stabilirea daca anul nasterii este bisect sau nu
if (ziuan<1 || (lunan==2 && anbisect==1 && ziuan>29) || (lunan==2 && anbisect==0 && ziuan>28) || ((lunan==1 || lunan==3 || lunan==5 || lunan==7 || lunan==8 || lunan==10 || lunan==12) && ziuan>31) || ((lunan==4 || lunan==6 || lunan==9 || lunan==11) && ziuan>30) )
{system("cls"); cout<<"Data nasterii incorecta!"<<endl<<endl;; goto data_gresita;}
varsta=anc-ann; //stabilirea varstei
if (lunan>lunac) varsta--;
else if (lunan==lunac) if (ziuan>ziuac) varsta--;
cout<<endl<<"Aveti "<<varsta<<" ani."<<endl;
if (varsta<14) {
cout<<"Ne pare rau, nu puteti avea un card de credit daca nu aveti minim 14 ani impliniti."<<endl<<endl;
system("pause");
main();
}
int card_number = generare_card_number();
int card_pin = generare_pin();
cout<<"Numarul cardului (si al contului) dumneavoastra este: "<<card_number<<endl;
cout<<endl<<"ATENTIE! Urmeaza sa va afisam codul secret PIN!!!"<<endl<<"(Apasati orice tasta)"<<endl;
cin.ignore(); cout<<"Pinul dumneavoastra este: "<<card_pin<<endl;cin.ignore();
ofstream new_data_base("database.txt", ios::app);
new_data_base << nume << ' ' << varsta << ' ' << bani << ' ' << card_number << ' ' << card_pin <<endl;
new_data_base.close();
}
void ro_utilizator_existent()
{
ifstream data_base("database.txt");
string nume, nume2;
int pin, optiune, inregistrat, varsta, bani, card_number, card_pin;
if (!data_base.eof()){
data_base >> nume >> varsta >> bani >> card_number >> card_pin;
}
nume_gresit:
system ("cls");
cout<<"Introduceti numele: "<<endl;
cin>>nume2;
cin.sync();
if (!string_litere(nume2)){system("cls");goto nume_gresit;}
while (data_base >> nume){
if (nume == nume2) goto pin_gresit;
else {
system("cls");
cout<<"Nume gresit! Nu sunteti inregistrat?"<<endl;
repetare:
cout<<"1 - Sunt inregistrat"<<endl<<"2 - Nu sunt inregistrat"<<endl;
cin>>inregistrat; cin.clear(); cin.ignore();
if (inregistrat==1) goto nume_gresit;
else if (inregistrat==2) ro_utilizator_nou();
else {system("cls"); goto repetare;
}
}
}
pin_gresit:
cout<<"Introduceti PIN-ul:"<<endl;
cin>>pin; cin.clear(); cin.ignore();
while (data_base >> nume >> varsta >> bani >> card_number >> card_pin){
if (pin == card_pin) goto reusit;
else {system("cls"); cout<<"PIN gresit!"; goto pin_gresit;}
}
reusit:
system("cls");
cout<<"Logare reusita!"<<endl<<endl;
optiune_gresita:
cout<<"1. Informatii cont."<<endl;
cout<<"2. Cautare utilizator."<<endl;
cout<<"3. Adaugare fonduri."<<endl;
cout<<"4. Transfer bancar."<<endl;
cout<<"5. Retragere numerar."<<endl<<endl;
cout<<"9. Delogare."<<endl<<endl;
cin>>optiune;
}
当我输入一个合适的名称时,它说这个名字是错误的;或者当这个名字起作用时,任何数字都被认为是正确的引脚。
PS:随着时间的推移,我计算了年龄("varsta"(; 附言2:"巴尼"的意思是钱 p.s.3:我从 https://www.youtube.com/watch?v=p3m3qLNX9zA 中举了一些例子,但它是不同的;我的代码(完整(在这里:https://www.mediafire.com/file/zmujjs9014mej7c/BCR_ATM.cpp/file
当你写文件时,每个人都在一行上:
new_data_base << nume << ' ' << varsta << ' ' << bani << ' ' << card_number << ' ' << card_pin <<endl;
所以当你读文件时。始终一次读取一条完整记录。这样,您就不必担心部分读取或查找下一条记录的开头。
所以替换:
while (data_base >> nume){
跟
while ( data_base >> nume >> varsta >> bani >> card_number >> card_pin ) {
然后,您可以删除尝试忽略或提取对象不同部分的部分。
我的下一条建议很重要。
- 让这个工作/
- 转到 https://codereview.stackexchange.com
请注意,他们只查看工作代码,因此请先执行第一步。 - 获取评论。这一点非常重要。
你用了这么多坏习惯。你现在需要停下来。你需要一些关于如何编写代码的真正建议。如果你继续沿着这条路走下去,你只会陷入一个非常糟糕的形状,代码是无法挽救的。
停止使用MediaFire来托管您的源。没有人会下载它。给自己一个github帐户。
相关文章:
- 如何使用文件中的文本,例如变量
- C++将文本从.txt文件解析为变量
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 文本 0 和 0 作为变量如何使用函数 __builtin_clz 产生不同的行为?
- 在C++中,从构造函数中将字符串文本分配给成员const char*变量时会发生什么
- 为什么 mfc 中静态文本工具中的变量未定义
- c++ 添加具有写入文件错误的文本变量
- 将文本文件读入类变量 - c++
- C++文本宏中的字符串变量
- 如何从文本文件中读取一行并将其分为不同的变量
- 是否有一种方法可以从文本文件中读取并将单个数据存储为不同的变量
- c ofstream写入变量到文本文件中
- 尝试使用文本文件初始化多个结构变量
- 如何在C++的文本变量中添加变量值
- fstream 变量获取文本文件中的所有行
- 声明具有常量引用与常量变量的常量文本字符串
- 更改变量文本
- 声明并初始化指针后,何时在变量文本前面加上星号,何时不作为前缀
- 如何创建以字符串格式使用给定变量文本的宏(或其他工具)
- 我如何实现由用户定义的变量文本文件名,然后从中获得3x3矩阵