| | |
logic error in big digit class!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 57
Reputation:
Solved Threads: 1
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.
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!
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using std::ostream; using std::istream; using std::string; using <vector> using namespace std; class LargeDigit { friend ostream &operator<<(ostream &input, const LargeDigit &); friend istream &operator>>(istream &output, LargeDigit &); public: LargeDigit(); LargeDigit(String ); int operator+(const LargeDigit &); private: vector<char> intz; }; # include "largeDigit.h" #include <iostream> using std::cout; using namespace std; LargeDigit::LargeDigit() { this.intz = 0; } LargeDigit::LargeDigit(String str) { for (int i = 0; i < str.length(); i++) { if(!isdigit(str[i]) { cout<<"non-digit number is entered, so 1 is used as default value"<<endl; this.intz = 1; } else { intz.push_back(str[i]); } } } istream &operator<<(istream &input, const LargeDigit &num) { input >> num.intz; (how do I check if user entered valid number here?) return input; } ostream &operator<<(ostream &output, const LargeDigit &digit) { output << digit.intz; return output; } int operator+::LargeDigit(const VeryLongInt &digit) { return digit.intz+=digit.intz; }
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!
0
#2 Oct 7th, 2009
•
•
•
•
C++ Syntax (Toggle Plain Text)
int operator+::LargeDigit(const VeryLongInt &digit) { return digit.intz+=digit.intz; }
C++ Syntax (Toggle Plain Text)
int LargeDigit::operator +(const LargeDigit &digit)//SEE THE SYNTAX properly { /*your code*/ }
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.
0
#4 Oct 8th, 2009
•
•
•
•
I fix that but I need help in writing the body of that +operator overloading function.
Thanks,
C++ Syntax (Toggle Plain Text)
int LargeDigit::operator +(const LargeDigit &digit) { return (this->intz + digit.intz); }
![]() |
Similar Threads
- Error: Could not find the main class (Java)
- Please Hlpe me, lojec erooe (C++)
- Logic error in my code. (C++)
- Error with constructors(with params) of class objects inside other class (C++)
- Logic Error in Java Recursive Method (Java)
- Logic error? (C++)
- Erm, logic error?! (C++)
- Could someone please help me with this logic error?? (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: Make an array of Subclass
- Next Thread: Question: 'class Y : public X'
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux 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 return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





