C++ float scanf input number - c

I use Bloodshed DevC++ to compile this code.
But I get problem with scanf float number.
And I don't want get decimal type, but the result must in float type,
But the result always be 0.000000.
This is the code:
#include <stdio.h>
main()
{
float A, B, C;
B = 3.14;
C = (4*B*A*A);
printf (" Enter value of A : "); scanf("%f", &A);
printf ("\n");
printf (" So, the result is : %f", C);
printf ("\n");
}
And result is:
Float.jpg
Enter value of A : 22.45
So, the result is : 0.000000
--------------------------------
Process exited after 7.235 seconds with return value 0
Press any key to continue . . .
It would be better if anyone help me, Thanks :)

When you do C = (4*B*A*A);, this doesn't mean that C will always contains what that expression evaluates to. It means that you're modifying C based on the value of that expression at that moment.
So changing A after this point has no effect on the value of C. And since A is uninitialized, this results in undefined behavior, meaning the results are not necessarily predictable.
You need to first read in the value of A, then modify C based on the current values of A and B:
printf (" Enter value of A : "); scanf("%f", &A);
C = (4*B*A*A);
printf ("\n");
printf (" So, the result is : %f", C);
printf ("\n");
To do what you though your original code would do, you'd need to define a function that will calculate the value of C when you call it.
#include <stdio.h>
int C(int A, int B)
{
return 4*B*A*A;
}
int main()
{
float A, B;
B = 3.14;
printf (" Enter value of A : "); scanf("%f", &A);
printf ("\n");
printf (" So, the result is : %f", C(A,B));
printf ("\n");
}

You are calculating the "result" before you ask for input:
float A, B, C;
B = 3.14;
C = (4*B*A*A); // What value has A here? Probably 0 !!
printf (" Enter value of A : "); scanf("%f", &A); // This has no influence
// whatsoever on the
// value of C
printf ("\n");
printf (" So, the result is : %f", C);
printf ("\n");
I am not sure about C (question was tagged C++ before), but I guess A gets initialized to 0, thus the result is indeed exactly 0.
PS: a kind compiler would issue a warning, something like "Warning: variable A is used uninitialized."

Related

Enter the value of two number

Actually, I'm learning C langage and I have written a programm
Enter the value of 2 numbers as shown below.
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
printf("Sum of the numbers = %d\n", c);
return 0;
}
But if I enter an alphabet I'm getting some 1522222 numbers. Instead of
this I want it throws an error as invalid input if I type alphabet ie a,b,c.
How could I do that?
You can check the return value of scanf. If it is successful, it should return 2, since you're reading two values. If you get anything else, you know that the input is incorrect. Try this:
if (scanf("%d%d", &a, &b) != 2)
printf("Invalid input type!\n");
else
printf("Sum of the numbers = %d\n", a+b);
On a different note, you don't initialize c anywhere, so printing it is undefined behavior. You don't even need c for this, you can just print a+b instead.

C language : why when input a float number in an int declared variable the result varries

using a simple code:
#include <stdio.h>
int main(int argc, int **argv)
{
int A, B, C, D, E, F;
printf ("input 1 : ");
scanf ("%d", &A);
printf ("input 2 : ");
scanf ("%d", &B);
C = A + B;
D = A - B;
E = A * B;
F = A / B;
printf ("sum : %d\n", C);
printf ("difference : %d\n", D);
printf ("product : %d\n", E);
printf ("quotient : %d\n", F);
return 0;
}
My question is as such, in the first scanf [p.s I know I can use other input methods its for a project] if you input a float/double number such as 1.3 or 20.5
the sum and difference are quite random for me,anyone can explain to me why the results are what they are?
Continuing from the comments, you must always validate all input (especially user input). All input functions provide a return. The scanf family returns the match count, the number of successful conversions processed based on the number of format specifiers in the format string. You use that to validate your input. E.g., at minimum:
#include <stdio.h>
int main(int argc, int **argv)
{
int A, B, C, D, E, F;
printf ("input 1 : ");
if (scanf ("%d", &A) != 1) {
fprintf (stderr, "error: invalid input - A.\n");
return 1;
}
printf ("input 2 : ");
if (scanf ("%d", &B) != 1 || B == 0) {
fprintf (stderr, "error: invalid input - B.\n");
return 1;
}
C = A + B;
D = A - B;
E = A * B;
F = A / B;
printf ("sum : %d\n", C);
printf ("difference : %d\n", D);
printf ("product : %d\n", E);
printf ("quotient : %d\n", F);
return 0;
}
note: your "quotient" will always be the result of integer division and truncated accordingly.
The first scanf(), if the input has a decimal point will stop at the decimal point and leave it to be read on the next operation. For input of 1.3, this means A will get the value 1, and the .3 will remain in the input stream to be read. This is because of the %d format - which tells scanf() to expect an integral value, and to stop on any characters that do appear in any representation of an integral value. A decimal point is one such character that is not used in the representation of an integral value.
The next operation (scanf("%d", &B)) immediately encounters the decimal point and returns, without changing B.
Since B is not initialised at all (before, during, or after the scanf("%d", &B)) in the program, any subsequent attempt to access its value gives undefined behaviour. Among other things, this can mean the value of B is indeterminate. From what you describe, for your setup, that results in "random" input.
If you're expecting to read input that looks like floating point values (e.g. that contains a decimal point) either read as floating point (e.g. %f format, and variables of a floating point type) or read a whole line (e.g. using fgets()) and check the contents of the line BEFORE trying to read integral values from it.
If you insist on using scanf(), check its return value. On the second call of scanf() in your scenario, scanf() will return 0 rather than 1 (since it has read no values, rather than the one value the format string specified). Which is an indication that something has gone wrong reading the input.

