I cant seem to find the solution to this. I want to ble able to convert cm to m, with a pretty simple math expression cm / 100 = m and I need to use functions, thats the idea behind the code. Can anyone help? It treats the output as int, not double, because when I enter 100, it outputs 1, so correct, but when I output anything lower than 100, it outputs 0. So I assume its the double/int problem. Help?
#include <iostream>
usingnamespace std;
int M (double a, double b); //funkcijas proto
int CM(); //funkcijas proto
int MM(); //funkcijas proto
int main() {
double x, z, sum;
cout << "Ievadi centimetrus : "; cin >> z;
sum = M(x, z);
cout << "Tie ir " << sum << " metri";
}
int M (double a, double b) {
double rez;
rez = a = b/100;
return rez;
}
#include <iostream>
double M(double a, double b);
int main()
{
// initialize your variables before using them
double x { };
double z { };
std::cout << "Ievadi centimetrus : ";
std::cin >> z;
// what value is x supposed to be?
double sum = M(x, z);
std::cout << "Tie ir " << sum << " metri\n";
}
double M(double a, double b)
{
double rez;
rez = a = b / 100;
return rez;
}
1a. sum in initialized at line 15 when it is used to take the function's return.
2. single letter variables are really not a good idea. YOU might know what they are used for, but anyone else has no clue. Give your variables descriptive names, like sum.
3. You create a variable, x, never assign it a value and then use it in a function call parameter. Shouldn't the user give x a value?
[ETA:] I go to all the trouble of 'splaining what to do and fail to make the changes myself. Oooops!