C++文件不给出任何输出,可能与Windows相关的问题有关

C++ file doesn't give any output, possibly related to Windows-related problem

提问人:gluon 提问时间:10/30/2023 最后编辑:user4581301gluon 更新时间:10/31/2023 访问量:63

问:

我编写了以下简单的代码来习惯使用C++,但是它不能在我的计算机上使用,即使它适用于在线编译器 https://www.programiz.com/cpp-programming/online-compiler/

#include <iostream>

int main() {

    int x, y;
    std::cout << "Type a number: ";
    std::cin >> x;
    std::cout << "Type another number: ";
    std::cin >> y;
    int sum = x + y;
    std::cout << "Sum is: " << sum;

    return 0;
}

我尝试在计算机终端上使用 g++ 文件名.cpp运行它,并在 VSCode 终端上运行它。我期望的是,我得到输出 Type a number: 打印在终端上,然后等待输入,然后键入另一个 number:,再次等待输入;然后打印两个数字的总和。g++ filename.cpp

然而,我得到的是以下内容:

PS C:\Users\49155\OneDrive\Masaüstü> g++ filename.cpp
PS C:\Users\49155\OneDrive\Masaüstü>

在两个航站楼。它不打印任何东西,不等待任何输入,不做任何事情。该文件在存储库中,因此我认为问题与路径无关。 我使用的是 Windows,由于代码在在线编译器上运行,因此我认为问题与代码无关,而是与计算机有关。我已经在 VSCode 中拥有 C++ 扩展,为了检查 C++ 版本,我在终端中进行了检查,结果如下:\Masaüstüg++ -v

PS C:\Users\49155\OneDrive\Masaüstü> g++ -v
Using built-in specs.
COLLECT_GCC=C:\msys64\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-12.2.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/include --libexecdir=/mingw64/lib --enable-bootstrap --enable-checking=release --with-arch=nocona --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++,jit --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts --enable-libstdcxx-time --disable-libstdcxx-pch --enable-lto --enable-libgomp --disable-multilib --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev10, Built by MSYS2 project' --with-bugurl=https://github.com/msys2/MINGW-packages/issues --with-gnu-as --with-gnu-ld --disable-libstdcxx-debug --with-boot-ldflags=-static-libstdc++ --with-stage1-ldflags=-static-libstdc++
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (Rev10, Built by MSYS2 project)

有谁知道为什么我不能运行它?

C++ Windows CIN中

评论

5赞 Pete Kirkham 10/30/2023
g++ 命令是编译器,而不是解释器。它将创建一个可执行文件,您可以在Windows上运行IIRC a.exe。查看它创建的文件并尝试运行该文件。
0赞 Sam Varshavchik 10/30/2023
你使用哪本教科书或参考资料来学习 C++,它是否向你解释了 C++ 是如何工作的,什么是 C++ 源代码,什么是 C++ 编译器,它是如何工作的,以及如何使用它?
0赞 molbdnilo 10/30/2023
任何一本像样的书都应该在第一章中解释编译器的作用以及如何执行程序。
0赞 Thomas Matthews 10/31/2023
您可能希望在从 返回之前暂停执行。程序完成后,某些控制台将消失。main()
0赞 gluon 10/31/2023
@SamVarshavchik我正在上一门编程课程,但我们没有涵盖这些,我们直接开始编写代码......

答: 暂无答案