0
我正在开展银行业务,但我遇到了一个奇怪的问题C++字符没有被写入?
cin >> task;
它应该启动一个开关。但它会不断跳过输入并结束程序。
我甚至有这方面的例子,该代码的作品,但这一个不,我不知道我做错了什么。
#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;
}
[为什么不使用'using namespace std;'](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – amanuel2
'cin> > name'在流中有一个换行符(你输入它!)。这是由'cin >>任务'读取的 - 在读取任务输入的字符之前。给你的'switch'一个'case'\ n':'你会看到它。 – Peter