简体   繁体   English

如何在 C++ 中以不同的方式接受来自同一行的用户输入?

[英]How can I accept user input differently from the same line in C++?

I have an assignment for my intro C++ class to make a program that has a user select C, D, or E to process a check, deposit a check, or end the program respectively.我的 C++ 介绍类有一个任务,让用户选择 C、D 或 E 来分别处理支票、存入支票或结束程序。 The professor specified that if a user chooses C or D, he would like to accept both the selection and the amount of money in the same line.教授指定,如果用户选择 C ​​或 D,他希望在同一行中同时接受选择和金额。 For example, if a user wants to deposit a check for $20, they would enter "D 20" in one line.例如,如果用户想存入一张 20 美元的支票,他们将在一行中输入“D 20”。 I have it set up as such:我是这样设置的:

cout << "Enter the beginning balance:\n\n";
cin >> balance;

cout << "Commands:\n";
cout << "C - process a check\n";
cout << "D - process a deposit\n";
cout << "E - end the program\n\n";

while (choice != 'E')
{
    cout << "Enter a transaction:\n";
    cin >> choice >> amount;

Followed by a switch statement.后面跟着一个switch语句。 The code itself works properly when entering C or D, but when I go to enter E to end the program, it will only work if I add a number to the end, because the line asking for input wants a char and a float.代码本身在输入 C 或 D 时可以正常工作,但是当我输入 E 结束程序时,只有在最后添加一个数字才能工作,因为要求输入的行需要一个字符和一个浮点数。 However, in the example output my professor showed, you could just enter E and it would terminate.但是,在我的教授展示的示例输出中,您只需输入 E 即可终止。 Is there any way around this?有没有办法解决? How can I set it up so it accepts E differently from C and D?我如何设置它以使其接受与 C 和 D 不同的 E?

EDIT: Here is the switch statement:编辑:这是 switch 语句:

switch(choice)
{
    case 'C':cout << fixed << setprecision(2);
             cout << "Processing check for $" << amount << endl;
             balance = processCheck(amount, balance);
             cout << "Balance: $" << balance << endl;
             break;
    case 'D':cout << fixed << setprecision(2);
             cout << "Processing deposit for $" << amount << endl;
             balance = depositCheck(amount, balance);
             cout << "Balance: $" << balance << endl;
             break;
    case 'E':cout << "Processing end of month\n";
             cout << "Final balance: $" << balance << endl;
             break;
    default : cout << "Invalid choice\n";
}

Already answered in comments, but anyway:已经在评论中回答了,但无论如何:

Replace this code替换此代码

cin >> choice >> amount;

by gradual and conditional input code:通过渐进和条件输入代码:

cin >> choice;
if (choice == 'C' || choice == 'D')
    cin >> amount;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户在 C++ 的同一行中输入字符串和整数时,如何拆分字符串和整数? - How can I split string and integer when user input both the string and the integer in the same line in C++? 如何在同一行C ++上输入多个变量 - How can I input multiple variables on the same line C++ 在c ++中,是否有可能/如何在输入后显示控制台输出,在同一行上,在给出用户输入之前? - in c++, is it possible to / how can i display console output after input, on same line, before user's input is given? 如何在C ++中接受来自用户的二进制输入? - How to accept binary input from user in C++? 如何通过 C++ 中的代码设置用户输入? - How can I set User Input from Code in C++? 如何在与用户输入相同的位置提示? C ++ - How to cout on same line as user input? C++ 从C ++中的文本文件逐行读取,如何确定输入为double? - Reading line by line from text file in C++, how can I make sure the input is a double? 如何在一行中获取用户的多个输入 c++ - How to get multiple input from user in one line c++ 如何在 C++ 中接受用户输入作为向量的长度 - How to accept user input as the length of a vector in C++ c++ 跨平台控制台应用程序,如何接受用户输入的运行时命令? - c++ cross-platform console application, how to accept runtime commands from user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM