This question already has answers here:
"printf" only printing variable addresses
(2 answers)
Closed 3 years ago.
So I'm using C for a few days and I didn't have this problem before but now I have a problem with C scanning different number than the one the user enter. I feel like it prints the location of the number but not the number itself. The number I get every time is 6422076 and if I print another number that I've scan from the user it just show the same number -4, 6422072 so I'm pretty sure It has to do with the location the computer storage the numbers.
I tried to print it with a few other ways and always get the same weird number.
void measures()
{
int height;
printf("\nEnter your height:\n");
scanf("%d",&height);
while(height<140 || height>210){
printf("Invalid input, try again: \n");
scanf("%d",&height);
}
printf("height: %d\n",&height);
}
not getting any errors
Here's your problem:
printf("height: %d\n",&height);
You're not printing the value of height. You're printing its address. Remove the address-of operator:
printf("height: %d\n",height);
Related
This question already has answers here:
How to scanf only integer?
(9 answers)
Closed 1 year ago.
I am trying to run a program whereby user can only enter 1 or 2. If the user enters any other numbers or alphabets, it should prompt the user back to entering input again. I have tried the following code and it works if user enter an integer which is not 1 or 2, but the whole program will go haywire if entered any alphabets. I tried to validate with
if == 0 but doesnt work as well. Does anyone have any idea?
int getUserInput(){
printf("1: You have chosen 1.\n");
printf("2: You have chosen 2.\n\n");
printf("Enter an option: ");
scanf("%d", &userSel);
if(userSel!= 1 && userSel!= 2){
printf("You have entered an invalid input. Please try again.\n");
getUserInput();
}
else{
printf("Enter a number: ");
scanf("%d", &userNo);
}
printf("userSelection: %d\n", userSel);
printf("1userNumber: %d\n", userNo);
return 0;
}
Perhaps you can make it so scanf sets the variable that will hold the user input only when the input is a integer. You can use this stack overflow link here that has a similar question to yours!
How to scanf only integer?
This question already has answers here:
C programming - Loop until user inputs number scanf
(4 answers)
Closed 4 years ago.
edit1:
Sorry, I am in an intro to programming class... I read the two links, but don't fully understand them...
I tried to to add
if (scanf_s("%d", &input) == 0) {
fflush(stdin);
continue;
}
but that didn't seem to do anything. I understand that scanf is in an error state, so it will refuse to run, and that I need to clear a buffer of the bad input from user.
so basically, I am looking for a beginner's solution, for someone who just learned about loops, input/output, and basic error check.
~~~~~~~~~
I am new to C and have a small question. I want to read a list of integers from the console. It works fine if the user does indeed input integers, but if it types something like "acdb", or "0.5", I just get the program printing "enter a number: " infinitely, as if it's running the loop but scanf_s is broken and so it just skips itself.
Thanks for your help.
int input = 1;
while (input != 0) {
printf("enter a number: \n");
scanf_s("%d", &input);
/* rest of code omitted */
}
scanf_s returns the number of variables in the variable argument list that were successfully filled with data.
In your case you want that to be 1 therefore.
If you do encounter invalid input then it's your job to clear that, otherwise it remains on the input stream. For more on that see How to clear input buffer in C?
This question already has answers here:
Simple C scanf does not work? [duplicate]
(5 answers)
Closed 5 years ago.
Just starting to learn C/C++ in my CS 221 class.
Here's the start of my code:
int main()
{
int id;
char name[100];
double wages;
printf("Enter employee's id: ");
scanf("%4i", id);
printf("Enter employee's full name: ");
scanf("%99[^\n]",name);
printf("Enter gross salary for %s :",name);
return 0;
}
Having trouble wrapping my head around char arrays, stdin and buffers in c. Why does the above code skip the user imputing a name? do I need fflush in there? couldn't really get that to work either.
Not sure why you use
scanf("%4i", id);
But try:
scanf("%d", &id );
This fixes the issue of %4i format, and replaces it with %d for "Decimal integer" format.
This also fixes the issue of , id); because written like this, will not allow id to be stored, and would definitely crash.
If you have issues with numbers you are entering crashing from being too big, maybe check the boundaries of your data type int which will be platform specific.
Also change:
scanf("%99[^\n]",name);
to:
scanf("%s\n",name);
This takes the data entered by the user through scanf and interprets it as a "String of characters" and places it in name properly.
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 7 years ago.
When I wrote a simple program to read and print a number using float, I came across some unexpected results.
In the following program when I entered a number 100.62, the result I got was 100.620003. Only upto eight digits the value showed correctly. Even when I tried different numbers the issue persisted. Why is this? Is there a way to overcome this problem other than by limiting the decimal points.
And also is there a way to print a digit as it is? ie; if I enter 100.62 it should print 100.62 and if enter 100.623 it should print 100.623 (not 100.620000 and 100.623000).
int main (void)
{
float i;
printf ("Enter a number : ");
scanf ("%f", &i);
printf ("You entered the number : %f", i)
return 0;
}
This issue comes from how computers handle the float values.
One way to overcome this problem is stopping using float and deal the numbers as decimal data.
To print a digit as it is, just read input as a string and print it. This method is very simple and can support difficult inputs such as 1e-10000 or 1.00000.
As far as I know, the C always display those complete digits of float in default.
If you want to display custom decimal points, you should try using ".x" before the f notation. X is the total number that you want to show after the dot. For example:
float i;
printf ("Enter a number : ");
scanf ("%f", &i);
printf ("You entered the number : %.2f", i)
return 0;
this will result 2 numbers after the dot. If you input 100.62, it will show as 100.62. However, this is static. I mean, if you input 100.623, it will show 100.62, not 100.623. You should know exactly how many numbers after dot that you want to show.
This question already has answers here:
Yes/No loop in C
(3 answers)
Closed 8 years ago.
I am trying to execute a small program using do-while.
#include<stdio.h>
#include<conio.h>
void main()
{
char another;
int num;
do
{
printf("Enter a number");
scanf("%d",&num);
printf("Square of %d id %d",num,num*num);
printf("Want to another another number y/n");
scanf("%c",&another);
}while(another=='y');
}
Now when I try to execute the program, it runs fine. I input a number and it displays its square. And then I see Want to enter another number y/n. But as soon as I press any key (y or n), the program exits itself before I can press enter to provide the input. I tried it many times but no success.
But the program runs fine if I ask the user to input either 1 or 2(in place of y/n). In that case it takes an integer input and can check the while block. If another == 1, the program runs again.
My problem is that why can't I check for a character in the while condition.
The reason it doesn't work is that after scanf gets num, the new line is still in the buffer, so it will be processed by the next scanf with %c format specifier. A direct way of fixing it is to use:
scanf(" %c", &another);
// ^space
Note that your original scanf("%c:,&another); won't compile, but I assume that's a typo. And always use int main, or it's undefined behavior.