class MyFloat
{
	private:
		enum {MAX_DIGITS = 20};
		char Number[MAX_DIGITS + 1];
		char NumberOfDigits;
	public:
		void Write();
		friend void AssignValue(MyFloat& X);
};

void MyFloat::Write( )
{  
	int i;
	if(!NumberOfDigits == 0)
	{   cout << "0.";
	    for(i=0; i<NumberOfDigits + 1; i++)
		   cout << int (Number[i]);
	}
	else
		cout << "0.?";
}

this is the code that i am working with. the problem i am having is that i want to display a decimal with the first number as a 0, but it keeps coming up as -52. Number of Digits is initialized in the other function as 3 and the array is Number[1]=1, Number[2]=2, and Number[3] =3. If you wanna kown more, just ask. But i dont know what it is doing at all!

Attach your whole code so I can try running it and see what is going on.

#include <iostream>
              #include "MyFloat.cpp"
              using namespace std;

              void AssignValue(MyFloat& X)
              {
                X.Number[1] = 1;
                X.Number[2] = 2;
                X.Number[3] = 3;          // run program first this way with
                X.NumberOfDigits = 3;     // these numbers then change
              }                           // X.NumberofDigits = 0, to test
                                          // "0.?", which stands for an error

              void main()
              {
                MyFloat X;

                AssignValue(X);

                cout << "X = ";
                X.Write();
                cout << endl << "Press Enter key to continue";
                cin.ignore();
               }

there is the rest of my code.

I made some changes and added some comments, if I missed anything just ask.

I used one file instead of two just because I think its easier to just post small programs in one file but in larger programs I use lots of headers and source files.

#include <iostream>
//#include "MyFloat.h"
//Normally you use headers instead of source files for includes ( atleast from what I've seen)
using namespace std;



//MyFloat.h
class MyFloat
{
	//classes start off private so you do not need to put private: in unless you declare public: first
		//#define MAX_DIGITS 20
		const int MAX_DIGITS = 20;
		//enum {MAX_DIGITS = 20};  I would define this with #define or you can make it a const int within your class
		int Number[MAX_DIGITS]; //start at index 0 and dont add one to max
		int NumberOfDigits;  //use ints so you do not have to put int(Number[i]) in write
	public:
		void Write();
		friend void AssignValue(MyFloat& X);
};

void MyFloat::Write( )
{  
	if(!NumberOfDigits == 0)
	{   
		cout << "0.";
	    for(int i = 0; i < NumberOfDigits; i++) //if you start at i = 0 then dont add 1 to number of digits
		   cout << Number[i]; //remove int if you use int type for your Number[] array
	}
	else
		cout << "0.?";
}

//main.cpp
void AssignValue(MyFloat& X)
{
	X.Number[0] = 1;  //arrays start at index 0
	X.Number[1] = 2;
	X.Number[2] = 3;          // run program first this way with
	X.NumberOfDigits = 3;     // these numbers then change
}                         	  // X.NumberofDigits = 0, to test
                       	  	  // "0.?", which stands for an error

int main() //in my compiler main has to be int not sure about yours
{
	MyFloat X;
	
	AssignValue(X);
	
	cout << "X = ";
	X.Write();
	cout << endl;
	
	system("PAUSE"); //use pause to stop the program from ending without waiting for user input
	return 0;
}

No need for AssignValue to be a friend function unless it is a requirement of the assignment.

How will AssignValue know how many digits to place before and after the decimal point? 123.456 0.123456 and 123456.0 are all valid float/doubles variables. Passing both values to the function would be one way. Generating random values would be another. Asking for user input from within the function would be a third. Whatever floats your boat is what you should use.

Using dynamic memory rather than static will allow user/program to specify the above values randomly. You can limit the number of significant digits if you wish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.