1

Can someone explain what i need to do in order to convert this program to return a pointer in the enterCubScouts function, I've tried everything i know of and nothing is working. I Read something about useing -> instead of the normal * but I'm kind of confused. Do you use -> in tandem with * or just ->.

 #include <iostream>
using namespace std;

struct CubScouts
{
    string name;
    int schoolGrade;
    string denName;
};

CubScouts *enterCubScouts(CubScouts *scouts[], int);
void printCubScouts(CubScouts scouts[], int);
int main()
{

    int numScouts;
    cout << "\n\nHow many cub scouts are in your pack?\n";
    cin >> numScouts;
    cin.ignore();
    CubScouts scouts[numScouts];

    enterCubScouts(scouts, numScouts);


    printCubScouts(scouts, numScouts);

    return 0;
}

CubScouts *enterCubScouts(CubScouts *scouts[],int size)
{
    for(int x=0; x<size; x++)
    {
            cout << "\nCUB SCOUT " << x+1 << ": \n";
            cout << "NAME: ";
            getline(cin, scouts[x].name);

            cout << "\n\nGRADE (1-5): ";
            cin >> scouts[x].schoolGrade;

            cout << "\n\nDEN NAME: ";
            cin.ignore();
            getline(cin, scouts[x].denName);
            cin.sync();
    }   
    return *scouts; // This needs to be a pointer 
}
5
  • For starters, enterCubScouts should return a pointer to CubScouts, not the struct; so you want CubScouts *enterCubScouts(blah, blah) Commented Apr 30, 2014 at 1:45
  • You're "new" call doesn't make sense. Maybe you should name your struct? Or else use struct when referencing it again. Commented Apr 30, 2014 at 1:53
  • @Jim ive deleted that in my code already, that was one of my failed attempts to figure this out Commented Apr 30, 2014 at 2:00
  • What are you returning the pointer for? Is the loop to be in the main call rather than the enterCubScouts() call? Commented Apr 30, 2014 at 2:02
  • "which will accept the pointer to the allocated array and the number of cub scouts." the extent of what i know about adding this part into the program, it worked fine without the pointer but im required to have it. Commented Apr 30, 2014 at 2:06

3 Answers 3

1

http://www.functionx.com/cpp/examples/returnpointer.htm

CubScouts * enterCubScouts(CubScouts *scouts[], int size)

Maybe?

I think you just need to add an asterisk

Going to try this in a c++ compiler; can't believe I don't have one on this machine

This worked for me:

CubScouts * enterCubScouts(CubScouts scouts[], int size)
{
    for (int x = 0; x<size; x++)
    {
        cout << "\nCUB SCOUT " << x + 1 << ": \n";
        cout << "NAME: ";
        getline(cin, scouts[x].name);

        cout << "\n\nGRADE (1-5): ";
        cin >> scouts[x].schoolGrade;

        cout << "\n\nDEN NAME: ";
        cin.ignore();

        cin.sync();
    }
    return scouts; // This needs to be a pointer 
}

void printCubScouts(CubScouts scouts[], int size)
{
    for (int x = 0; x<size; x++)
    {
         cout << scouts[x].name << " " << scouts[x].denName << " " << scouts[x].schoolGrade;
    }
}

not sure if it's doing what you want it to

Sign up to request clarification or add additional context in comments.

5 Comments

return &scouts; ? sorry I am trying to get it to compile but I haven't used c++ in so long
no dice on &scouts and heres a virtual compiler if you dont want to install one, onlinecompiler.net
return scouts; ? thanks for the virtual compiler, I have it in visual studio now but it doesn't like getline and I'm trying to figure out why v.v I'll try your link
Leaves this error 13.cpp: In function 'int main()': 13.cpp:41:34: error: cannot convert 'CubScouts*' to 'CubScouts**' for argumen t '1' to 'CubScouts* enterCubScouts(CubScouts**, int)'
Take the asterisk off of scouts: CubScouts * enterCubScouts(CubScouts scouts[], int size)
0

You don't even use the returned value so you might as well make it void.

This line:

CubScouts *enterCubScouts(CubScouts *scouts[],int size)

should be:

CubScouts *enterCubScouts(CubScouts scouts[],int size)

(and update the prototype, too). The way you have it currently, should generate a compiler error. Also , this line:

CubScouts scouts[numScouts];

should generate a compiler error, because arrays cannot have run-time size in C++. Try invoking your compiler in standard mode. If it seems to work for you, I'd still advise using standard containers instead of your compiler extension, as we do have a document that describes exactly how standard containers have to work.

Anyway... logically it doesn't make a lot of sense to return a pointer to a scout; since your function has no code to detect input failure. What would main() do with this pointer?

If you added some error-checking then you could return a pointer indicating the end of the list of scouts that were entered successfully, e.g.

return scouts + size;

Comments

0

This compiles at least -- not sure what the print is/ supposed to do:

#include <iostream>
using namespace std;

struct CubScouts
{
string name;
int schoolGrade;
string denName;
};

CubScouts *enterCubScouts(int x);
//void printCubScouts(CubScouts*[], int);

int main()
{

int numScouts;
cout << "nnHow many cub scouts are in your pack?n";
cin >> numScouts;
cin.ignore();
CubScouts *scouts[numScouts];

for(int x=0; x<numScouts; x++){


scouts[x] = enterCubScouts(x);

}

//printCubScouts(scouts, numScouts);

return 0;
}

CubScouts *enterCubScouts(int x)
{
 CubScouts *scout = new CubScouts;
        cout << "nCUB SCOUT " << x+1 << ": n";
        cout << "NAME: ";
        getline(cin, scout->name);

        cout << "nnGRADE (1-5): ";
        cin >> scout->schoolGrade;

        cout << "nnDEN NAME: ";
        cin.ignore();
        getline(cin, scout->denName);
        cin.sync();

return scout; // This needs to be a pointer 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.