944,070 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 432
  • C++ RSS
Oct 18th, 2009
0

help with a number array

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. class MyFloat
  2. {
  3. private:
  4. enum {MAX_DIGITS = 20};
  5. char Number[MAX_DIGITS + 1];
  6. char NumberOfDigits;
  7. public:
  8. void Write();
  9. friend void AssignValue(MyFloat& X);
  10. };
  11.  
  12. void MyFloat::Write( )
  13. {
  14. int i;
  15. if(!NumberOfDigits == 0)
  16. { cout << "0.";
  17. for(i=0; i<NumberOfDigits + 1; i++)
  18. cout << int (Number[i]);
  19. }
  20. else
  21. cout << "0.?";
  22. }

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninreznorgirl2 is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: help with a number array
Attach your whole code so I can try running it and see what is going on.
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Oct 18th, 2009
0
Re: help with a number array
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "MyFloat.cpp"
  3. using namespace std;
  4.  
  5. void AssignValue(MyFloat& X)
  6. {
  7. X.Number[1] = 1;
  8. X.Number[2] = 2;
  9. X.Number[3] = 3; // run program first this way with
  10. X.NumberOfDigits = 3; // these numbers then change
  11. } // X.NumberofDigits = 0, to test
  12. // "0.?", which stands for an error
  13.  
  14. void main()
  15. {
  16. MyFloat X;
  17.  
  18. AssignValue(X);
  19.  
  20. cout << "X = ";
  21. X.Write();
  22. cout << endl << "Press Enter key to continue";
  23. cin.ignore();
  24. }

there is the rest of my code.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ninreznorgirl2 is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: help with a number array
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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. //#include "MyFloat.h"
  3. //Normally you use headers instead of source files for includes ( atleast from what I've seen)
  4. using namespace std;
  5.  
  6.  
  7.  
  8. //MyFloat.h
  9. class MyFloat
  10. {
  11. //classes start off private so you do not need to put private: in unless you declare public: first
  12. //#define MAX_DIGITS 20
  13. const int MAX_DIGITS = 20;
  14. //enum {MAX_DIGITS = 20}; I would define this with #define or you can make it a const int within your class
  15. int Number[MAX_DIGITS]; //start at index 0 and dont add one to max
  16. int NumberOfDigits; //use ints so you do not have to put int(Number[i]) in write
  17. public:
  18. void Write();
  19. friend void AssignValue(MyFloat& X);
  20. };
  21.  
  22. void MyFloat::Write( )
  23. {
  24. if(!NumberOfDigits == 0)
  25. {
  26. cout << "0.";
  27. for(int i = 0; i < NumberOfDigits; i++) //if you start at i = 0 then dont add 1 to number of digits
  28. cout << Number[i]; //remove int if you use int type for your Number[] array
  29. }
  30. else
  31. cout << "0.?";
  32. }
  33.  
  34. //main.cpp
  35. void AssignValue(MyFloat& X)
  36. {
  37. X.Number[0] = 1; //arrays start at index 0
  38. X.Number[1] = 2;
  39. X.Number[2] = 3; // run program first this way with
  40. X.NumberOfDigits = 3; // these numbers then change
  41. } // X.NumberofDigits = 0, to test
  42. // "0.?", which stands for an error
  43.  
  44. int main() //in my compiler main has to be int not sure about yours
  45. {
  46. MyFloat X;
  47.  
  48. AssignValue(X);
  49.  
  50. cout << "X = ";
  51. X.Write();
  52. cout << endl;
  53.  
  54. system("PAUSE"); //use pause to stop the program from ending without waiting for user input
  55. return 0;
  56. }
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Oct 18th, 2009
0
Re: help with a number array
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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problems with strcpy.
Next Thread in C++ Forum Timeline: Student needs help..please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC