简体   繁体   English

简单的cin cout代码通过“构建和运行”运行,但是来自bin / debug / x.exe的少量工作

[英]Simple cin cout Code runs by “build and run” but dosent work from bin/debug/x.exe

I'm new to coding. 我是编码新手。 I just made this simple code by using codeblocks. 我只是通过使用代码块制作了这个简单的代码。 It works perfectly from the "build and run" option but when running the ".exe" file, it closes instead of working when a value is entered. 它可以从“构建并运行”选项中完美地工作,但是在运行“ .exe”文件时,它会关闭而不是在输入值时不起作用。

I have reinstalled codeblocks two times, but still it isn't working. 我已经两次安装了代码块,但仍然无法正常工作。

#include <iostream>

using namespace std;

int main()
{
  int no;
  cout << "Type the number u need the square of" << endl;
  cin >> no;
  cout << " The square of " << no << " is " << no*no << endl;

  return 0;
}

You can make the application wait till your input before it closes. 您可以让应用程序等到输入关闭为止。 Add two lines at the end of your code: 在代码末尾添加两行:

std::cout << "Press enter to quit.\n";
std::cin.ignore();

If you run it directly by clicking on the .exe, then the program will close as soon as it's finished. 如果您通过单击.exe直接运行它,则程序将在完成后立即关闭。 This closes the terminal window too. 这也将关闭终端窗口。

If you run the program directly from the terminal, this won't happen (so start it up inside the terminal , instead of clicking on the .exe). 如果您直接从终端运行程序,则不会发生(因此请在终端内部启动它,而不是单击.exe)。

Alternatively you can have it wait for user input after printing the value but before exiting. 或者,您可以让它在打印值之后但退出之前等待用户输入。 This will give you a chance to see what the value is. 这将使您有机会了解其价值。

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

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