如何在C++中输出特定的代码行
How to output particular lines of code in C++
我刚刚自学完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;
}
相关文章:
- 我的代码输出总是得到 0
- 代码输出随机符号,我不确定出了什么问题
- 为什么当我正确执行语法时,我的代码输出为 0
- 为什么这段代码输出这么多数字
- 当我输入任何内容时,我的代码输出无效
- 我在让我的代码输出整个链表时遇到问题.它只是输出我修改过的一些文本文件,而不是整个东西
- 在 netbeans 中将C++代码输出为汇编语言
- 使用向量(STL)时,未定义的行为,请说明理性背后的以下代码输出
- 当我尝试运行此代码输出时"merge.exe has stopped working"?
- 欧拉项目 #14 代码输出未到来
- C++中的代码输出出现问题
- 如何删除此代码输出的结束空格
- 这段代码输出什么笑脸
- Win32 键转换代码输出不需要的按键
- 在 QT 中将代码输出到主窗口
- 我无法让此代码输出平均工资率,任何人都可以解释原因
- C++字母等效数字代码输出程序
- 我的代码输出重复
- c++代码输出说明
- C++代码输出负值