Just started learning C and I have a problem with the scanf function. Every time I enter a number in the console, it will be printed right under the input. The program still works, but it is a little bit annoying.
(I am using CLion from JetBrains)
int main()
{
int x;
printf("Number: ");
scanf("%d", &x);
printf("Your number is %d!", x);
}
This is the output:
Number:15
15
Your number is 15!
Process finished with exit code 0
It is an issue in clion (Why is CLion printing back inputs from standard input?). Currently unresolved. This problem exist for C and C++.
This bug resides for four years. I definitely advice you to change your compiler if you are not bound this for a particular reason.
Related
This question already has an answer here:
printf not print on the console in eclipse?
(1 answer)
Closed 12 months ago.
So I just installed eclipse on windows after having too many problems with my old installation on a vm running Ubuntu.
All problems gone except for what I wrote in the title. Scanf text never comes up first, no matter which program I'm running. It shows text when I have entered all that I can enter. Let me give you an example:
#include <stdio.h>
#include <stdlib.h>
int main() {
int number1;
int number2;
printf ("Type in a number \n");
scanf ("%d", &number1);
printf ("Type in a number \n");
scanf ("%d", &number2);
int number3 = number1 * number2;
printf ("%d", number3);
return 0;
}
Console looks like this:
2
3
Type in a number
Type in a number
6
2 and 3 I had to type in first, then the last three lines showed up. That's not how it's supposed to be is it?
Thanks for any help!
What seems to work is writing
setbuf(stdout, NULL);
into every main of every program. It does fix the problem, yet there's maybe another fix that's not so annoying as doing that everytime. It's not bad, but I don't get why it simply worked on another instance of eclipse but here not.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
printf("enter the 2 numbers: ");
scanf("%d %d",&a,&b);
c=a+b;
printf("the sum is : %d ",c);
return(0);
}
this is a simple program to add 2 numbers.
my program would let me input the value..but it would not print the sum ,nor would it print the next line.
it would run till the scanf() and as i press enter, it would jst exit the program.
can you please tell me whats wrong. I am a beginner programmer...
There are two things you should think of here.
End printouts with a newline character, because stdout is often line buffered. Do printf("the sum is : %d \n",c); instead. Or call fflush(stdout); expliticly after the printout. This will ensure everything gets printed.
Add some input code in the end. Like an extra scanf("%d", &a); This is basically a small hack to prevent the window from closing before you can see the final output. Another alternative is to add sleep(3); to sleep for 3 seconds. A third alternative here is to see if there are some settings that controls the closing of the window in your IDE.
Your program works correctly, but it exits right after printing the output, giving you no time to look at it.
Consider adding some input before return(0);, such as 2 getchar(); calls. You need 2, because the first character read will be the \n that you typed after the numbers.
I'm new to programming in C and I'm trying to build and execute my first programs. My first program was Hello, World printed in the CMD like many of you. It worked great. Now it's onto bigger and better projects though and I'm having a weird issue.
I'm trying to make a basic addition calculator using standard IO operations (scanf and printf) and it won't function properly.
My program asks for 2 numbers to be input by the user and it will then display the output of said calculation. The program executes flawlessly until the scanf expression comes into play. After I input my 2 numbers to be added the CMD prompt just closes without warning and never spits out an answer or the text I have to be displayed afterward.
I tried multiple solutions to fix this problem including copying and pasting the source code directly from the website I was learning from, even their perfect code nets the same outcome..just a crash before an output is displayed. I'm posting today because I'm wondering where my issue is coming from because I'm just not sure why this program won't execute as it's supposed to. Thanks in advance and heres the code I'm working with:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
c = a + b;
printf("Sum of the numbers = %d\n", c);
return 0;
}
Once console application returns from main method, the associated console window closes automatically. I assume you are using Windows OS. In that case add a system("pause"); before your return 0; statement.
For platform independent solution you can just show a prompt to user and wait for a key press before returning from main. As #chux pointed out in comment any character remaining in input buffer (enter from scanf in this case) must be cleared.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
c = a + b;
printf("Sum of the numbers = %d\n", c);
//clear input buffer
int d;
while ((d = getchar()) != '\n' && d != EOF) { }
printf("Press ENTER key to Continue\n");
getchar();
return 0;
}
CMD prompt just closes without warning and never spits out an answer
Seems like you are opening a new terminal on Windows machine. I compiled the code and it works. Your program closes just after printing the answer so you simply cannot see it. Stop it artificially before the end. To prove it add the following line directly before return:
scanf("%c", &a);
This will result in stopping the program and waiting for input. You will need to enter another number which will essentially be ignored but will stop the program so you can see the output. This is not a good target solution though due to you need to enter some characters to go on and exit but it proves the point :)
I have tried both Turbo C and Borland C. No difference.
The programme is as follows:
#include<stdio.h>
#include<conio.h>
int main()
{
int c,y;
float r,si;
printf("Enter the value of c,y,r");
scanf("%d%d%f",&c,&y,&r);
si=c*y*r/100;
printf("%f\n",si);
getch();
}
When I compile and run this no matter whatever value I choose, the answer is always 0.
A quick reply will be highly appreciated.
I just ran your program on g++. I think your just doing the input wrong.
arvvvs#UHome-K53E:~/Documents/StackOverflowHelp$ ./interest
Enter the value of c,y,r1 2 3
0.060000
arvvvs#UHome-K53E:~/Documents/StackOverflowHelp$ ./interest
Enter the value of c,y,r1, 2, 3
0.000000
(I did remove the conio.h and getch line.)
If you want to have input with commas,
change from:
scanf("%d%d%f",&c,&y,&r);
to:
scanf("%d, %d, %f",&c,&y,&r);
(Or, even better, test that the return value from scanf() is 3. If it is not, then there is a data entry problem.)
change your scanf statement
scanf("%d%d%f",&c,&y,&r);
to
scanf("%d %d %f",&c,&y,&r);
#include <stdio.h>
#include <math.h>
int main (void)
{
float inches;
printf("Enter the number of inches\n");
scanf("%f\n",&inches);
float feet;
float cm;
float yards;
float meter;
feet = 12 * inches;
cm = 2.54 * inches;
yards = 36 * inches;
meter = 39.37 * inches;
printf("Amount in feet: %f\n", &feet);
printf("Amount in cm: %f\n", &cm);
printf("Amount in yards: %f\n", &yards);
printf("Amount in meters: %f\n", &meter);
getchar();
return 0;
}
I'm using Dev c++
Is the problem i'm problem I'm working on in C. Basically enter in a number in inches then print amount in cm,yards,meters and feet. This is giving me 0.0000 or something for all of them or actually the time it is up. I can't keep the screen up and I thought that was the purpose of getchar() but I must have been mistaken. Any help is great. Thanks!
EDIT 1
What about as far as keeping dev c++ on the screen instead of closing out after I put stuff in? I am also having to put 2 values in before it returns in anything when the screen pops up? Why??
Two problems:
The usual problem with using scanf(), in that it leaves the newline after the number unread and the following read operation (the getchar() here) reads it.
You shouldn't pass pointers to printf(), but the actual values.
You're trying to print the addresses of your floats as floats, you just want to say this:
printf("Amount in feet: %f\n", feet);
Note the lack of an address (&) operator on feet. You want to apply similar changes to your other printf calls.
With printf, you don't give it the address of the float values, you just give it the values. Remove the &s from the printf calls.
You need the address in scanf because the function modifies the variables that you pass in, but printf just needs the values. As it is, the printf is essentially reinterpreting the pointers as floats, which is why you get the garbage values displayed.
About I can't keep the screen up, it's a common problem to everyone trying to execute a console program in a graphical environment directly from the IDE, in particular Dev-C++. The problem is that there's no console for I/O, then one is provided but just for the time the program is running, and since programs are fast, if you do not add a pause after your last input and output, you won't have the time to read the output.
Many MS Windows Dev-C++ users add an horrorific system("pause"). I always suggest that, if Dev-C++ is not able to provide a console for I/O with the option "keep it opened even after the program ends", then it is better you open a shell (cmd or powershell on windows) and run your program directly from there.
About the input problem, unluckly scanf-ing has several buffering problem since the input that is not recognized for the given format is not discarded and is ready for the next reading. E.g.
scanf("%f", &aFloat);
scanf("%f", &theNextFloat); // in your case you have the extra getchar();
won't stop for the second scanf if you write 1.25 4.5 as first input, since the 4.5 is already available for the next scanf. In your case it was a newline that was left in the buffer and since getchar has found it, it does not need to wait for input. You could use a
while( getchar() != EOF ) ; instead, and then to exit you need to hit Ctrl-D.