[SOLVED] Issues doing math with functions.

Thanks for all the advice, I changed my functions from void type to int type and that fixed the issue. Excuse some of my terminology, I'm still new to this.


I'm trying to do math with the results of functions. I'm not sure what I'm doing wrong here.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
#include <cstdlib>
using namespace std;

// Initialize functions
void reverse(int);
void value(int, int);

int main()
{
    // Variables
    int rng;
    int val;
    int sum;
    int total;

    // Opening and RNG seed
    cout << "\nThis program calculates a 'Special Difference' for each inputted number and sums them all together.\n";
    cout << "Please enter a seed for the RNG.\n";
    cout << "> ";
    cin >> rng;

    // Input and summation loop
    cout << "\nEnter the values to find the Special Difference then add.\n";
    do
    {
        cout << "> ";
        cin >> val;

        total = value(rng, val);

        sum += total;

    } while (val == 0);

    cout << "\nThe Sum is " << sum << ".\n";

    return 0;
}

// value function
void value(int seed, int num)
{
    return abs(reverse(num) - srand(seed) % num);
}

// reverse function
void reverse(int n)
{
    int reversedNum = 0, remainder;
    while (n !=0)
    {
        remainder = n % 10;
        reversedNum = reversedNum * 10 + remainder;
        n /= 10;
    }
}
Last edited on
What is this trying to do ?
Remember that a function defined to return void can't return anything.

Also when passing by value any change made to the parameters are lost when the function returns.

Hello PacificAtlantic,

To cover some things that have not been said yet.

You have included "cstdlib" for "srand()" and "rand", but you have used "abs()" without including "cmath". I know that VS and its header files the "iostream" header file will eventually include "cmath", but do not count on this being the case for all compilers and header files. Implementations are different.

1
2
3
// Initialize functions. Define prototypes. Does not initialize  anything.
int reverse(int);  // <--- Changed.
int value(int);    // <--- Changed. 


In "main":
1
2
3
4
5
// Variables
unsigned int rng{};  // <--- Changed. "seed" would be a better name.
int val{};
int sum{};
int total{};

It is a good idea to initialize your variables especially "sum" and "total". Uninitialized this line sum += total; could produce something like this: -858993460 = -858993460 + total. You would just end up with a smaller negative number better known as a garbage value.

Most often the time is used to seed the RNG as srand(static_cast<unsigned int>(time(nullptr)));. But to use what you have it should look like:
1
2
3
4
5
cout << "\nPlease enter a seed for the RNG. > ";
//cout << "> ";
cin >> rng;

srand(rng);

"srand()" only needs done once usually near the beginning of "main". See
http://www.cplusplus.com/reference/cstdlib/srand/
https://en.cppreference.com/w/c/numeric/random

If you use "time()" to seed the RNG with "srand()" know that "time" does not return an "unsigned int".

Using "srand" and "rand" this video is helpful:
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

The do/while loop does not work well. When I first tested it it only executed once. Also your code:
1
2
cout << "> ";
cin >> val;

I changed to:
1
2
cout << "Enter number > ";
cin >> val;

The wording on the prompt can be changed any way you like.

In the function "value" and in addition to what has been said there are several problems with:
return abs(reverse(num) - srand(seed) % num);

First: you are trying to return something when you have said that the function returns nothing.
return abs(reverse(num) - srand(seed) % num);. Just the "return" would work, but is not needed at the end of a function.

Second: return abs(reverse(num) - srand(seed) % num);. You are trying to use a returned value of a function that returns nothing. Not sure what you will get from this, but not what you want.

Third: return abs(reverse(num) - srand(seed) % num);. "srand" only seeds the RNG. It returns nothing. Using it in this way does not produce any random number. I think what you wanted is
return abs(reverse(num) - rand() % num);. Not sure what you are wanting, but this will produce a number from (0) zero to num -1, e.g., if "num" is 10 you would get 0 - 9. If you want 1 - 10 you would have to add 1 rand() % num + 1.

Andy
Andy, I'm not sure what you mean when you said:

Second: return abs(reverse(num) - srand(seed) % num);. You are trying to use a returned value of a function that returns nothing. Not sure what you will get from this, but not what you want.

What do you mean that the function returns nothing?
What do you mean that the function returns nothing?

The two functions you use don't return anything:

1
2
3
4
5
6
7
8
9
void test(int a)
{
     return a/2;
}

int main()
{
     int g = test(10);
}


This doesn't work. g does NOT equal 5. The function "test" is void, so a/2 dies at the end of the function and is never sent to int main() in order to set the variable g.

This is what you would want to do:

1
2
3
4
5
6
7
8
9
int test(int a) //Notice this is an "int" function and not "void".
{
     return a/2; //Now this function will return a/2 as an integer.
}

int main()
{
     int g = test(10);
}
Topic archived. No new replies allowed.