![]() |
| | #1 |
| Registered User Join Date: Feb 2008
Posts: 12
| My class. Code: class MayanNumberType
{
public:
int GetArabicNumber();
void GetMayanNumber();
void ConvertToArabic();
void ConvertToMayan();
void PrintArabicNumber();
void PrintMayanNumber();
char MayanNumber[50];
int ArabicNumber;
};
Code: void MayanNumberType::GetMayanNumber()
{
MayanNumberType x;
cout << "Please enter the Mayan Number: ";
cin.ignore();
cin.getline(x.MayanNumber, 50);
}
Code: void MayanNumberType::PrintMayanNumber()
{
MayanNumberType x;
cout << x.MayanNumber[0];
}
|
| rainman39393 is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| 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? |
| tabstop is offline | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| 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).
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| array, class |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trouble with private portion of Classes | SoBlue | C++ Programming | 3 | 01-22-2007 05:32 PM |
| Having trouble with Classes | flicka | C++ Programming | 3 | 10-08-2005 08:41 AM |
| Having Trouble Understanding Classes | prog-bman | C++ Programming | 1 | 05-19-2004 12:44 PM |
| Trouble Understanding Classes and Objects. Please Help. | Jeffcubed | C++ Programming | 4 | 12-06-2002 02:23 PM |
| Trouble with classes | PorkyChop | C++ Programming | 3 | 11-27-2002 02:43 PM |