functions and double/int

Feb 20, 2022 at 6:14pm
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace 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;
}

Feb 20, 2022 at 6:18pm
Line 20: rez is double. Your return type is int. You're forcing a double into an int. Truncation will occur.
Feb 20, 2022 at 6:21pm
So how do I change the return type to int? Sorry if its obvious, im really ne
Feb 20, 2022 at 6:29pm
Your return type likely should be double, not an int. Unless you want to chop off the fractional part of the returned value.

sum is a double, why return an int? You convert from double to int for the return and then convert back to double to store the returned value.

Multiple conversions and a mangled value as well.
Feb 20, 2022 at 6:33pm
Im sorry I must of misread the answer before. Rez is double - yes, how to I change the return type to double? I need it to return with fractionals.
Feb 20, 2022 at 6:38pm
same as a variable, a function has a type. You maybe are used to seeing int there?

int M (double a, double b) {
double M (double a, double b) {

and you have to change the prototype too, same thing


Feb 20, 2022 at 6:43pm
My god, i'm actually blind... Yeah, im used to int most of the time, I rarely get to use double. Thank you so much for the quick responses! Much love!
Feb 20, 2022 at 6:49pm
Change int M to double M, both the function declaration and definition. It's that simple.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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;
}

A couple of issues with your code.

1. Always initialize your variables before using them. See lines 8-9, both variables are default initialized to 0.0 using "uniform initialization."
https://mbevin.wordpress.com/2012/11/16/uniform-initialization/

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!
Last edited on Feb 20, 2022 at 6:57pm
Feb 21, 2022 at 10:22am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

double M(double);

int main() {
	double z {};

	std::cout << "Ievadi centimetrus : ";
	std::cin >> z;

	std::cout << "Tie ir " << M(z) << " metri\n";
}

double M(double b) {
	return b / 100;
}

Topic archived. No new replies allowed.