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?
printf("Please enter month number: ")withprintf("Please enter month number:\n"). It this works, then it's a line buffering issue. If not, the problem is within your environment.\ndoesn't fix it. Definitely a problem with my environment.externalConsoleto false,printfworks fine butscanfdoesn't apparently because VSCode's debug console cant take in user inputs. So I setexternalConsoleto true, and when I do this nothing works.