C ++ char没有被写入?

时间:2016-10-09 02:27:22

标签: c++ char cin

我正在从事银行业务计划,但我遇到了一个奇怪的问题

CIN>>任务;

它应该启动一个开关。但它不断跳过输入并结束程序。

我甚至有一个这方面的例子,代码有效,但是这个我不确定我做错了什么。

#include<iostream>
#include<conio.h>
#include<string>
#include <ctype.h>
using namespace     std;

int main()
{

    string name = "";
    int count;

    char task;
    double bal = 0;

    int depo, withd;
    cout << "Welcome to bank of the future! Please sign in" << endl;

    cin >> name;

    cout << "Enter your account number." << endl;
    cin >> count;
    cout << "Welcome back " << name << " What would you like to do?";

    cout << "\n A. Deposit \n B. Withdraw \n C. Show balance \n D. Quit" << endl;


    cin >> task;

    //just to check if it's being skipped over or not.
    cout << "egg " << endl;

    switch (task)
    {
    case 'A':
        cout << "Enter an amount to deposit" << endl;
        cin >> depo;
        cout << "Prevous balance: " << bal << endl;
        bal = bal + depo;
        cout << "New balance: " << bal << endl;
        break;

    case 'B':
        cout << "Enter an amount to withdraw" << endl;
        cin >> withd;
        cout << "Prevous balance: " << bal << endl;
        bal = bal - withd;
        cout << "New balance: " << bal << endl;
        break;
    case 'C':
        cout << "Your current balance is " << bal << "\n For account: " << name << "Acount number: " << count << endl;
        break;
    case 'D':
        cout << "Goodbye forever" << endl;
        break;

    }


    return 0;

}

1 个答案:

答案 0 :(得分:0)

我认为它可能与大写或小写的char有关。在交换机中,您指定该任务是大写的。当我用大写字母尝试你的代码时,它对我有用,但是当我输入一个小写字母时,它结束了程序。