C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-27-2008, 11:31 PM   #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];
}
rainman39393 is offline   Reply With Quote
Old 09-27-2008, 11:35 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 09-28-2008, 02:45 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
array, class

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:36 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22