gpt4 book ai didi

c++ - 如何在 C++ 中输出特定的代码行

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:17 24 4
gpt4 key购买 nike

我刚刚自己完成了 C++ 的学习(没有外部帮助,只是通过一些网站教程),现在我正在做我自己的迷你项目。该项目只是一个注册用户的基本银行账户系统(询问用户他们的帐号、姓名和当前余额)

这是我到目前为止所做的。我创建了一个名为“BankAccount.text”的文本文件,现在它包含以下内容:

欢迎使用您的银行账户

您的帐号:

您的名字:

您的姓氏:

您当前的余额:

您的帐号:1

你的名字:ftest

你的姓氏:ltest

您当前的余额:$10

我的问题是,如何向想要查看其信息的用户输出特定的文本行,例如,要求用户输入其帐号(如上所示,为 1),然后程序输出四行 (1, ftest, ltest, $10)

这是我的代码(我不会展示整个代码):

#include <fstream> 
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

int accnum; //account number of user
string fname; //first name of user
string lname; //last name of user
double balance; //balance amount of user


void show_record()
{
cout << "Enter your account number: ";
cin >> accnum;

/*
ifstream bnkacct("BankAccount.text"); //needed to read the bank account information file

if(!bnkacct) //if we couldn't open the output file for reading
{
cerr << "Sorry, we could not open your bank account information. We are sorry for the inconvenience" << endl;
system("pause"); //needed, or else the program will end right away
exit(1);
}
else
{
while (bnkacct)
{
string strInput;
getline(bnkacct, strInput);

cout << strInput << '\n';
}
}
*/
}

int main()
{
cout << "**Banking account information system**" << '\n';
cout << "\n";
cout << "Select one of the options below" << '\n';
cout << "1: Add record to file" << '\n';
cout << "2: Show record from file" << '\n';
cout << "3: Search record from file" << '\n';
cout << "4: Update record information" << '\n';
cout << "5: Delete record from file" << '\n';
cout << "6: Exit options" << '\n';
cout << "\n";

int option;
cout << "Enter an option: ";
cin >> option;

cout << "\n";

switch (option)
{
case 1:
add_record();
break;
case 2:
show_record();
break;
default: //else if the user did not enter either of the following options
cout << "You must enter the following options specified!" << endl;
break;
}

system("pause"); //needed if using Visual Studio, else the program ends immediately after program is run

return 0;
}

那么我该如何解决这个问题呢?

最佳答案

@David C. Rankin 的想法是对的。我为你整理了一个基本的实现。它几乎没有错误检查、数据验证或任何聪明的东西,所以请记住这里有很大的改进空间。但它有效并且应该给你一些继续下去的东西。

struct AccountDetails
{
string fname; //first name of user
string lname; //last name of user
double balance; //balance amount of user
};

void show_record(std::map<int, AccountDetails> bankRecords, int accountNumber)
{
auto itr = bankRecords.find(accountNumber);
AccountDetails detailsToDisplay;
if (itr != bankRecords.end())
{
detailsToDisplay = itr->second;

cout << "Account number: " << accountNumber << '\n';
cout << "Your first name: " << detailsToDisplay.fname << '\n';
cout << "Your last name: " << detailsToDisplay.lname << '\n';
cout << "Your balance: " << detailsToDisplay.balance << '\n';
}
else
{
cout << "Sorry, we could not open your bank account information. We are sorry for the inconvenience." << endl;
}
}

std::map<int,AccountDetails> ReadRecords(string fileName)
{
std::map<int, AccountDetails> records;
ifstream bankFile(fileName);
string accountNumber;
string firstName;
string lastName;
string balance;

AccountDetails temp;

if(bankFile.is_open())
{
while (!bankFile.eof())
{
getline(bankFile, accountNumber);
getline(bankFile, firstName);
getline(bankFile, lastName);
getline(bankFile, balance);

temp.fname = firstName;
temp.lname = lastName;
temp.balance = atof(balance.c_str());

records.emplace(atof(accountNumber.c_str()),temp);
}
}
return records;
}

int main()
{
cout << "**Banking account information system**" << '\n';
cout << "\n";
cout << "Select one of the options below" << '\n';
cout << "1: Add record to file" << '\n';
cout << "2: Show record from file" << '\n';
cout << "3: Search record from file" << '\n';
cout << "4: Update record information" << '\n';
cout << "5: Delete record from file" << '\n';
cout << "6: Exit options" << '\n';
cout << "\n";

int accountNumber = INT_MAX;
int option;
cout << "Enter an option: ";
cin >> option;
cout << "\n";

std::map<int,AccountDetails> bankRecords = ReadRecords("BankAccount.text");

switch (option)
{
case 1:
break;
case 2:
cout << "Enter your account number: ";
cin >> accountNumber;
show_record(bankRecords, accountNumber);
break;

default:
cout << "You must enter the following options specified!" << endl;
break;
}

system("pause");
return 0;
}

关于c++ - 如何在 C++ 中输出特定的代码行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54544697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com