943,925 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 748
  • C++ RSS
Sep 8th, 2009
0

Passing array between functions

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. #include "Savedata.h"
  6.  
  7. using namespace std;
  8.  
  9. void outputSavedata (int stats[4])
  10. {
  11.  
  12. cout<< "\nStrengh:" << stats[0] << "\n";
  13. cout<< "Defense:" << stats[1] << "\n";
  14. cout<< "Hitpoints:" << stats[2] << "\n";
  15. cout<< "Experience:" << stats[3] << "\n";
  16. cout<< "Level:" << stats[4] << "\n";
  17.  
  18. }
  19.  
  20.  
  21. void retreiveSavedata (string filename)
  22. {
  23.  
  24. int stats[4];
  25.  
  26.  
  27.  
  28. ifstream file(filename.c_str());
  29.  
  30. string skipline;
  31.  
  32. getline(file, skipline);
  33.  
  34. file>> stats[0];
  35. file>> stats[1];
  36. file>> stats[2];
  37. file>> stats[3];
  38. file>> stats[4];
  39.  
  40. outputSavedata(stats);
  41.  
  42. file.close();
  43.  
  44. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
AssaultM16 is offline Offline
26 posts
since Jul 2009
Sep 8th, 2009
0

Re: Passing array between functions

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!

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. void retreiveSavedata (int* stats);
  4. void outputSavedata (int stats[4]);
  5.  
  6.  
  7. int main()
  8. {
  9. int stats[4];
  10. retreiveSavedata(stats);
  11. outputSavedata(stats);
  12. return 0;
  13. }
  14.  
  15. void outputSavedata (int stats[4])
  16. {
  17.  
  18. std::cout<< "\nStrengh:" << stats[0] << "\n";
  19. std::cout<< "Defense:" << stats[1] << "\n";
  20. std::cout<< "Hitpoints:" << stats[2] << "\n";
  21. std::cout<< "Experience:" << stats[3] << "\n";
  22.  
  23.  
  24. }
  25.  
  26.  
  27. void retreiveSavedata (int* stats)
  28. {
  29.  
  30. stats[0] = 0;
  31. stats[1] = 10;
  32. stats[2] = 20;
  33. stats[3] = 30;
  34.  
  35. }
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Sep 8th, 2009
0

Re: Passing array between functions

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
C++ Syntax (Toggle Plain Text)
  1. #ifndef SAVEDATA_H
  2. #define SAVEDATA_H
  3.  
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void retrieveSavedata (int stats[4], string filename);
  9.  
  10. void outputSavedata (int* stats);
  11.  
  12. #endif

Savedata.h
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. #include "Savedata.h"
  6.  
  7. using namespace std;
  8.  
  9.  
  10. void retreiveSavedata(int stats[4], string filename)
  11. {
  12. ifstream file(filename.c_str());
  13.  
  14. string skipline;
  15.  
  16. getline(file, skipline);
  17.  
  18. file>> stats[0];
  19. file>> stats[1];
  20. file>> stats[2];
  21. file>> stats[3];
  22. file>> stats[4];
  23.  
  24. file.close();
  25.  
  26. }
  27.  
  28. void outputSavedata (int* stats)
  29. {
  30.  
  31. cout<< "\nStrengh:" << stats[0] << "\n";
  32. cout<< "Defense:" << stats[1] << "\n";
  33. cout<< "Hitpoints:" << stats[2] << "\n";
  34. cout<< "Experience:" << stats[3] << "\n";
  35. cout<< "Level:" << stats[4] << "\n";
  36.  
  37. }

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:
C++ Syntax (Toggle Plain Text)
  1. outputSavedata(int stats[4])

Thanks,

AssaultM16
Reputation Points: 10
Solved Threads: 1
Light Poster
AssaultM16 is offline Offline
26 posts
since Jul 2009
Sep 8th, 2009
1

Re: Passing array between functions

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]);
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Sep 8th, 2009
0

Re: Passing array between functions

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
Last edited by AssaultM16; Sep 8th, 2009 at 5:47 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
AssaultM16 is offline Offline
26 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to get textbox value of external process
Next Thread in C++ Forum Timeline: Inkey input control





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC