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

How to output particular lines of code in C++

本文关键字:代码 输出 C++      更新时间:2023-10-16

我刚刚自学完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;
}