Write your question here.
I'm new to c++, but have some experience with coding.
I'm not sure why my program is not doing what i want it to do.
I want the user to input two values(doubles) to be the mass and acceleration, and then I want the values to be multiplied by each other.
#include <stdio.h>
int main(void) {
double mass=0.00, acceleeration=0.00 , force=0.00 ;
//1. Promot the user for the mass and acceleration
printf("Please enter the mass and accleeration <both floating-points values> for use in Newton's Second Law ");
//2. Get the mass and accleration from the user
scanf("%f %f", &mass, &acceleeration);
//printf("%d %d\n", mass, acceleeration);
//3. Mutliply the mass with accelleration
force = mass * acceleeration;
//4. Put it together
printf("Newton's Second Law: force = mass*acceleeration= %f\n",force);
}
C's scanf will not work correctly if it's looking for the wrong data format.
When I compile your code, I get the warning format '%f' expects the argument of type 'float*' but argument 2 (and 3) has type 'double*' [-Wformat=]
The format specifier %lf (long float) is used for doubles, not %f.
C's library is picky about that stuff, and makes the end-user do the formatting work.
EDIT: You also want to put fflush( stdout ); right after your printf statement, or else the user may not see the initial prompt.
_______________________
Side note: Since you seem to want to learn C++ and not C, I would suggest using the <iostream> library with a modern compiler
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int main()
{
double mass = 0.0;
double acceleration = 0.0;
double force = 0.0;
std::cout << "Please enter the mass and accleeration <both floating-points values> for use in Newton's Second Law " << std::endl;
std::cin >> mass >> acceleration;
force = mass * acceleration;
std::cout << "Force = " << force << std::endl;
}
10:38: warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double*' [-Wformat=]
10:38: warning: format '%f' expects argument of type 'float*', but argument 3 has type 'double*' [-Wformat=]
PS. The line 12, if uncommented, produces:
12:39: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]
12:39: warning: format '%d' expects argument of type 'int', but argument 3 has type 'double' [-Wformat=]
PS2. You say "new to c++", and then show code written in C. (It is valid C++, but C++ has other, more convenient I/O options).