要求两次帐户类型

时间:2013-11-08 21:44:51

标签: c++

现在正在研究这个程序。 (有一些问题,并在这里问过几次。)但是进入另一个问题!该程序要求我的帐户类型两次。无法弄清楚为什么或如何解决它。感谢任何帮助,谢谢!

    /*
project3.cpp
Andre Fecteau
CSC135-101
October 29, 2013
This program prints a bank's service fees per month depending on account type
*/

#include <iostream>
using namespace std;
/*
Basic Function for Copy Paste
<function type> <function name> (){

// Declarations
// Initalizations
// Input
// Process
// Output
// Prolouge
}
*/

void displayInstructions (){
// Declarations
// Initalizations
// Input
// Process
// Output
cout <<"| -------------------------------------------------------------- |" << endl;
cout <<"| ---------- Welcome to the bank fee calculator ---------------- |" << endl;
cout <<"| -------------------------------------------------------------- |" << endl;
cout <<"| This Program wil ask you to eneter your account number.        |" << endl;
cout <<"| Then it will ask for your account type Personal or Commercial. |" << endl;
cout <<"| Then ask for the amount of checks you have written.            |" << endl;
cout <<"| Lastly it will output how much your fees are for this month.   |" << endl;
cout <<"| -------------------------------------------------------------- |" << endl;
cout << endl;
// Prolouge
}

int readAccNumb(){
  // delarations
  int accNumber;
  // intitalizations
  accNumber = 0.0;
  // input
  cout << "Please Enter Account Number:";
  cin >> accNumber;
  // Procesas
  // output
  // prolouge
  return accNumber;
}

int checksWritten (){
// Declarations
int written;
// Initalizations
written = 0.0;
// Input
cout <<"Please input the amount of checks you have written this month:";
cin >> written;
// Output
// Prolouge
return written;
}

char accType (){
// Declarations
char answer;
int numberBySwitch;
// Initalizations
numberBySwitch = 1;
// Input
while (numberBySwitch == 1){
    cout << "Please Enter the acount type (C for Comerical and P for Personal):";
    cin >> answer;
// Process
switch (answer){
    case 'p':
        answer = 'P';
        numberBySwitch += 2;break;
    case 'P':
        numberBySwitch += 2;break;
    case 'c':
        answer = 'C';
        numberBySwitch += 3;break;
    case 'C':
        numberBySwitch += 3;break;
    default:
        if(numberBySwitch == 1) {
        cout << "Error! Please enter a correct type!" <<endl;
        }
    }
}
// Output
// Prolouge
return answer;
}

int commericalCalc(int checksWritten){
// Declarations
int written;
int checkPrice;
// Initalizations
checkPrice = 0.0;
// Input
// Process
if(written < 20){
    checkPrice = 0.10;
}
// Output
// Prolouge
return checkPrice;
}

int personalCalc(int checksWritten){

}

double pricePerCheck(char accType, int checksWritten){
// Declarations
double price;
char answer;
// Initalizations
price = 0.0;
// Input
// Process
if(accType == 'P'){
}
if(accType == 'C'){
    if(checksWritten < 20){
        price = 0.10;
    }
}
// Output
// Prolouge
return price;
}

int main(){
  // Declarations
  int accountNumb;
  char theirAccType;
  int writtenChecks;
  double split;
  // Initalizations
  accountNumb = 0.0;
  writtenChecks = 0.0;
  split = 0.0;
  theirAccType = ' ';
  // Input
  displayInstructions();
  theirAccType = accType();
  accountNumb = readAccNumb();
  split = pricePerCheck(accType(), checksWritten());
  // Output
cout << endl;
cout << "Account Type: " << theirAccType << endl;
cout << "Check Price: " << split << endl;
  // Prolouge
 return 0;
}

3 个答案:

答案 0 :(得分:2)

theirAccType = accType();
...
split = pricePerCheck(accType(), checksWritten());
//                    ^^^^^^^^^

你第二次打电话给accType。您应该传入用于在第一行上保存初始调用的变量。

split = pricePerCheck(theirAccType, checksWritten());
//                    ^^^^^^^^^^^^

答案 1 :(得分:0)

帐户类型请求在accType()函数中生成。您在此代码中调用该函数两次:

 displayInstructions();
  theirAccType = accType();
  accountNumb = readAccNumb();
  split = pricePerCheck(accType(), checksWritten());
调用

accType()将值分配给theirAccType,并再次作为pricePerCheck()的参数。

您可能需要

  split = pricePerCheck(theirAccType, checksWritten());

答案 2 :(得分:0)

当您致电pricePerCheck()时,您传递了调用accType()而不是theirAccType的结果:您存储了第一个电话的结果,但您正在拨打第二个电话。 C ++不会记住函数调用的结果。

顺便说一句,您希望以更高的警告级别编译代码!您的代码中存在许多致命问题:

  1. personalCalc()被声明为返回int但不返回任何内容(但该函数不是调用,即您的问题显然不是SSCCE
  2. 函数commericalCalc()使用未初始化变量(written)的值。它的名字似乎也很糟糕。