| | |
help with a number array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
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!
•
•
Join Date: Oct 2009
Posts: 11
Reputation:
Solved Threads: 0
0
#3 Oct 18th, 2009
C++ Syntax (Toggle Plain Text)
#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.
0
#4 Oct 18th, 2009
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.
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.
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
0
#5 Oct 18th, 2009
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.
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.
Klatu Barada Nikto
![]() |
Similar Threads
- number of times each number in array occurs? (C)
- to secomd smallest number in array (Java)
- find larget number in an array (C)
- appending and subtracting a number in an array (C++)
- Trying To Find The Square Root Of Each Number Of A Array (C++)
- Insert a number in an array (C++)
- help with inserting a number into an array (C++)
Other Threads in the C++ Forum
- Previous Thread: Problems with strcpy.
- Next Thread: Student needs help..please
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






