Hello, I'm very new to C++ programming and I'm trying to get this script to work and the only error that I'm getting is:
Function does not take 0 arguments
I've googled this error to attempt to learn from other programmers mistakes, but after looking at several examples I still can't seem to find out what's wrong with my coding. It would be greatly appreciated if someone could take a look at it and get back to me on what I did wrong:
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <iostream>
float problem2a(float x, float y)
{
printf("Please enter two floating point values: ");
scanf("%f %f", x, y);
printf("The two floating point values you have entered are: ");
printf("%f and ", x);
printf("%f\n", y);
printf("%f ", x);
printf("and %f", y);
printf("are multiplied together the result is: %f", x * y);
return x * y;
}
float main()
{
problem2a();
}
I believe it has to do with the bolded/underlined section, but I'm not quite sure on how to fix this error. Any help is appreciated, thanks.
float problem2a()
{
float x, y;
float result = x * y;
printf("Please enter two floating point values: ");
scanf("%f %f", x, y);
printf("The two floating point values you have entered are: ");
printf("%f and ", x);
printf("%f\n", y);
printf("%f ", x);
printf("and %f", y);
printf("are multiplied together the result is: %f", result);
return result;
}
int main()
{
problem2a();
}
Though when I run the program I get a run time error that says:
The variable "x" is being used without being initialized
You are trying to multiply x and y before you have given them any values. Try moving your multiply instruction after you have accepted values for x and y. You can't multiply something that you don't have...
float problem2a()
{
float x;
float y;
printf("Please enter two floating point values: ");
scanf("%f ", &x);
scanf("%f ", &y);
float result = x * y;
printf("The two floating point values you have entered are: \n");
printf("%f and ", x);
printf("%f\n", y);
printf("%f ", x);
printf("and %f", y);
printf("are multiplied together the result is: %f", result);
return result;
}
int main()
{
problem2a();
}
Not only did I switch the placement of the x * y = result portion of the code, but I put & in front of x and y when it goes to scan. Works like a charm now, thanks a lot!