简体   繁体   中英

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.

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. 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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