Hi!. I am having some problems with functions scopes again. I need to use one array initialized in one function, in a different function. I have searched on how to pass the array by reference or value ( and I truly don't know if that's what I need) but I really don't understand it.

This is only part of my code. If you need the rest I will post it:

#include <iostream>
#include <fstream>
#include <string>

#include "Savedata.h"

using namespace std;

void outputSavedata (int stats[4])
{

    cout<< "\nStrengh:" << stats[0] << "\n";
    cout<< "Defense:" << stats[1] << "\n";
    cout<< "Hitpoints:" << stats[2] << "\n";
    cout<< "Experience:" << stats[3] << "\n";
    cout<< "Level:" << stats[4] << "\n";

}


void retreiveSavedata (string filename)
{

    int stats[4];



    ifstream file(filename.c_str());

    string skipline;

    getline(file, skipline);

    file>> stats[0];
    file>> stats[1];
    file>> stats[2];
    file>> stats[3];
    file>> stats[4];

    outputSavedata(stats);

    file.close();

}

As you can see I need to use the stats[4] array in the retrieveSavedata function in the outputSavedata function. But when I try to initializeit, it gives me this error:

'stats' not declared in that scope

Any help is appreciated,

AssaultM16

You have to create the array in the scope that you are going to use it! Probably the way to go here is have the retrieve() function accept a pointer to the array it will fill. Also, be careful not to use element 4 of an array[4] - it has 4 elements, 0,1,2,3!

#include <iostream>

void retreiveSavedata (int* stats);
void outputSavedata (int stats[4]);


int main()
{
	int stats[4];	
	retreiveSavedata(stats);
	outputSavedata(stats);
	return 0;
}

void outputSavedata (int stats[4])
{

	std::cout<< "\nStrengh:" << stats[0] << "\n";
	std::cout<< "Defense:" << stats[1] << "\n";
	std::cout<< "Hitpoints:" << stats[2] << "\n";
	std::cout<< "Experience:" << stats[3] << "\n";
	

}


void retreiveSavedata (int* stats)
{

	stats[0] = 0;
	stats[1] = 10;
	stats[2] = 20;
	stats[3] = 30;
	
}

Thanks David. Maybe you can now help me with another problem. I did exactly like that and its now giving me "undefined reference to `retrieveSavedata(int*, std::string)"

Heres the code:

Savedata.h

#ifndef SAVEDATA_H
#define SAVEDATA_H

#include <string>

using namespace std;

void retrieveSavedata (int stats[4], string filename);

void outputSavedata (int* stats);

#endif

Savedata.h

#include <iostream>
#include <fstream>
#include <string>

#include "Savedata.h"

using namespace std;


void retreiveSavedata(int stats[4], string filename)
{
    ifstream file(filename.c_str());

    string skipline;

    getline(file, skipline);

    file>> stats[0];
    file>> stats[1];
    file>> stats[2];
    file>> stats[3];
    file>> stats[4];

    file.close();

}

void outputSavedata (int* stats)
{

    cout<< "\nStrengh:" << stats[0] << "\n";
    cout<< "Defense:" << stats[1] << "\n";
    cout<< "Hitpoints:" << stats[2] << "\n";
    cout<< "Experience:" << stats[3] << "\n";
    cout<< "Level:" << stats[4] << "\n";

}

I believe I have correctly defined the retrieveSavedata function so I really don't why the compiler is giving me this error.

Also why do I need to use a a pointer to the stats variable for the outputSavedata function?. Couldn't I do this:

outputSavedata(int stats[4])

Thanks,

AssaultM16

1) The undefined reference is because you are not linking to SaveData.cpp. Which IDE are you using (Visual studio, etc)?

2) 'retreive' should be spelled 'retrieve'.

3) you seem to still be using the 5th element (element 4), when it is not valid.

4) it seems to work just fine when you change void outputSavedata (int* stats); to void outputSavedata (int stats[4]);

Thanks for your time and patience David!. It seems misspelling retrieve caused the error. Also I already fixed the stats array to [5]. Its working perfectly.

Thanks Again,

AssaultM16

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.