简体   繁体   English

在输出的开头出现“程序以退出代码0结尾”吗?

[英]“Program ended with exit code: 0” appears towards the beginning of my output?

So my code is basically supposed to read a postfix expression and compute the value. 因此,我的代码基本上应该读取后缀表达式并计算值。 This is my code so far and it seems to work except that the "Program ended with exit code: 0" appears towards the beginning of my output every time I run it on Xcode. 到目前为止,这是我的代码,并且似乎可以正常工作,只是每次在Xcode上运行它时,输出的开头都会出现“程序以退出代码结尾:0”。 Is there an error in my code that is causing this? 我的代码中是否存在导致此错误的错误? Also, I'm supposed to only have the user input the value of a variable with the same name once but will always have to enter it twice. 此外,我应该只让用户输入一次具有相同名称的变量的值,但始终必须输入两次。 Is there a way around this that is simple? 有没有解决这个问题的简单方法?

Here is my code 这是我的代码

#include <iostream>
#include <string>
#include <stack>
#include <sstream>

int main()
{

char input = 'y';
while (input == 'y')
{
    std::string expression;
    std::stack<int> myStack;

    std::cout << "Enter a postfix expression with a $ at the end: ";
    std::getline(std::cin,expression);


    int i = 0;


    std::istringstream ss(expression);
    while (ss >> expression)
    {
        if (expression[i] >= 'a' && expression[i] <= 'z')
        {
            int number = 0;
            std::cout << "Enter the value of " << expression << ": ";
            std::cin >> number;
            myStack.push(number);
        }
        else if (expression[i] >= '0' && expression[i] <= '9')
        {
            int number = std::stoi(expression);
            myStack.push(number);
        }
        else
        {

            switch (expression[i]) {
                case '+':
                {
                    int number1 = myStack.top();
                    myStack.pop();
                    int number2 = myStack.top();
                    myStack.pop();

                    int total = number1 + number2;
                    myStack.push(total);
                    break;
                }

                case '-':
                {
                    int number1 = myStack.top();
                    myStack.pop();
                    int number2 = myStack.top();
                    myStack.pop();

                    int total = number1 - number2;
                    myStack.push(total);
                    break;
                }

                case '*':
                {
                    int number1 = myStack.top();
                    myStack.pop();
                    int number2 = myStack.top();
                    myStack.pop();

                    int total = number1 * number2;
                    myStack.push(total);
                    break;
                }

                case '/':
                {
                    int number1 = myStack.top();
                    myStack.pop();
                    int number2 = myStack.top();
                    myStack.pop();

                    int total = number1 / number2;
                    myStack.push(total);
                    break;
                }
            }
        }
    }

    std::cout << "\tFinal value = " << myStack.top() << std::endl;

    std::cout << "Continue(y/n)? ";
    std::cin >> input;

    std::cin.clear();
    std::cin.ignore();


}
return 0;
}

This is the output after entering 'n' to terminate the program 输入“ n”以终止程序后的输出

Xcode版本10.1 beta 3(10O45e)为我解决了此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM