Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,907 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,771 people online right now. Registration is fast and FREE... Join Now!




c++

 
Reply to this topicStart new topic

c++, how to create a function that returns more than one variable

BigDadE84
31 Mar, 2008 - 07:36 PM
Post #1

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 16

Help!!!!!!!!!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: C++
31 Mar, 2008 - 07:38 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,998



Thanked: 126 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks smile.gif


I don't believe a method can return more than a single value, unless you return it in an array
User is online!Profile CardPM
+Quote Post

BigDadE84
RE: C++
31 Mar, 2008 - 07:45 PM
Post #3

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 16

QUOTE(PsychoCoder @ 31 Mar, 2008 - 08:38 PM) *

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif
CODE

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
float calls(float*,int, int, float, float, float);

int main()
{
    float arr[100]={0};
    int j, i=1;
    float *ptra, max=0, min=0, average=0;
    ptra=arr;
    do
    {
            cout<<"Enter grade (-1 to end): " << endl;
            cin >> arr[i];
            i++;
            
    } while (arr[i-1] > 0);
        cout << "Number" <<"    " <<"Grade"<<endl;
        cout << "------" <<"    " <<"------"<<endl;
        for (j=1;(j<i)&& (arr[j] >=0);j++)
            cout<<j<<"    "<<arr[j]<<endl;
        int size=sizeof(arr);
        a=calls(ptra,j,size,max,min,average);
        cout << "max is :" <<  a[0]<< endl;
        


    
    return 0;
}
float calls(float *ptra,int j,int size, float max, float min, float average)
{
    for (int j=1; j = size;j++)
    {
        if (*ptra > *ptra+1)
        {
            max=*ptra;
            min=*ptra+1;
            j++;
        }
        else if (*ptra < *ptra+1)
        {
            max=*ptra+1;
            min=*ptra;
            j++;
        }
        average=*ptra/j;
    }
    return max,min, average;
}


Thanks smile.gif


I don't believe a method can return more than a single value, unless you return it in an array

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++
31 Mar, 2008 - 07:52 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,495



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
...close.
User is online!Profile CardPM
+Quote Post

BigDadE84
RE: C++
31 Mar, 2008 - 07:57 PM
Post #5

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 16

Im sorry im new to this website! The code i posted shows the function called calls which i want to return a max, min, and average from an array which i passed to it using a pointer. I dont know how to make the function return the values. I guess i need to make it an array but i dont know how to create a function that returns arrays.

heres the code in a better view:
CODE

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
float calls(float*,int, int, float, float, float);

int main()
{
    float arr[100]={0};
    int j, i=1;
    float *ptra, max=0, min=0, average=0;
    ptra=arr;
    do
    {
            cout<<"Enter grade (-1 to end): " << endl;
            cin >> arr[i];
            i++;
            
    } while (arr[i-1] > 0);
        cout << "Number" <<"    " <<"Grade"<<endl;
        cout << "------" <<"    " <<"------"<<endl;
        for (j=1;(j<i)&& (arr[j] >=0);j++)
            cout<<j<<"    "<<arr[j]<<endl;
        int size=sizeof(arr);
        

                a=calls(ptra,j,size,max,min,average);
        cout << "max is :" <<  a[0]<< endl;
        
    return 0;
}
float calls(float *ptra,int j,int size, float max, float min, float average) // This is the function i need help with!!!!!!!!!!!!!
{
    for (int j=1; j = size;j++)
    {
        if (*ptra > *ptra+1)
        {
            max=*ptra;
            min=*ptra+1;
            j++;
        }
        else if (*ptra < *ptra+1)
        {
            max=*ptra+1;
            min=*ptra;
            j++;
        }
        average=*ptra/j;
    }
    return max,min, average;
}

User is offlineProfile CardPM
+Quote Post

syazhani
RE: C++
31 Mar, 2008 - 10:06 PM
Post #6

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 33


My Contributions
<return_type> <function_name>(<optional_parameters>)

Given that, float calls(...) will return a float. float* calls(...) will return a float pointer.

Another way to return multiple values would be to make a struct/class:
CODE

class Values
{
  public:
    float max;
    float min;
    float avg;
};
...
...
Values calls(float max, float min, float avg)
{
    //Do whatever you need before returning

    Values v;
    v.max = max;
    v.min = min;
    v.avg = avg

    return v;
}


Hope that helps... =)
User is offlineProfile CardPM
+Quote Post

BigDadE84
RE: C++
1 Apr, 2008 - 05:27 AM
Post #7

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 16

ok this is the code i have gotten so far and it still doesnt work. it says:
Line (5) : error C2146: syntax error : missing ';' before identifier 'calls'
Line (5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Line (5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Line (34) : error C2065: 'average' : undeclared identifier
Line (35) : error C2109: subscript requires array or pointer type
Line (39) : error C2146: syntax error : missing ';' before identifier 'calls'
Line (39) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Line (39) : error C2086: 'int Values' : redefinition
Line (5) : see declaration of 'Values'
Line (40) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Line (41) : error C2065: 'size' : undeclared identifier
Line (57) : error C2146: syntax error : missing ';' before identifier 'v'
Line (57) : error C2065: 'v' : undeclared identifier
Line (58) : error C2228: left of '.max' must have class/struct/union type is ''unknown-type''
Line (59) : error C2228: left of '.min' must have class/struct/union type is ''unknown-type''
Line (60) : error C2228: left of '.avg' must have class/struct/union type is ''unknown-type''
Line (62) : error C2143: syntax error : missing ';' before 'return'

This is the code I have.
CODE

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
Values calls(float*,float, float, float);

int main()
{
    float arr[100]={0};
    class Values
        {
        public:
            float max;
            float min;
            float avg;
            
        };
    float *ptra, a;
    ptra=arr;
    int j, i=1;
    
    do
    {
            cout<<"Enter grade (-1 to end): " << endl;
            cin >> arr[i];
            i++;
            
    } while (arr[i-1] > 0);
        cout << "Number" <<"    " <<"Grade"<<endl;
        cout << "------" <<"    " <<"------"<<endl;
        for (j=1;(j<i)&& (arr[j] >=0);j++)
            cout<<j<<"    "<<arr[j]<<endl;
        int size=sizeof(arr);
        a=calls(ptra,max,min,average);
        cout << "max is :" <<  a[0]<< endl;
            
    return 0;
}
Values calls(float *ptra, float max, float min, float avg)
{
    for (int j=1; j = size;j++)
    {
        if (*ptra > *ptra+1)
        {
            max=*ptra;
            min=*ptra+1;
            j++;
        }
        else if (*ptra < *ptra+1)
        {
            max=*ptra+1;
            min=*ptra;
            j++;
        }
        avg=*ptra/j;
    }
    Values v;
    v.max = max;
    v.min = min;
    v.avg = avg

    return v;
}


User is offlineProfile CardPM
+Quote Post

KYA
RE: C++
1 Apr, 2008 - 05:31 AM
Post #8

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,002



Thanked: 118 times
Dream Kudos: 1200
My Contributions
Class declaration needs to be outside of main()
User is online!Profile CardPM
+Quote Post

BigDadE84
RE: C++
1 Apr, 2008 - 05:37 AM
Post #9

New D.I.C Head
*

Joined: 31 Mar, 2008
Posts: 16

ok i did that but it still doesn't work. is my function even correct and the call also? Im lost!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 09:50PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month