提问人:Asutherland8219 提问时间:7/17/2023 最后编辑:Vlad from MoscowAsutherland8219 更新时间:7/17/2023 访问量:76
我认为在 C++ 函数中应该如何工作的错误路径
Bad path not working how I think it should in C++ function
问:
我正在参加 C++ 编程课程并从事一个项目。我对这个问题的糟糕路径有疑问。这是一个基于用户的温度转换输入问题。我有一条快乐的道路,但糟糕的道路并没有像我预期的那样行事。根据我的理解,如果我遇到了错误的路径,它应该等待我重新输入另一个输入,然后再退出。不过,在我的代码中,它只是打印错误路径中的所有信息并退出。
#include "unitConversionHeader.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <typeinfo>
#include <any>
using namespace std;
void tempOutputs(int, char);
void tempConversion() {
// Parent function.Collect the inputs, handle the conversions and checks then return the result.
int temperature;
char units;
char retry;
char killswitch;
bool running;
running = true;
cout << "This program converts Temperatures from Fahrenheit to Celsius and vice versa. \n";
while (running) {
cout << "Please enter your temperature: \n";
cin >> temperature;
cout << "\n";
if (cin.fail()) {
cout << "The value you have entered is incorrect or not supported. Would you like to try again? (Y/N): \n";
cin >> retry;
units = toupper(retry);
if (units == 'Y') {
break;
}
else {
cout << "Too many incorrect attempts. Exiting program now. \n";
cout << "Thank you. Goodbye. \n";
running = false;
}
}
else if (cin.good()) {
cout << "Please enter your units (F/C): ";
// convert to upper to eliminate risk of inputting the wrong value, lowercase is sufficient
cin >> units;
units = toupper(units);
cout << "\n";
if (units == 'F' || units == 'C') {
tempOutputs(temperature, units);
cout << "\n";
cout << " Do you want another conversion? (Y/N): \n";
cin >> killswitch;
killswitch = toupper(killswitch);
}
if (killswitch == 'N') {
cout << "Thank you. Goodbye. \n";
running = false;
}
}
}
}
void tempOutputs(int temperature, char units) {
// After receiving the inputs we add the conversion logic and then run the calculations
string lhs = "Celsius";
string rhs = "Celsius";
int calculation;
if (units == 'F') {
lhs = "Fahrenheit";
}
else {
rhs = "Fahrenheit";
}
// taken from header
calculation = unitConversion(temperature, units);
cout << "A temperature of " << temperature << " degrees " << lhs << " is equivalent to " << calculation << " degrees " << rhs << ". \n";
}
当我运行问题时,如果我为第一个输入输入一个字符并点击 cin.fail 逻辑,我只会得到以下输出:
This program converts Temperatures from Fahrenheit to Celsius and vice versa.
Please enter your temperature:
f
The value you have entered is incorrect or not supported. Would you like to try again? (Y/N):
Too many incorrect attempts. Exiting program now.
Thank you. Goodbye.
应该发生的事情是它挂起并等待我输入另一个值以确认我想如何进行。
答:
0赞
Vlad from Moscow
7/17/2023
#1
在这些声明之前
cout << "The value you have entered is incorrect or not supported. Would you like to try again? (Y/N): \n";
cin >> retry;
units = toupper(retry);
你应该写
#include <limits>
//...
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
清除输入缓冲区。否则,变量将读取缓冲区的当前内容,根据您提供的输出,该内容是字母 ..retry
f
此外,似乎使用 break 语句
cout << "The value you have entered is incorrect or not supported. Would you like to try again? (Y/N): \n";
cin >> retry;
units = toupper(retry);
if (units == 'Y') {
break;
与提示相矛盾。
评论
0赞
Asutherland8219
7/18/2023
太棒了,谢谢!你知道为什么会这样吗?它不应该默认清除吗?
0赞
Vlad from Moscow
7/18/2023
@Asutherland8219 如果无法从输入缓冲区读取数据,则该数据将保留在缓冲区中。
评论
cin::ignore
cin::clear
std::getline