C++ 请求帐户类型两次

C++ 请求帐户类型两次,c++,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>

这个项目已经做了一段时间了。(遇到了一些问题,在这里问了几次。)但是遇到了另一个问题!程序两次要求输入我的帐户类型。无法找出原因或如何修复它。感谢您的帮助,谢谢

    /*
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.cpp
安德烈·费克图
CSC135-101
2013年10月29日
此程序根据帐户类型打印银行每月的服务费
*/
#包括
使用名称空间std;
/*
复制粘贴的基本功能
(){
//声明
//初始化
//输入
//过程
//输出
//序言
}
*/
无效显示说明(){
//声明
//初始化
//输入
//过程
//输出
库特
您正在第二次调用
accType
。您应该在第一行传入用于保存初始调用的变量

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

帐户类型的请求是在
accType()
函数中发出的。在此代码中,您将调用该函数两次:

 displayInstructions();
  theirAccType = accType();
  accountNumb = readAccNumb();
  split = pricePerCheck(accType(), checksWritten());
调用
accType()
将值分配给
theirAccType
,并再次作为
pricePerCheck()
的参数

你可能需要

  split = pricePerCheck(theirAccType, checksWritten());

当调用“代码> PRICEPACCHECK())/>代码>时,调用调用“代码> > ActhType(<代码)>的结果,而不是<代码> TyrAccType < /C> >:您存储了第一次调用的结果,但正在进行第二次调用。C++没有记住函数调用的结果。

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

  • personalCalc()
    被声明为返回一个
    int
    ,但不返回任何内容(尽管函数不是调用,也就是说,您的问题显然不是调用
  • 函数
    commericalCalc()
    使用未初始化变量(
    writed
    )的值。它的名称似乎也很糟糕

  • personalCalc和CommercialCalc可能是我尝试其他方法时遗留下来的。如果没有使用它们,我会确保将它们删除。你知道如何在Code::blocks中添加更高级别的警告吗?@AndréFecteau:我没有使用Code::blocks(从未使用过IDE…)。我认为Code::blocks可能会在幕后使用gcc。如果您使用的是gcc enable-W-Wall。了解它的工作原理很重要-accType()是一个返回值的函数。您将该值指定为
    TheiracType
    。因此,当您要调用
    pricePerCheck
    时,需要传递*该值*,而不是像您那样(在解决问题之前)再次调用
    accType()'。另一种实现方法是调用
    accType()
    直接调用
    pricePerCheck
    (就像您最初那样),然后第一次不调用它(分配给他们的raccType。正如@0x499602D2所指出的,您调用了它两次。您只需要调用一次;)
      split = pricePerCheck(theirAccType, checksWritten());