C - Integer throwing out random value?

Currently I am writing a simple program in C that reads in values the user enters in a loop. For some reason, when I initialize the integer a I am given a random value as opposed to the value I specified. Any help would be greatly appreciated
#include <stdio.h>
int main()
{
char sName[10];
int sTime;
int a = 0;
printf("%d", &a);
printf("Please enter the name of your snail: ");
scanf("%s", &sName);
for(a = 10; a < 20; a = a + 1) {
printf("%d", &a);
printf("Please enter the %d time of your snail: ", &a + 1);
scanf(" %d ", &sTime);
}
return 0;
}
Change this:
printf("%d", &a);
to this:
printf("%d", a);
&a is the address of a (and it's of type int*, so %d is the wrong format). a gives you the value of a.
You still need the & in scanf(" %d ", &sTime);; scanf needs the address of sTime so it knows where to store the value.
You're printing the address of a. You don't want the & in there:
printf("%d", a);
You do want the & for scanf() because you need to tell that function where (at what address) to store the value.

Error code: %d expected type int, but argument has type int *

I have an error code I do not understand:
format %d expects type int, but argument 2 has type int *
I do not know the difference between int and int *. I did not know there were different types of int, and cannot find any note of it on webpages about printf and scanf key letters.
The code is as follows:
#include <stdio.h>
#include <math.h>
int main(void)
{
int X = 0, Y = 0, A = 0, D = 0;
printf("This program computes the area of a rectangle ");
printf("and its diagonal length.");
printf("Enter Side 1 dimentions: ");
scanf("%d", &X);
printf("Enter Side 2 dimentions: ");
scanf("%d", &Y);
/* Calc */
A = X * Y;
D = pow(X,2) + pow(Y,2);
D = pow(D, 1 / 2);
/* Output */
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
return 0;
}
The error references the last two printf's:
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
Additionally, this program was originally written using floats (declaring X,Y,A, and D as float and using %f). But that gave an even stranger error code:
format %f expects type double, but argument 2 has type float *
I knew that %f is used for doubles and floats, so I could not understand why I had this error. After I got the error code about floats/doubles I tried changing everything to int (as shown in the above code), just to check. But that delivered the error code at the top of this post, which I do not understand either.
I've been using the gcc compiler.
Would someone explain what's being done wrong?
The problem is that you're trying to pass pointers to the printf function. Here's what your code looks like:
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
A is the int variable, but &A is a pointer to the int variable. What you want is this:
printf("Rectangle Area is %d sq. units.", A);
printf(" Diagonal length is %d.", D);
int* means a pointer to an int object. this is what you get because you use & before the variable name (i.e &A in your code)
You can read this to understand more about pointers and references, but basically if you omit the & before the variable names, it will work fine.
Why passing pointers to printf("...%d...", &D)?
Take a look to pointers explanation:
http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers
And to simplified printf() manual:
http://www.cplusplus.com/reference/cstdio/printf/
int d = 1;
printf("I'm an integer: %d", 42); // OK, prints "...42"
printf("I'm an integer too: %d", d); // OK, prints "...1"
printf("I'm a pointer, I have no idea why you printing me: %p", (void*)&d); // OK, prints "...<address of d>", probably not what you want
printf("I'm compile-time error: %d", &d); // Fail, prints comiper error: 'int' reqired, but &d is 'int*'

Codeblocks project.exe has stopped working C

Every time I build and run my project file, it crashes once I interact with it.
#include <stdio.h>
int main()
{
float complexnumber, a, b, r, j, theta;
j = -1;
complexnumber = a+b*j;
printf ("Please enter intput A and B in the form of a+bj\n");
printf ("Input A:");
scanf ("%f" , a);
printf ("Input B:");
scanf ("%f" , b);
theta = atan (a/b);
printf ("Theta=\n" , theta);
r = sqrt (pow(a, 2) + pow(b , 2));
printf ("R=\n" , r);
return 0;
}
Any help is much appreciated
scanf ("%f" , a);
scanf needs a pointer to the variable it shall fill, so that must be
scanf ("%f" , &a);
and similarly for b.
It might be a good idea to include the header file <math.h>.
Your printf() statement
printf ("Theta=\n" , theta);
looks incorrect,
it should be,
printf ("Theta=%f\n" , theta);
Similarly,
printf ("R=%f\n" , r);
Your scanf() statement is also wrong, it should be
scanf("%f",&a);
The line complexnumber = a+b*j; will assign garbage value to complexnumber as both a and b are uninitialized.

Resources