输入不是数字时的输出错误。 C ++ [英] Output error when input isn't a number. C++

查看:273
本文介绍了输入不是数字时的输出错误。 C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个函数,从用户的输入中获取一个数字,并找到它的绝对值。我想让它返回一个错误,如果用户输入任何东西,而不仅仅是一个数字。我会怎么做呢?

I am making a function that takes a number from the user's input and finds the absolute value of it. I want to make it return an error if the user inputs anything other than just a number. How would I go about doing that?

(我知道这对于很多人来说可能是一个很简单的问题,但我正在进行第一个编程)

(I know that this is probably an easy question for a lot of you, but I'm taking my first programming class in C++ so I know very little. Any help would be greatly appreciated.)

推荐答案

如果你真的是这样的话试图在惯用的C ++中编程,忽略(故意)。特别是答案指向你的C函数。 C ++可能在很大程度上与C向后兼容,但它的灵魂是一种完全不同的语言。

If you are actually trying to program in idiomatic C++, ignore the (intentionally?) bad advice you are being given. Especially the answers pointing you toward C functions. C++ may be largely backwards-compatible with C, but its soul is a totally different language.

你的问题是基础的,为了一个可怕的家庭作业。特别是如果你这么漂泊,你不知道避免 conio.h 和其他悲剧。所以我只是在这里写出一个解决方案:

Your question is so foundational as to make for a terrible homework assignment. Especially if you're so adrift that you don't know to avoid conio.h and other tragedies. So I'm just going to write out a solution here:

#include <iostream>
#include <string>

// Your function is presumably something like this
// although maybe you are just using integers instead of floats
float myAbs(const float x) {
    if (x >= 0) {
        return x;
    } else {
        return -x;
    }
}

int main(int argc, char* argv[]) {
    // give a greeting message followed by a newline
    std::cout << "Enter values to get |value|, or type 'quit'" << std::endl;

    // loop forever until the code hits a BREAK
    while (true) {
        // attempt to get the float value from the standard input
        float value;
        std::cin >> value;

        // check to see if the input stream read the input as a number
        if (std::cin.good()) {

            // All is well, output it
            std::cout << "Absolute value is " << myAbs(value) << std::endl;

        } else {

            // the input couldn't successfully be turned into a number, so the
            // characters that were in the buffer that couldn't convert are
            // still sitting there unprocessed.  We can read them as a string
            // and look for the "quit"

            // clear the error status of the standard input so we can read
            std::cin.clear();

            std::string str;
            std::cin >> str;

            // Break out of the loop if we see the string 'quit'
            if (str == "quit") {
                break;
            }

            // some other non-number string.  give error followed by newline
            std::cout << "Invalid input (type 'quit' to exit)" << std::endl;
        }
    }

    return 0;
}

这样可以使用iostream类的自然能力。他们可以注意到,当他们无法自动将用户输入的格式转换为所需的格式,并给您一个机会,只是抛出了你的双手错误 - 或尝试用不同的方式解释未处理的输入。

This lets you use the natural abilities of the iostream classes. They can notice when they couldn't automatically convert what a user entered into the format you wanted, and give you a chance to just throw up your hands with an error -or- try interpreting the unprocessed input a different way.

这篇关于输入不是数字时的输出错误。 C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