gpt4 book ai didi

c++ - 如何保存用户的整数输入,并返回一个字符串值?

转载 作者:行者123 更新时间:2023-11-28 00:18:23 25 4
gpt4 key购买 nike

测试环境:MS Visual 2010 Ultimate。

我有两个函数需要从一个传递到另一个。

功能一:

//Record which account was chosen
string Account::getAccountType()
{
if (accountType == 1)
{
return "Account Type: Chequing";
}
else
{
return "Account Type: Savings";
}

};

此函数将获取用户的输入并确定选择的是支票还是储蓄。

功能二:

    //Print the Account Statement 
void Account::PrintStatement()
{

cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
cout << "SIN Number: " << sinNumber << endl;
cout << "Account Type: " << accountType << endl;
cout << "Transactions: " << transactions << endl;
cout << "Final Balance: " << balance << endl;

};

此函数获取所有已返回值或已保存输入的全局变量,并打印所有输入和所做的计算。

主要.cpp:

//Retrieve client information
cout << "Please fill out the information below for registration:" << endl;
cout << "Enter first name: ";
cin.getline(firstName, 255);
cout << "Enter last name: ";
cin.getline(lastName, 255);
cout << "Enter SIN number: ";
cin.getline(sinNumber, 255);
cout << "Enter initial balance: ";
cin >> balance;
cout << "Enter account type:\n Chequing - 1\n Savings - 2 (Or any number)\n Choice: ";
cin >> accountType;
cout << "\nPlease wait..." << endl;

我的问题:accountType 被打印时(通过 PrintStatement()),它显示了用户输入的整数值。我明白为什么要这样做。我只是不知道如何更改它,以便在使用 1 或其他数字时,PrintStatement() 函数会显示相应的帐户(Chequing 或 Savings)。

我尝试了什么:我尝试将函数 1 更改为基于整数的函数,并让 accountType 等于 Chequing 或 Savings 选项,但整数不能存储字符串值。我尝试改用 char,但出现 const char 错误。

我想做什么:我希望能够获取用户输入的任意数字,并将其传递给 PrintStatement()(通过另一个函数),以便它可以显示相应的帐户。

编辑(为清楚起见添加额外的代码)

账户.cpp

//Initializing account entities
Account::Account(char fn[], char ln[], char sin[], double bal, int actype, int trans)
{
strcpy(firstName, fn);
strcpy(lastName, ln);
strcpy(sinNumber, sin);
balance = bal;
accountType = actype;
transactions = 0;
};

账户.h

#define ACCOUNT_H
#include <iostream>
#include <cstring>

using namespace std;

class Account {

public:
//Object constructor
Account(char firstName[], char lastName[], char sinNumber[], double balance, int accountType, int transactions);

//Object operations
double DepositAmt(double amount);
double WithdrawAmt(double amount);
void PrintStatement();
double getFinalBalance(double fbal);
string getAccountType();
double getTransactions (double cDeposit, double cWithdraw);

private:
//Object properties
char firstName[255];
char lastName[255];
char sinNumber[255];
double balance;
int accountType;
int transactions;
};

最佳答案

在您的 PrintStatement() 函数中,将 accountType 的输出更改为

cout << "Account Type:  " << accountType  << endl;

cout << getAccountType() << endl;

关于c++ - 如何保存用户的整数输入,并返回一个字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28789301/

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