我认为在 C++ 函数中应该如何工作的错误路径

Bad path not working how I think it should in C++ function

提问人:Asutherland8219 提问时间:7/17/2023 最后编辑:Vlad from MoscowAsutherland8219 更新时间:7/17/2023 访问量:76

问:

我正在参加 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. 

应该发生的事情是它挂起并等待我输入另一个值以确认我想如何进行。

C++ while-loop cin

评论

0赞 n. m. could be an AI 7/17/2023
我不确定你说的“糟糕的路径”是什么意思,我的猜测是“糟糕的输入”。您的问题是在尝试失败后重试不当。您需要熟悉 和 .有许多问题和答案可以成功处理这个确切的问题,请使用页面顶部的搜索功能来查找它们。cin::ignorecin::clear
2赞 n. m. could be an AI 7/17/2023
另一种非常有效的方法是使用 .如果您的程序需要输入为数字,请将您得到的字符串转换为数字。std::getline
0赞 Chipster 7/17/2023
我相信可以回答你的问题。

答:

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' ); 

清除输入缓冲区。否则,变量将读取缓冲区的当前内容,根据您提供的输出,该内容是字母 ..retryf

此外,似乎使用 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 如果无法从输入缓冲区读取数据,则该数据将保留在缓冲区中。