help with a number array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 11
Reputation: ninreznorgirl2 is an unknown quantity at this point 
Solved Threads: 0
ninreznorgirl2 ninreznorgirl2 is offline Offline
Newbie Poster

help with a number array

 
0
  #1
Oct 18th, 2009
  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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
0
  #2
Oct 18th, 2009
Attach your whole code so I can try running it and see what is going on.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 11
Reputation: ninreznorgirl2 is an unknown quantity at this point 
Solved Threads: 0
ninreznorgirl2 ninreznorgirl2 is offline Offline
Newbie Poster
 
0
  #3
Oct 18th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 30
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso
 
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC