本文介绍了如何删除"1.#INF"在Visual C ++中,当您将任何数字除以零时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个小问题,我无法弄清楚该如何消除它,就像我在控制台中将任何数字除以一样,它向我显示了 不知道它来自哪里,有什么办法可以消除它.

例如,如果我将4/0除以1,则显示#INF,我知道我们不能除以零,但是如何删除呢?

这是简单的c ++程序:

hello guys,
i have a small question which i cannot figure out how to get rid of that like when ever i divide any number by zero in console it shows me i don''t know where it comes from is there any way to remove this.

for example if i divide 4/0 it shows 1.#INF i know we cannot divide by zero but how can i remove this?

here is the simple c++ program:

#include <iostream>
#include <math.h>

using namespace std;

int main()
{

	double left;
	double right;
	char op;
	
	cin >> left >> op >> right;
	pow(right, left);

	switch (op){
		case ''^'':
			cout << pow(left, right) << endl;
			break;

		case ''+'':
			cout << left + right << endl;
			break;

		case ''-'':
			cout << left - right << endl;
			break;

		case ''*'':
			cout << left * right << endl;
			break;

		case ''/'':
			cout << (left / right) << endl;
			break;
	
		default:
			cout << "Error: invalid operator" << endl;
			break;
	}

	if (op == ''/'' && right == 0){
		cout << "Error: division by zero" << endl;
	}


	system("pause");
	return 0;
}

推荐答案


switch(op)
...
case '/':
	if (right == 0)
	{
		cout << "Error: division by zero" << endl;
	}
	else
		cout << (left / right) << endl;
	
	break;
...


这篇关于如何删除"1.#INF"在Visual C ++中,当您将任何数字除以零时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 09:44