C++ 程序在计算结果之前终止

C++ program terminating before the result computed

这个程序需要 2 个整数(ab)并且应该打印他们的产品。 它终止而不打印最后一行,但是:

问题:是什么导致程序没有给出您想要的结果并提前关闭? 这条线有问题吗?

        std::cout << "Product of entered numbers = " << c << std::endl;
// first program, printing chars and multiplying 2 integers

#include <iostream>

int main()
{
    printf("Characters: %c %c \n", 'a', 65);
    printf("Decimals: %d %ld\n", 1977, 650000L);
    printf("Preceding with blanks: %10d \n", 1977);
    printf("Preceding with zeros: %010d \n", 1977);
    printf("Some different radices: %d %x %o %#x %#o \n", 100, 100, 100, 100, 100);
    printf("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416, 3.1416);
    printf("Width trick: %*d \n", 5, 10);
    printf("%s \n", "A string");
    int a, b, c;

    std::cout << "Enter two numbers to multiply\n";
    std::cin >> a >> b;

    c = a*b;
    std::cout << "Product of entered numbers = " << c << std::endl;

    return 0;
}

正如其他人在评论中所说,您可能只是没有看到程序退出前的最后一行输出,因为没有什么可以保持程序 运行 和 window 打开打印出来后。

尝试在 return 语句之前添加这样的内容:

std::cout << "Press any key to exit.";
char dummy;
std::cin >> dummy;