Getting the prompt from the remote terminal over ssh - c

I have a simple program test which gets a number as input and prints the same on console.
#include<stdio.h>
int main(void)
{
int i;
printf("Test Pgm \n");
printf("Enter a no:");
scanf("%d",&i);
printf("No Inputted:%d \n",i);
return 0;
}
//The above program lies on 10.220.5.xx (different machine)
##gcc -o test test.c
On invoking the test pgm frm another machine over ssh , I don't get any prompt on the machine where im executing.
$ ssh user#10.220.3.xx '/home/user/test'
user#10.220.3.xx's password
After entering the password i don't get see anything not even 'Test Pgm'. How do i get the prompt remotely and input the values?

Try adding fflush(stdout); before the scanf().
Also, you must check the return value of scanf(), it can fail to convert the input if given non-numerical text:
fflush(stdout);
if(scanf("%d", &i) == 1)
printf("Number input: %d\n", i);

Related

My command prompt wont run the whole program

This code runs perfectly in my IDE; I even tried it in an online compiler just to be sure, but when I try to open the .exe it will only ask for the integer and automatically close. I tried it with another program from the school where I asked for like 15 numbers but right before a goodbye message it just closes. Any idea how to fix cmd?
#include <stdio.h>
int main()
{
int numberOfWidgets;
printf("Give me a number: ");
scanf("%d", &numberOfWidgets);
printf("You choose the number: %d", numberOfWidgets);
return 0;
}
You need to add two lines to the end of your program:
int main() {
...
getchar();
getchar();
return 0;
}
The first call to getchar will clear the return key you pressed when you entered the number, the second one will stop and wait for a key to be pressed.
That way, your program will not exit until you press a key, and you will be able to read the output.

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.

Why does running C code in Vim skip scanf()?

I'm using neovim in arch linux with the gcc C compiler, this is what I use in my .vimrc to compile and run
map <F5> :w <CR> :!gcc % -o %< && ./%< <CR>
The issue is that my code will run fine but any scanf() functions won't prompt an input and will be ignored as the program will runs. Even after compiling with vim then running in a separate zsh terminal it will allow me to enter the values when running the code with ./x
I apologise in advance, I'm new to vim and wanted to use this to speed up my workflow.
The following code exhibits the issue:
#include <stdio.h>
int main()
{
char Team1[20];
char Team2[20];
int team1Score, team2Score;
printf("Please enter the name of team one: ");
scanf("%s", Team1);
printf("Please enter the name of team two: ");
scanf("%s", Team2);
printf("Please enter the score for %s: ", Team1);
scanf("%d", & team1Score);
printf("Please enter the score for %s: ", Team2);
scanf("%d", & team2Score);
if (team1Score > team2Score)
{
printf("%s scores 3 points and %s scores 0 points", Team1, Team2 );
}
else
if (team1Score < team2Score)
{
printf("%s scores 3 points and %s scores 0 points", Team2, Team1 );
}
else
{
printf("Both %s and %s score 1 point", Team1, Team2);
}
return 0;
}
The fault is probably not in your program, but the way vim executes it. If you check the documentation of :! command then you can see the following:
The command runs in a non-interactive shell connected to a pipe (not a
terminal).
Non-interactive shell means a shell, which does not allow user commands to be entered. Your program will not read scanf input from the terminal, but from the pipe which was created by vim.
If you are using a recent version of vim (8.0 or later, if I'm right) or neovim then you can use the :term command to open a terminal. In that terminal you will be able to enter user input.

why is printf not called when expected?

This is my Code [Note: I am using Eclipse for C/C++ on Windows Platform]
#include <stdio.h>
#include<stdlib.h>
int main(void) {
int num;
printf("Enter a number:\n");
scanf("%d",&num);
if(num%2==0)
printf("Number is Even");
else
printf("Number is Odd");
return EXIT_SUCCESS;
}
Here I have to enter an Integer first only then printf is called... I want to call printf first before I enter an Integer...What am I doing wrong here?
for example this is the output that I get
6
Enter a number:
Number is Even
and expected output is
Enter a number:
6
Number is Even
you can call fflush(stdout) after first printf to print the buffered output. But considering in future if you extend the program with more printfs then adding fflush after every printf will be an overhead. So you can add
setbuf(stdout, NULL)
just before all the printfs.
This will make sure no output is buffered and you will see the prints instantaneously.

Problems with scanf and or fgets program cuttoff

I am trying to write a code that reads in a integer using scanf and then asks the user if they would like to enter another by entering "yes" or "no". I get no errors when I compile but when I run the program it asks for the number, displays it as it should, ask to continue and then promptly closes without taking any input.
int main()
{
int *data=malloc(1*sizeof(int));
if(data==NULL)
{
printf("out of memory");
exit(1);
}
char *yc="yes";
char uinput[20];
int numinput;
printf("enter a number \n");
scanf("%d",&numinput);
data[0]=numinput;
printf("you entered %d \n",numinput);
printf("would you like to enter another # ?");
fgets(uinput,sizeof(uinput),stdin);
int count=0;
int n,i;
int comp=strncmp(uinput,yc,1);
while(comp==0)
{
n=sizeof(data)/sizeof(data[0]);
count=count+1;
data=(int*)realloc(data,n+1);
printf("enter another number:\n");
scanf("%d",&numinput);
data[n+1]=numinput;
printf("the numbers are:\n");
for(i=0;i<=count;i++)
{
printf("%d\n",data[i]);
}
printf("would you like to enter another # ?");
fgets(uinput,sizeof(uinput),stdin);
}
printf("Goodbye!\n");
return(0);
}
This is what happens when I run the program
./vector_play.out
enter a number
6
you entered 6
would you like to enter another # ?Goodbye!
I am not sure why it is immediately exiting any help would be great.
Your problem stems from the fact that after numinput has been read using the code in
scanf("%d",&numinput);
the newline character is still left in the input stream. The call to fgets reads the newline as a complete line and returns without waiting for any further input.
You need to add a line to ignore the rest of the input stream. You can use fgets for that purpose too.
scanf("%d",&numinput);
fgets(uinput,sizeof(uinput),stdin); // Read and ignore
printf("you entered %d \n",numinput);
printf("would you like to enter another # ?");
fgets(uinput,sizeof(uinput),stdin);

Resources