My command prompt wont run the whole program - c

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.

Related

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.

C console output hidden while awaiting input

Hi I'm trying some C coding on Eclipse, and I had a problem:
I am trying to print some text, however, while awaiting input from the user, the text actually fails to appear until AFTER the user has input.
Here is an example of what I mean:
TEST
#include <stdio.h>
#include <stdlib.h>
int main(void){
char c[5];
printf("test\n"); //PRINTING 'test' BEFORE i have to enter code
fgets(c, 5, stdin);
printf("You entered: %s\n", c);
return 0;
}
OUTPUT:
dog (this is what i typed)
test
You entered: dog
Rather than appearing BEFORE I am prompted to enter code, the "test" printf only appears AFTER I have entered the code.
Probably this text to print is still waiting in the buffer (that's an optimization, to group data to write to make it more efficient). To make sure everything from buffer gets out to the console you should flush it like this
fflush(stdout);
or you can use a function that does not use buffering like (on linux)
write()

Easy C: Program stuck after asking for prompt

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.

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

Wrong output when I run code in Eclipse Indigo CDT

I am using Eclipse Indigo CDT and just running simple code which is:
#include <stdio.h>
void main()
{
int num;
printf("enter no\n");
scanf("%d",&num);
printf("no is %d\n",num);
}
Opuput:
55
enter no
no is 55
But when I run this code it won't print enter no. Instead of that it waits to enter the number. After pressing some number it is printing enter no. What could be the reason?
That would depend on the flush scheme of standard out.
Traditionally, stdout is line buffered when connected to a terminal and page buffered when it's not connected to a terminal.
Apparantly, when you run your program in eclipse, standard out is page buffered.
You can overcome this by flushing stdout:
#include <stdio.h>
void main()
{
int num;
printf("enter no\n");
fflush(stdout);
scanf("%d",&num);
printf("no is %d\n",num);
}
It is good practice to flush a file handle whenever you expect the recipient of the information to respond. That way you can be sure that the recipient gets everything you have written regardless of the buffering of the file handle.

Resources