logic error in big digit class!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

logic error in big digit class!

 
0
  #1
Oct 7th, 2009
Hello,

I am writing a class that can handle large number of digits.

I am trying to overload >> operator so that it can store the user input value and if user entered nondigit then it should print an error message and display the default value of 1 ; overload << to output the large digits and overload + operator which adds up two large digits ( it doesn't sum the digits rather it concatenates two strings together for example if user enters 123456 for the first time and 67899 second time then + operator will create 122456 67899). But if user enter nondigits like 12a, 5r, and so on it should print an error message and set the user input value to 1.
  1. #include <iostream>
  2. #include <string>
  3. using std::ostream;
  4. using std::istream;
  5. using std::string;
  6. using <vector>
  7. using namespace std;
  8.  
  9. class LargeDigit
  10. {
  11. friend ostream &operator<<(ostream &input, const LargeDigit &);
  12. friend istream &operator>>(istream &output, LargeDigit &);
  13. public:
  14. LargeDigit();
  15. LargeDigit(String );
  16. int operator+(const LargeDigit &);
  17. private:
  18. vector<char> intz;
  19. };
  20.  
  21. # include "largeDigit.h"
  22. #include <iostream>
  23. using std::cout;
  24. using namespace std;
  25.  
  26. LargeDigit::LargeDigit()
  27. {
  28. this.intz = 0;
  29. }
  30.  
  31. LargeDigit::LargeDigit(String str)
  32. {
  33. for (int i = 0; i < str.length(); i++)
  34. {
  35. if(!isdigit(str[i])
  36. {
  37. cout<<"non-digit number is entered, so 1 is used as default
  38. value"<<endl;
  39. this.intz = 1;
  40. }
  41. else
  42. {
  43. intz.push_back(str[i]);
  44. }
  45. }
  46. }
  47.  
  48. istream &operator<<(istream &input, const LargeDigit &num)
  49. {
  50. input >> num.intz;
  51. (how do I check if user entered valid number here?)
  52. return input;
  53. }
  54.  
  55. ostream &operator<<(ostream &output, const LargeDigit &digit)
  56. {
  57. output << digit.intz;
  58. return output;
  59. }
  60.  
  61. int operator+::LargeDigit(const VeryLongInt &digit)
  62. {
  63. return digit.intz+=digit.intz;
  64. }

I know I am in right direction but can you guys tell me if I am doing everything correctly. Are my overloaded operators okay? it compiles fine and I tested my function with my quick and dirty driver and I am getting logic errors. What do I need to do in + overloaded operator function?

Thanks for your help!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 352
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 53
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #2
Oct 7th, 2009
Originally Posted by complexcodes View Post
  1. int operator+::LargeDigit(const VeryLongInt &digit)
  2. {
  3. return digit.intz+=digit.intz;
  4. }
it should be
  1. int LargeDigit::operator +(const LargeDigit &digit)//SEE THE SYNTAX properly
  2. {
  3. /*your code*/
  4. }

where did u get that VeryLongInt ........?????
U are overloading the operator + for the class LargeDigit right ...!!!
Last edited by dkalita; Oct 7th, 2009 at 5:51 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training
 
0
  #3
Oct 7th, 2009
I fix that but I need help in writing the body of that +operator overloading function.
Thanks,
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 352
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 53
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #4
Oct 8th, 2009
Originally Posted by complexcodes View Post
I fix that but I need help in writing the body of that +operator overloading function.
Thanks,
  1. int LargeDigit::operator +(const LargeDigit &digit)
  2. {
  3. return (this->intz + digit.intz);
  4. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC