C Programs compiled and run through NPP_Exec console on Notepad++ - c

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.

Related

Difference in output between Command Prompt looking terminal and VSC Terminal

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!

Xcode not showing output

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

My Visual Studio Code won't run my code properly

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.

While running my code in MPI, the root process never executes despite following the correct syntax

While trying to execute my code written in C using the MPI Library, I encountered something very odd happening.
My code generates no syntax error yet when I try
mpirun -n 3 ./q4
I get this
hello
hello
hello
from the other side.
from the other side.
from the other side.
It seems to never enter the rank 0 process. I have no idea why this is happening. This code is structurally identical to some other samples that I have writtten (I can provide the entire code if need be)
However, if I were to type in any two random things after the sixth line, I get this
1213
123
Enter a length for the string that is divisible by the number of processes Number of vowels 27
I don't really know what to do to fix it except that I checked my code for logically errors, which there are none and even if they are, they are much later which means that at least the the code under the first if case should execute.
int main(int argc, char * argv[])
{
printf("hello\n");
int rank,m,size,num;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
printf("from the other side.\n");
char str[100];
if (rank == 0 )
{
printf("Enter a length for the string that is divisible by the number of processes ");
scanf("%d",&m);
scanf("%s",str);
}
.
.
In case it is relevant, I am running Ubuntu 18.04.
You need to add some fflush(stdout); after the last printf to force the text refresh.
When there is no \n in printf text, the text is not displayed immediately.
So you should write:
printf("Enter a length for the string that is divisible by the number of processes ");
fflush(stdout);
scanf("%d",&m);
....
Or simpler:
puts("Enter a length for the string that is divisible by the number of processes ");
scanf("%d",&m);
....
The puts is to print a message on a line (it creates a new line). And it's not buffered.
In case anyone ever reads this in the future, this is to add to what Matthieu said.
The code should be like this
if (rank == 0 )
{
printf("Enter a length for the string that is divisible by the number of processes \n");
fflush(stdout);
.
.
.

Eclipse cdt no output on console; MinGW Path is already set

I'm sure you guys have gotten sick of seeing this question over and over again by now but none of the solutions displayed in the other threads have worked for me. I've tried setting the C:\MinGW Path variable, the linker flags, nothing works.
The only thing that has worked is adding:
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
to the code but I really do not want to keep writing this in every single program I make.
The console does show the red "Terminate" button as active so that at least means that the program is running but just not being displayed in the console.
To those not familiar with this bug; I wrote a simple C program in eclipse:
#include <stdio.h>
int main()
{
int n;
printf("Enter number: ");
scanf("%d", &n);
for(int i=0; i<=n; i++) {
printf("%d\n", i);
}
return 0;
}
When I run it the console looks like this:
link
And output is:
5
Enter number: 0
1
2
3
4
5
Any ideas?
Also I'm running Windows 10, maybe that makes a difference?

Resources