1

Recently started learning c++ and wanted to try to use inputs. Just made a very basic program that tells you the number of weeks in a given number of days with remaining days included. Whenever I run this program in Visual Studio Code it runs the code but it waits for a input with not way to input. Tried to build this program with g++ but the ending .exe closes out when pressing enter. Nothing is wrong with the code itself since I tried it in a online compiler and it worked perfectly. Also ran it through navigating to it through command prompt with cd and that also worked. Is there anyway visual studio code can accept inputs? and is there anyway to fix command prompt closing when pressing enter when opening built .exes from the desktop?

#include <iostream>
using namespace std;
int main () {
int days;
int weeks;
cin >> days;
weeks = days / 7;
cout << "There are/is " << weeks << " weeks for every " << days << " days " << endl;
cout << "There is " << days % 7 << " remaining days" << endl;
}
  • Some problem in your `launch.json`? – drescherjm Jun 17 '20 at 20:46
  • looked it up and there was something wrong in the debugger path but I fixed that before building – Sterben3254 Jun 17 '20 at 20:49
  • Does this answer your question? [How to stop C++ console application from exiting immediately?](https://stackoverflow.com/questions/2529617/how-to-stop-c-console-application-from-exiting-immediately) – eesiraed Jun 17 '20 at 21:57

1 Answers1

0

use the integrated terminal in visual studio code. you can toggle it with ctrl+ù. then compile with g++ and run the output. typically, it looks like this : g++ main.cpp -o out.exe; .\out.exe .

to prevent the terminal from closing, you can add a cin.get() at the end of the cpp file

Midnight Exigent
  • 615
  • 4
  • 15