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

标签 c++

测试环境: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/

相关文章:

c++ - 如何将局部变量传递给 lambda 函数?

c++ - 使用 MODULEENTRY32 和 MODULEINFO,在单独的进程中扫描特定 INT 值的步骤是什么?

c++ - Lock/CriticalSection 与 c++ 中的 volatile 的可见性

c++ - qt 通过 qprocess 运行 shell 命令

c++ - Windows proc/callback 函数可以是类的成员函数吗?

c++ - std::move weak_ptr::lock 的返回值弄乱了 shared_ptr 的引用计数?

c++ - Lua C API : Retrieve values from Lua function returning a table in C code

c++ - 如何使用可变终点迭代基于范围的 for 循环

c++ - 链接器如何确定链接到哪个函数?

c++ - 查找大数数字总和的最快方法