Thread: poiter returning wierd number

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    3

    poiter returning wierd number

    I did a exercise,but got a wierd result.
    Here is the question:
    Write a function that takes two input arguments and provides two separate results to the caller, one that is the result of multiplying the two arguments, the other the result of adding them. Since you can directly return only one value from a function, you'll need the second value to be returned through a pointer or reference parameter.
    Here is my solution:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int calculate(int x,int y,int *sum)
    {
        *sum = x + y;
    
    
        return x*y;
    }
    int main()
    {
        int num1,num2,sum;
        cout<<"enter number 1:";
        cin>>num1;
        cout<<"enter number 2:";
        cin>>num2;
        cout<<calculate(num1,num2,&sum)<<endl<<sum;
        return 0;
    }

    If I put num1=4 and num2=5,It would cout 20 .
    4309744
    Where is 4309744 coming from?
    Is that address of variable sum? But I think my sytanx is right.
    Any help would be appreciated.




    Last edited by Jin-Ting Lyu; 06-22-2016 at 03:59 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You have experienced undefined behavior. In a nutshell, don't use it like that. Never should you change a variable that is used later( or earlier ) inside the same cout statement.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The main problem is that sum is uninitialized, and in this case, gets evaluated before calculate(...), so it's printing the random garbage value that it just happens to have, before you assign anything to it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    To the OP: your calculate() has 3 arguments but you're required to use 2. One way is to use pointers to capture the return values of sum and product of the 2 integers and then pass them to console in the following manner:

    Code:
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    
    void multi_return(int x, int y);
    
    
    int main(){
        int x, y; 
        cout<<"Enter 2 numbers: "<<endl;
        while(cin>>x>>y){
            multi_return(x, y);
        }
    }
    void multi_return(int x, int y){
        int * p = (int*)malloc(sizeof(int)*2);
        p[0]=x + y;
        p[1]=x*y;
        cout<<"Sum of the numbers is: "<<p[0]<<endl;
        cout<<"Product of the numbers is: "<<p[1]<<endl;
    }
    If you search using something like "c++ 2 return values from function" you'll find other ways of doing this like using structure, templates, etc.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by sean_cantab View Post
    To the OP: your calculate() has 3 arguments but you're required to use 2.
    Incorrect. The requirement is that the function accept two input arguments. That does not implicitly mean two arguments, in total. It could be interpreted to mean that the output parameter should be in addition to the input parameters.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your code relies on a particular order of evaluation.

    The compiler has opted to insert sum before it was changed by calculate(), which it is allowed to do. See Sequence point - Wikipedia, the free encyclopedia for examples of the behavior.

    There are a number of fixes, including this one:
    Code:
    cout<<calculate(num1,num2,&sum)<<endl;
    cout<<sum<<endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an integer ... not returning; weird error
    By Imanuel in forum C++ Programming
    Replies: 19
    Last Post: 09-25-2011, 01:30 PM
  2. Poiter to a pointer
    By linuxlover in forum C Programming
    Replies: 9
    Last Post: 03-16-2011, 12:37 PM
  3. return not returning expected number
    By lord in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2008, 10:07 PM
  4. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  5. Question about the wierd number representations
    By Jaken Veina in forum C Programming
    Replies: 13
    Last Post: 04-07-2005, 11:19 PM

Tags for this Thread