【问题标题】:C++ char not being written?C++ char 没有被写入?
【发布时间】:2017-02-17 18:13:10
【问题描述】:

我正在开发一个银行项目,但遇到了一个奇怪的问题

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;

}

【问题讨论】:

  • cin &gt;&gt; name 之后,流中还有一个换行符(您输入了!)。 cin &gt;&gt; task 会读取它 - 在读取您为任务输入的字符之前。给你的switchcase '\n' : ,你会看到的。

标签: c++ char cin


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多