Passing array between functions

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 26
Reputation: AssaultM16 is an unknown quantity at this point 
Solved Threads: 1
AssaultM16 AssaultM16 is offline Offline
Light Poster

Passing array between functions

 
0
  #1
Sep 8th, 2009
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Passing array between functions

 
0
  #2
Sep 8th, 2009
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!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 26
Reputation: AssaultM16 is an unknown quantity at this point 
Solved Threads: 1
AssaultM16 AssaultM16 is offline Offline
Light Poster

Re: Passing array between functions

 
0
  #3
Sep 8th, 2009
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
  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
  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:
  1. outputSavedata(int stats[4])

Thanks,

AssaultM16
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Passing array between functions

 
1
  #4
Sep 8th, 2009
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]);
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 26
Reputation: AssaultM16 is an unknown quantity at this point 
Solved Threads: 1
AssaultM16 AssaultM16 is offline Offline
Light Poster

Re: Passing array between functions

 
0
  #5
Sep 8th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC