【问题标题】:how to display error message when met with different condition遇到不同条件时如何显示错误消息
【发布时间】:2016-12-23 00:25:08
【问题描述】:

我从 3 个月前开始学习 C++ 编程,目前遇到了一些问题。

这是我被分配期望输出的预期输出:

虽然调和平均数和几何平均数的公式是:

H 是调和平均,而 G 是几何平均。

我尝试了几种使用 while-loop 或 do-while-loop 以及 if-else 语句来实现预期输出的方法,但每当我故意输入错误,例如字母 s、负数或十进制数时,程序就是直接将我引导到程序的末尾,无需重新输入或进一步输入以进行下一次操作...

这是我最新的代码:

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
double H, G, n, f, x;
long double HX = 0, GX = 1;
double TypeIn[1000] = {};


cout << "How many values to type?: ";
cin >> n;

while (!(n > 0) && (n == static_cast <int> (n)) && !(cin >> n));
{
    if (n != static_cast <int> (n))
    {
        cout << "No decimal number please: ";
        cin >> n;
    }

    else if (!(cin >> n))
    {
        cin.clear();
        cin.ignore();
        cout << "INTEGER number ONLY: ";
        cin >> n;
    }

    else if (n <= 0)
    {
        cout << "the number must be integer number more than 0, please retype: ";
        cin >> n;
    }
}


for (int k = 0; k < n; k++)
{
    cout << "Enter number #" << k + 1 << ": ";
    cin >> x;

    while (!(x > 0) && (x == static_cast <int> (x)) && !(cin >> x));
    {
        if (x != static_cast <int> (x))
        {
            cout << "No decimal number please: ";
            cin >> x;
        }

        else if (!(cin >> x))
        {
            cin.clear();
            cin.ignore();
            cout << "INTEGER number ONLY: ";
            cin >> x;
        }

        else if (x <= 0)
        {
            cout << "the number must be integer number more than 0, please retype: ";
            cin >> x;
        }
    }
        TypeIn[k] = x;

    HX += 1 / TypeIn[k];
    GX *= TypeIn[k];
}

H = n / HX;
f = 1 / n;
G = pow(GX, f);

cout << "\nFor data:";
for (int k = 0; k < n; k++)
{
    cout << TypeIn[k];
}
cout << setprecision(5);
cout << "\n\nThe harmonic mean is " << H << endl;
cout << "The geometric mean is " << G << endl;

system("pause");
return 0;
}

这里非常感谢帮助和改变。

【问题讨论】:

  • 为什么不把输入当作一个字符串,只确认字符串中的所有字符都是数字(意思是它是一个正数)?只要您检查 '0' 不是唯一的字符,您就可以大大简化您的代码,然后再转换为 int。
  • urm,预期的检查是这样的:如果输入是任何字母,则会显示'only integer number'的错误;如果输入为负数或零,将显示“数字必须是大于0的整数”的错误;如果输入是小数位,如 5.6 或 4.00,则会显示“没有小数位,只有整数”的错误......只有当它是一个正整数时才会继续......
  • 离题:cin.ignore(); 不足以在用户输入错误后进行清理。它只忽略一个字符,错误的输入可能远不止于此。 cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n'); 应该通过忽略直到用户按下回车键(或者用户输入足够长的时间以溢出输入流)来覆盖它

标签: c++ if-statement for-loop while-loop do-while


【解决方案1】:

您可以对字符串执行以下操作:

string line;
int yourNumber;

getline(cin, s);

//Cycle through the word and check for characters such as '@' which don't fit any of the specific error messages.
for (int i = 0; i < line.length(); i++){
    if (line[i] != '-' || line[i] != '.' || !(line[i] >= '0' && line[i] <= '9'){
        cout << "Generally invalid input such as 5sda2" << endl;
        break;
    }
}

//This line just checks if '-' exists in the string.
if (line.find('-') != std::string::npos)
    cout << "No negatives" << endl;
else if (line.find('.') != std::string::npos)
    cout << "No decimals" << endl;
else
    yourNumber = std::stoi(line);

std::stoi 函数将 std::string 转换为整数。它在

中定义
#include <string> header

对于std::string,无论如何你都需要它。如果在使用 getline 之前使用 cin,请确保调用 cin.ignore 以克服缓冲区中剩余的空白 (When and why do I need to use cin.ignore() in C++?)。

【讨论】:

  • 我认为我自己和其他一些编辑的编辑发生了冲突,可能已经打破了这个问题。我已经恢复让另一个人在罐头上再踢一次。但是,请将损坏的“#include header”标头修复为“#include &lt;string&gt; header”
  • 我的错,它以伪代码开始,所以没有标题。添加了
  • 其实问题出在代码块下方的文字中。如果没有代码标记 #include &lt;string&gt;&lt;string&gt; 将被视为不受支持的 XML 标记并被丢弃。
  • 我明白了......伪代码要好得多......哦,是的,我忘了问并提到了,从我的程序那里你应该注意到所有那些 if-else具有这些输出消息然后是cin的语句,因为预期的程序是,弹出任何错误消息,我将不得不重新输入数字,如果再次发现错误,它将需要我一次又一次地重新输入,直到没有错误,然后只进行下一个操作......那么,如果我在这里对您回复的伪代码采取相同的方式可以吗?
  • 啊,我明白了,从来不知道 xml 标记。谢谢。 @AetheneLockhart:是的,保持输入的一种简单方法是创建一个以 true 开头的 bool(假设 bool redoInput = true)并将整个块放入 while(redoInput)形式的 while 循环中。最后,如果输入良好(我称之为 stoi),您也可以将 redoInput 更改为 false,然后它将退出循环。如果 if / else 块下有多行,请确保使用方括号..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多