Thread: having trouble using classes

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Question having trouble using classes

    I have an assignment to write a program that allows you to input either arabic or mayan numbers and convert them to each other. we are supposed to use a class containing all of the functions and variables. lucky for me my teacher from my previous class neglected to teach us classes so I am sort of learning them as I go along. So far everything seemed to work ok until I tried to write the function that outputs the mayan number. I have the mayan number stored in a char array. In my output function it compiles with a warning and I get an error when I run the program. But when I cout the array in the same function as I inputted it everything works out just fine. Any information would be great, Thanks!

    My class.

    Code:
    class MayanNumberType 
    {
    public: 
    	int GetArabicNumber();
    	void GetMayanNumber();
    	void ConvertToArabic();
    	void ConvertToMayan();
    	void PrintArabicNumber();
    	void PrintMayanNumber();
    	char MayanNumber[50];
    	int ArabicNumber;
    };
    This is my input function.

    Code:
    void MayanNumberType::GetMayanNumber()
    {
    	MayanNumberType x;
    	cout << "Please enter the Mayan Number: ";
    	cin.ignore();
    	cin.getline(x.MayanNumber, 50);	
    }
    This is my Output function. I am just outputting the first value in the array until I get it working.

    Code:
    void MayanNumberType::PrintMayanNumber()
    {
    	MayanNumberType x;
    	cout << x.MayanNumber[0];
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why print the Mayan Number from x, which should print out (but, alas, probably won't) as "completely uninitialized data" when you can just drop the x and print out the Mayan Number of the calling object?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The problem is that "MayanNumberType x" creates a new instance of the class. So you input the number into an instance of the class (which is destroyed when the function ends) and then you create another instance in Print and print out whatever is inside.
    They're not the same instance! Two different objects! So you can never expect to get the right answer.

    You want the class to refer to itself (its own instance). You can do this by using they keyword this. But this->myvar is the same as just myvar.
    So read into x, then print x later.

    And I should probably tell you that classes aren't meant for this sort of thing. A class is meant to be like an object, like a car. It doesn't ask questions. It merely follows your command (and takes care of everything that needs to be done to make it rolling).
    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. Trouble with private portion of Classes
    By SoBlue in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2007, 05:32 PM
  2. Having trouble with Classes
    By flicka in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2005, 08:41 AM
  3. Having Trouble Understanding Classes
    By prog-bman in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2004, 12:44 PM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 02:23 PM
  5. Trouble with classes
    By PorkyChop in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2002, 02:43 PM

Tags for this Thread