Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在条件设置为true时防止do…while循环多次循环_C++ - Fatal编程技术网

C++ 如何在条件设置为true时防止do…while循环多次循环

C++ 如何在条件设置为true时防止do…while循环多次循环,c++,C++,除选项1外,其他所有选项均有效。用户输入名称和帐户类型后。它进入一个无限循环。如何设置do while循环来修复此行为?有人能帮我解决这个问题吗 #include <iostream> #include <queue> #include <stack> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; #

除选项1外,其他所有选项均有效。用户输入名称和帐户类型后。它进入一个无限循环。如何设置do while循环来修复此行为?有人能帮我解决这个问题吗

#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

#include<iostream>
#include<string>
#include<iomanip>
#include<cstdlib>
using namespace std;

class Bank{

    //data members
private:
    string customer_name;
    double account_number;
    string type_of_account;
    double account_balance;
    double balance;


    //function declaration
public:
    void assignInitialValue(void);
    void deposit();
    void makeWithdrawal();
    void displayNameAndBalance();
    void displayDetails();
};

//function definitions
void Bank::assignInitialValue(void){
    cout<<"Welcome to Our Bank."<<endl;
    cout<<"To open an Account with us,please supply the following details"<<endl;
    cout<<"Enter your name in full"<<endl;
    cin>>customer_name;
    cout<<"Choose an Account type"<<endl;
    cin>>type_of_account;
    cout<<"Enter your Account Number"<<endl;
    cin>>account_number;
    cout<<"Enter an Amount you will like to open this account with"<<endl;
    cin>>account_balance;
    cout<<"Thank you for banking with us";
}

void Bank::deposit(void){
    scout<<"Enter the amount you want to deposit"<<endl;
    double deposit_amount = 0.0;
    cin>>deposit_amount;
    account_balance = account_balance + deposit_amount;
    cout<<"Your new balance is now:"<<account_balance;
    balance = account_balance;
}

void Bank::makeWithdrawal(void){
    int withdrawal_amount;
    cout<<"\nBalance Amount = "<<balance;
    cout<<"\nPlease Enter the Amount you want to withdraw:-";
    cin>>withdrawal_amount;
    if(!(withdrawal_amount > balance))
        balance=balance-withdrawal_amount;
    else
    {
        cout<<"Insufficient Balance";
    }
    cout<<"Your Available Balance is:"<<balance;
}
void Bank::displayDetails(void)
{
    cout<<endl<<endl<<endl;
    cout<<setw(50)<<"CUSTOMER DETAILS"<<endl;
    cout<<setw(50)<<"Customer Name "<<customer_name<<endl;
    cout<<setw(50)<<"Account Number."<<account_number<<endl;
    cout<<setw(50)<<"Account Type"<<type_of_account<<endl;
    cout<<setw(50)<<"Balance"<<balance<<endl;
}

int main()
{
    Bank app;
    int Activity_code;

    do
    {
        cout<<"\n\nAvailable Transactions\n\n";
        cout<<"1)  To create Account with us press 1\n";
        cout<<"2)  To make deposit press 2\n";
        cout<<"3)  To make Withdrawal press 3\n";
        cout<<"4)  To Display All Details press 4\n";
        cout<<"5)  EXIT\n";
        cout<<"Please enter an Activity choice :-";

        cin>>Activity_code;


        switch(Activity_code)
        {
            case 1: app.assignInitialValue();
                break;
            case 2: app.deposit();
                break;
            case 3: app.makeWithdrawal();
                break;
            case 4: app.displayDetails();
                break;
            case 5: goto terminate;
        }
    }while(true);
terminate:
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#包括
#包括
#包括
#包括
使用名称空间std;
班级银行{
//数据成员
私人:
字符串客户名称;
双帐号;
账户的字符串类型;
双倍账户余额;
双平衡;
//函数声明
公众:
无效值(void);
无效存款();
撤回无效();
void displayname和balance();
无效显示详细信息();
};
//函数定义
作废银行::assignInitialValue(作废){

cout如果您的输入是意外的,那么前面会有问题,例如,您的程序需要双精度输入,但您的输入无法正确转换。在这种情况下,cin的缓冲区不会被清除,并且您的循环在没有再次询问您的情况下就有输入,因此会重复一次又一次

我建议使用字符串进行输入,并在阅读后进行转换。 下面是修改后的函数assignInitialValue():

void Bank::assignInitialValue(void){
我不能完全谈论这个话题(这对我来说是罕见的),所以我还是回答这个问题为好


>
只读取1个空格分隔的标记,也就是1个单词,因此
cin>>客户名称;
将在“John Smith”中遇到很多问题。这将留下“Smith”在要解析为
类型的\u帐户的流中
,这将使流处于非常糟糕的状态,如果不使用
cin清理,将导致无限循环。清除
cin.ignore
。我建议在
std::getline上阅读,一次阅读多个单词。

您能说明什么是e预期行为?说真的,伙计,如果你不能以一种可理解的方式格式化代码,没有人会想阅读你的代码。这次为你做了,但请注意花点时间。你可能认为这不重要,但当你在十几个源文件中倾注了十万行代码时,这会产生不同。请说明fy您要折叠的部分。如果您不希望某个对象循环不止一次-不要循环!如果
If
statement.user4581301 cin.ignore()则称为“仅循环一次的循环”在很大程度上帮助我解决了这个问题,但问题是,当我尝试输出客户名称时,它是空的,当我尝试打印帐号时,客户名称会出现。似乎无法解决这个问题。Thanx供您输入。@HenryNnonyelu这里可能发生的是
cin。忽略
会因为按enter键而消耗掉一个剩余的换行符。是否不要关注
忽略
。在出现错误后使用它进行清理。您真正想要的是能够一次读取整行用户输入。为此,您需要
std::getline
void Bank::assignInitialValue(void){
  cout<<"Welcome to Our Bank."<<endl;
  cout<<"To open an Account with us,please supply the following details"<<endl;
  cout<<"Enter your name in full"<<endl;
  cin>>customer_name;
  cout<<"Choose an Account type"<<endl;
  cin>>type_of_account;
  cout<<"Enter your Account Number"<<endl;
  string temp1;
  cin>>temp1;
  istringstream convert1(temp1);
  convert1>>account_number;
  cout<<"Enter an Amount you will like to open this account with"<<endl;
  string temp2;
  cin>>temp2;
  istringstream convert2(temp2);
  convert2>>account_balance;
  cout<<"Thank you for banking with us";
}