Thread: Share values between functions in same class?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    Share values between functions in same class?

    Code:
    #ifndef CANDIES_H
    #define CANDIES_H
    
    
    #include <iostream>
    #include <fstream>
    #include <Global.h>
    
    
    
    
    class Candies
    {
        public:
            void getName(std::string name, int i);
            void getPosition(std::string position, int i);
            void getClass(std::string Class, int i);
            void getImageFile(std::string file, int i);
    
    
            std::string loadName(int i);
            void loadPosition(int i);
            void loadClass(int i);
            void loadImageFile(int i);
        protected:
    
    
        private:
            std::string name[15];
            std::string position[15];
            std::string Class[15];
            std::string file[15];
    
    
            std::string name_addTXT[15];
            std::string position_addTXT[15];
            std::string Class_addTXT[15];
            std::string file_addTXT[15];
            std::string temp;
    };
    
    
    #endif // CANDIES_H
    Code:
    #include "Candies.h"
    
    
    #include <Windows.h>
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <Global.h>
    
    
    using namespace std;
    
    
    void Candies::getName(std::string name, int i)
    {
        name_addTXT[i] = name;
        name_addTXT[i] += ".txt";
    
    
        ofstream nameTXT;
        nameTXT.open(name_addTXT[i].c_str());
        nameTXT << name << std::endl;
        nameTXT.close();
    
    
    }
    
    
    void Candies::getPosition(std::string position, int i)
    {
        position_addTXT[i] = position;
        position_addTXT[i] += ".txt";
    
    
        ofstream positionTXT;
        positionTXT.open(position_addTXT[i].c_str());
        positionTXT << position << std::endl;
        positionTXT.close();
    }
    
    
    void Candies::getClass(std::string Class, int i)
    {
        Class_addTXT[i] = Class;
        Class_addTXT[i] += ".txt";
    
    
        ofstream classTXT;
        classTXT.open(Class_addTXT[i].c_str());
        classTXT << Class << std::endl;
        classTXT.close();
    }
    
    
    void Candies::getImageFile(std::string file, int i)
    {
        file_addTXT[i] = file;
        file_addTXT[i] += ".txt";
    
    
        ofstream fileTXT;
        fileTXT.open(file_addTXT[i].c_str());
        fileTXT << file << std::endl;
        fileTXT.close();
    }
    
    
    std::string Candies::loadName(int i)
    {
        ifstream file;
    
    
        file.open(name_addTXT[i].c_str());
        file >> name[i];
    
    
        temp = name[i];
    
    
        file.close();
    
    
        return temp;
    }

    When I tested it, the file name in getName() outputted the right file name. However the file name in loadName() does not print out anything in the console window.

    Can anyone explain to me why, and the best solution for this? I want it to be so that the strings in [I]name_addTXT.c_str() in both functions are equal.

    Thanks.
    Last edited by Meerul264; 03-22-2013 at 04:23 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Maybe you should also post the class definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    std::string name_addTXT[15];
    Do you really want arrays in all your member variables? o_O

  4. #4
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Code:
    std::string Candies::loadName(int i)
    {
        ifstream file;
    
    
        file.open(name_addTXT[i].c_str());
        file >> name;
    
    
        file.close();
    
    
        std::cout << "file name in loadName():" << name_addTXT[0].c_str() << std::endl;
        std::cout << "name: " << name << std::endl;
    
    
        return name;
    }
    I'm not entirely get what you're trying to point out. I guess this can do to?

    But still, how can I make the value of one variable in one function to be the same as the value of the same variable in different function within the same class? I this the point where I should use pointers?

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Rather than an object of arrays, make an array of objects. I.e. get rid of all of your arrays within the class (so each object of that class has exactly one of each member variable) and then construct 15 objects from that class.

    No need for pointers - member variables of the class are exactly the method you should be using. I'd refactor this to get rid of your arrays first, then troubleshoot.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing in values to functions
    By Mentallic in forum C Programming
    Replies: 7
    Last Post: 10-11-2012, 01:34 AM
  2. Using values from derived class in base class
    By gratuitous_arp in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2011, 09:25 AM
  3. Returning values from functions
    By JayCee++ in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2011, 11:38 AM
  4. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM
  5. Passing Values to Functions
    By shad0w in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 08:28 PM