Thread: Question about passing arrays

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Question about passing arrays

    I'm writing a piece of code to pass an array into a variable within an object, and then to be able to access the array again. I'm very new at programming so don't be too hard on me .

    What I don't understand is how to input an array in console, store it in a variable within an object (which is in a seperate file), and then be able to access the array from the object and print it out.

    Here's the Main function, Computer.cpp. It isn't in the least bit finished and I'm simply working on this issue before I do the rest of the code.
    Code:
    #include <iostream>
    #include "Computer.h"
    
    int main()
    {
        using namespace std;
    	char mdl[15];
    	char* mdl1;
    	cout << "What model of computer do you use?: ";
    	cin >> mdl;
    	Computer mBook;
    	mBook.SetModel(mdl);
    	mBook.GetModel(mdl1);
    	cout << mdl1 << endl;
    	return 0;
    }
    And here's my object:
    Code:
    class Computer {
    public:
    	void SetModel(char arg[15]){
    		iModel = arg;
    	}
    	void GetModel(char *arg1){
    		arg1 = iModel;
    	}
    private:
    	char iModel[15];
    };
    I was attempting to use pointers to get the address of iModel, but I couldn't figure it out.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    you can't touch iModel, it's private. What ever you want to do with iModel should be through a public method. You shouldn't break encapsulation by returning iModel.
    Code:
    iModel = arg;
    You can't assign an array to another array. you have to copy element by element, so use a loop.
    Last edited by nimitzhunter; 03-02-2011 at 05:36 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Using the
    Code:
    std::string
    class cn make your work much easier.
    and if you are not allowed to do that ( which is often the case )
    maybe you could use loops in the functions
    Computer::SetModel(char*) and Computer::GetModel(char*)
    to set and get the individual characters until you find '\0'

    btw...I'm not sure

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by nimitzhunter View Post
    you can't touch iModel, it's private. What ever you want to do with iModel should be through a public method.
    Code:
    iModel = arg;
    The function GetModel is public

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Yes it's public. But returning its address is a bad idea. Whatever you need the address for, you can write other public method to do it.
    Here's one that could work.
    Code:
    char* GetMode()
    {
       return iModel;
    }
    Last edited by nimitzhunter; 03-02-2011 at 05:43 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Here is a better code using C++ features and not C strings:
    Code:
    #include <iostream>
    #include <string>
    
    class Computer
    {
    public:
    	void SetModel(const std::string& model)
    	{
    		m_model = model;
    	}
    	const std::string& GetModel()
    	{
    		return m_model;
    	}
    private:
    	std::string m_model;
    };
    
    int main()
    {
    	std::string model;
    	cout << "What model of computer do you use?: ";
    	std::getline(std::cin, model);
    	Computer mBook;
    	mBook.SetModel(model);
    	model = mBook.GetModel();
    	std::cout << model << endl;
    	return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  2. entry level homework help: Passing Arrays into functions.
    By DHart07 in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2010, 09:11 AM
  3. Passing arrays
    By renvaar in forum C Programming
    Replies: 8
    Last Post: 11-19-2009, 08:37 PM
  4. Comparing Arrays question
    By taugust7 in forum C++ Programming
    Replies: 36
    Last Post: 04-14-2009, 12:29 PM
  5. Replies: 12
    Last Post: 06-06-2008, 05:26 PM