Reputation: 326
I am learning the C language on macOS using the VS Code IDE.
Below is the C code I am trying to run.
#include <stdio.h>
int main(void)
{
int number_from_user;
/* Get number from user */
printf("Please enter month number: ");
scanf("%d", &number_from_user);
/* Print month name */
switch (number_from_user)
{
case 1:
printf("January");
break;
case 2:
printf("February");
break;
case 3:
printf("March");
break;
case 4:
printf("April");
break;
case 5:
printf("May");
break;
case 6:
printf("June");
break;
case 7:
printf("July");
break;
case 8:
printf("August");
break;
case 9:
printf("September");
break;
case 10:
printf("October");
break;
case 11:
printf("November");
break;
case 12:
printf("December");
break;
default:
printf("Not a month");
printf("Please run the program again");
break;
}
}
Below is the launch.json file.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
"preLaunchTask": "C/C++: gcc build active file"
}
]
}
The problem that I am facing is, to make the scanf
function to work, I need to set externalConsole
to true. Unfortunately, when I run the code, it does open the terminal but it opens a blank terminal and doesn't actually run the code.
What am I doing wrong and how do I fix this?
Upvotes: 1
Views: 2324
Reputation: 71
I installed the plugin code-Runner, normally if you get the gcc already installed and working would be working at the moment you install it.
https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner
You just click the play (or run) button, it will opens automatically a console inside the VS-Code and run the actual program.
If the problem is only with scanf
persists, you can try this
Go to File > Preferences -> User Settings and add custom settings:
{
"code-runner.runInTerminal": true
}
Here's the source of the explanation: https://github.com/formulahendry/vscode-code-runner/issues/72
Ps: Be careful, only accepts single file programs, in the beginning is useful, but maybe later would change to others more complex.
Upvotes: 1