I recently started learning C and have just started using VSC. I was previously using CodeBlocks. While testing out a simple array program, I realised that there is a difference in output. For reference, the array program code is shown below.
#include <stdio.h>
int main() {
int grades[10];
int count = 10;
long sum;
float average;
printf("\nPlease enter the 10 grades: \n");
//printf("%i", count);
for(int i = 0; i < count; i++) {
printf("%2u> ", i+1);
scanf("%d", &grades[i]);
sum += grades[i];
};
average = (float)sum/count;
printf("The average of the 10 grades are %.2f", average);
return 0;
}
I am able to run the for loop but the code after the for loop does not appear in the CMD like terminal and the program just quits after the for loop. However, the code after the for loop does appear in the VSC terminal. I am able to enter the ten values.
The VSC Terminal Output:
However, the CMD looking terminal did not manage to print the code after the for loop and instead exits the program.
Could it be because of this error: "The preLaunchTask 'C/C++:gcc.exe build active file' terminated with exit code -1." everytime I click 'Run > Start Debugging'.
Sorry for this messy format but thank you in advance for helping!
Related
I was instructed to write a game of dice in C language in which the computer and the user act as competing sides of the game. First, a random number is generated by the computer, and then the string "g" command input by the user is accepted to generate the user's random number (simulate to roll a dice once), and compare their values. If the random number obtained by the user is less than that obtained by the computer, output "Sorry, you loss!", if the results is otherwise, output "Congratulations, you won!".
The code:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand((unsigned)time(NULL));
int com_inp = (rand() % (6 - 1 + 1)) + 1;
int user_inp=0;
char ch;
scanf("%c",&ch);
if(ch=='g')
user_inp = (rand() % (6 - 1 + 1)) + 1;
if(user_inp<com_inp)
printf("\n Sorry, you lost!");
else
printf("\n Congratulations, you won!");
printf("\n Computer dice = %d \t User dice = %d",com_inp,user_inp);
return 0;
}
After running on Xcode v12.2, no output is shown. The console is blank.
However, the output is shown if the program is run on Dev-C++(Windows).
Any help would be appreciated.
I don't see anything printed before the scanf. I am not familiar with Dev-C++ on Windows, but in the Xcode console there is no prompt to show you that it is waiting for input, so you might want to put something like:
printf("Enter a number between 1 and 6\n>");
before your scanf.
You may also be running into some line-buffering issues. Try adding \n at then end of each line you are printing.
if(user_inp<com_inp)
printf("\n Sorry, you lost!\n");
else
printf("\n Congratulations, you won!\n");
printf("\n Computer dice = %d \t User dice = %d\n",com_inp,user_inp);
I can't properly run/debug my code in VS Code using the C language. I've installed C/C++ package on VSC, Mingw & applied the path for Mingw. All my files are running .c format as well.
Only the last part of my code keeps crashing in VSC, when I run this same code on website compilers, it works!
Here is my code:
#include <stdio.h>
int main(void) {
int num1;
int num2;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Answer: %d ", num1 + num2);
return 0;
}
That last printf is where VSC just shuts down the output window, so I never get to see the end result of my code. Anyone have any solutions to fix this? It'd be greatly appreciated!
When you run your console program from Visual Studio, it opens a terminal window, runs the program and the terminal window closes automatically when the program exits. This is a classic problem with the Microsoft Windows platform that they do not seem to care about despite millions of newbie programmers like you experiencing the same problem.
If you open the terminal window yourself, by running the CMD command from the start menu, you will be able to run your program manually after changing the current directory to that of the program binary.
To prevent the terminal window from closing immediately when running directly from Visual Studio, you should add 2 getchar(); statements before returning from main() to wait for user input and get a chance to see the output. Just reading a single byte with getchar() will not suffice because it will just read the pending newline entered by the user in response to the second prompt.
Also note that it is preferable to output a trailing newline to ensure the output is properly flushed on some legacy systems:
printf("Answer: %d\n", num1 + num2);
Here is a modified program you can test:
#include <stdio.h>
int main(void) {
int num1 = 0, num2 = 0;
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Answer: %d\n", num1 + num2);
getchar(); // read the pending newline
getchar(); // read at least another byte from the user.
return 0;
}
run you program from the console:
In the search box type cmd
cd \path_to_your_executable
run your program.
I'm new to the programming world, recently I began my programming path with C, and because of that I made a program that determines if a number is whether perfect or not. I use Code::Blocks IDE, and it works just fine, the problem is when I click the option "Build and run", the IDE executes the program and works perfectly, but when I select the .exe file from my desktop, it opens up, but doesn't show any output, the window just closes suddenly. Does someone have any idea on how to solve this issue?
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main() {
int N;
int j;
int Sum = 0;
printf("Input a number.\n");
scanf("%d",&N);
for (j = 1; j < N; j++)
{
if (N%j==0)
{
Sum+=j;
}
}
if (Sum==N)
{
printf("The number is perfect.\n");
} else {
printf("The number is not perfect.\n");
}
return 0;
}
Running program with Code::Blocks Build and run option
The only part of the Desktop located .exe that I can reach
If someone can suggest a solution, I will be very thankful!
As it was said, the program exits immediatly after it's completion. If you want to run the program, executing it by double click, you can place a pause condition in the program, for instance if you put getchar(); just before the return 0; statement, it will only exit after it receives an input from the keyboard, that is after you enter a key.
Is there some way to run C programs interactively through Notepad++'s console where prompts for input would appear? I can't seem to get it work as expected. I can demonstrate with this bit of code-
#include <stdio.h>
int main()
{
int number, i;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",i);
}
}
return 0;
}
When I run this through the NppExec plugin with this script-
NPP_CONSOLE 1
E:\Documents\Notepad++Portable\tcc\tcc.exe -run $(FULL_CURRENT_PATH)
The result is I get no prompt for input. But if I go ahead and enter it anyway, the program proceeds - prints the prompt but also completes the run as seen here.
E:\Documents\Notepad++Portable\tcc\tcc.exe -run E:\Documents\Programs_C\Factors.c
Process started >>>
60
Enter a positive integer: Factors of 60 are: 1 2 3 4 5 6 10 12 15 20 30 60 <<< Process finished. (Exit code 0)
================ READY ================
I can get it to work just fine through a cmd window which I can call if I make this small change-
npp_run cmd /k E:\Documents\Notepad++Portable\tcc\tcc.exe -run $(FULL_CURRENT_PATH)
Just curious if it is at all possible to get this to work through the console which would make testing small bits of code much more convenient.
In the following simple program after the user inputs an integer the command line remains a flashing prompt. When I exit the program the answer is then printed out. Why is this and how can I fix it?
//powers of 2
#include <stdio.h>
int main(void){
int a,b=1,i;
printf("What power of 2?\n");
scanf("%i\n",&a);
for (i=0; i<a;i++)
{
b=b*2;
}
printf("the answer is: %i\n",b);
return 0;
}
Try remove the \n in your scan :
scanf("%i",&a);
Remove \n from scanf. After that I compiled your program and it worked properly.