Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么if语句不返回无穷大?C++;_C++ - Fatal编程技术网

C++ 为什么if语句不返回无穷大?C++;

C++ 为什么if语句不返回无穷大?C++;,c++,C++,我已经编写了这个程序,它应该打印无穷如果除以0,例如根据代码0/0=无穷,因此输出不是我期望的。我的代码有问题吗?或者我需要用另一种方式 #include <iostream> using namespace std; void optionMenu(); void userInput(char); int main () { optionMenu(); char userResult; cout << " Choice: "

我已经编写了这个程序,它应该打印无穷如果除以0,例如根据代码0/0=无穷,因此输出不是我期望的。我的代码有问题吗?或者我需要用另一种方式

#include <iostream>
using namespace std;

void optionMenu();
void userInput(char);

int main ()
{
    optionMenu();
    char userResult;
    cout << " Choice: ";
    cin >> userResult;
    userInput(userResult);
    return 0;
}
void optionMenu()
{
    cout << " Enter your choice" << '\n' ;
    cout << " + for Addition" << '\n';
    cout << " - for Subtraction" << '\n';
    cout << " * for Multiplication" << '\n';
    cout << " / for Division" << '\n';
}
void userInput(char userResult)
{
    int value1, value2;
    switch (userResult)
    {
        case '+':
        cout << " Enter your first Number: "; cin >> value1; cout << " Enter your second number: "; cin >> value2;
        cout << " The result for the Entered values is equal to: "; cout << (value1 + value2);
        break;

        case '-':
        cout << " Enter your first Number: "; cin >> value1; cout << " Enter your second number: "; cin >> value2;
        cout << " The result for the Entered values is equal to: "; cout << (value1 - value2);
        break;

        case '*':
        cout << " Enter your first Number: "; cin >> value1; cout << " Enter your second number: "; cin >> value2;
        cout << " The result for the Entered values is equal to: "; cout << (value1 * value2);
        break;

        case '/':
            if(!(value2))
                cout << "INFINITY" << '\n';
            else
        cout << " Enter your first Number: "; cin >> value1;
        cout << " Enter your second number: "; cin >> value2;
        cout << " The result for the Entered values is equal to: " << (value1 / value2) << '\n';
        break;

       default:
        cout << " Unidentified or Wrong Input / maybe not allowed! Try again." << '\n';
    }
}
#包括
使用名称空间std;
void optionMenu();
无效用户输入(字符);
int main()
{
选项菜单();
字符用户结果;
cout>userResult;
用户输入(userResult);
返回0;
}
无效选项菜单()
{

cout在未初始化用户输入之前,检查value2是否等于0

这样做:

case '/':
        cout << " Enter your first Number: "; cin >> value1;
        cout << " Enter your second number: "; cin >> value2;
        if(value2)
            cout << " The result for the Entered values is equal to: " << (value1 / value2) << '\n';
        else
            cout << "INFINITY" << '\n';
        break;
案例“/”:
cout>value1;
cout>value2;
如果(值2)

cout这是正常的,因为您在这里使用value2时没有初始化,这是一个运行时故障:

case '/':
        if (!(value2))
            std::cout << "INFINITY" << '\n';
案例“/”:
如果(!(值2))
标准::cout值2;
如果(!(值2))

std::cout
value2
在到达
if(!(value2))时未初始化
line-因此,您的代码调用未定义的行为
else
分支只是
cout问题的关键是您在要求用户输入值之前检查了
value2
的值。您需要在输入值之后输入if,而不是在您的代码绝对无效之前。这是未定义的行为,在输入值时使用value2尚未初始化。@Tahir移动if语句,使其位于
cin>
语句之后。在变量有值之前,您不能检查变量的值。感谢您分享您的知识)
case '/':
        std::cout << " Enter your first Number: "; std::cin >> value1;
        std::cout << " Enter your second number: "; std::cin >> value2;
        if (!(value2))
            std::cout << "INFINITY" << '\n';
        else
            std::cout << " The result for the Entered values is equal to: " << (value1 / value2) << '\n';
        break;