【发布时间】: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 >> name之后,流中还有一个换行符(您输入了!)。cin >> task会读取它 - 在读取您为任务输入的字符之前。给你的switchcase '\n' :,你会看到的。