Thread: Help w/adjustment to program

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    Help w/adjustment to program

    The following code works well, it takes input from sample.txt which is just a list of five integers. The main, as you can see, just adds, subtracts, etc. Here is code, then I'll tell you what I want to change:

    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    struct Node{
    	char digit;
    	Node *next;
    };
    
    class BigInt{
    	int Num;
    public:
    	friend BigInt operator+(BigInt A, BigInt B);
    	friend BigInt operator-(BigInt A, BigInt B);
    	friend istream& operator>>(istream& in, BigInt &B);
    	friend ostream& operator<<(ostream& o, BigInt &B);
    };
    
    BigInt operator+(BigInt A, BigInt B)
    {
    	A.Num += B.Num;
    	return A;
    }
    
    BigInt operator-(BigInt A, BigInt B)
    {
    	A.Num -= B.Num;
    	return A;
    }
    
    istream& operator>>(istream& in, BigInt &B)
    {
    	in >> B.Num;
    	return in;
    }
    
    ostream& operator<<(ostream& o, BigInt &B)
    {
    	o << B.Num;
    	return o;
    }
    
    
    void main()
    {
    	BigInt v1, v2, v3, v4, v5;
    
    	ifstream in;
    
    	in.open("sample.txt");
    
    	in >> v1 >> v2 >> v3 >> v4 >> v5;
    
    	cout << v1+v2 << endl;
    	cout << v3-v4 << endl;
    }
    Ok, I want to change:

    Code:
    class BigInt{
    	int Num;
    to:

    Code:
    class BigInt{
    	Node *Num;
    How do I change the overload operator functions to accomodate this??
    Thanks!!

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    PHP Code:
    BigInt operator+(BigInt ABigInt B)
    {
        
    A.Num->digit += B.Num->digit;
        return 
    A;


    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;/'_`&nbsp;\/\&nbsp;\/\_&nbsp;,`\&nbsp;&nbsp;/&nbsp;__`\\&nbsp;\&nbsp;\&nbsp;&nbsp;/\&nbsp;\/\&nbsp;\&nbsp;&nbsp;/'__`\&nbsp;
    /\&nbsp;\_\&nbsp;\&nbsp;\&nbsp;\/_/&nbsp;&nbsp;/_/\&nbsp;\_\&nbsp;\\_\&nbsp;\_\&nbsp;\&nbsp;\_/&nbsp;|/\&nbsp;&nbsp;__/&nbsp;
    \&nbsp;\___,_\&nbsp;\_\/\____\&nbsp;\____//\____\\&nbsp;\___/&nbsp;\&nbsp;\____\
    &nbsp;\/__,_&nbsp;/\/_/\/____/\/___/&nbsp;\/____/&nbsp;\/__/&nbsp;&nbsp;&nbsp;\/____/
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks disolve.....now my output is letters??? Here is the overload functions I have in place:

    Code:
    BigInt operator+(BigInt A, BigInt B)
    {
        A.Num->digit += B.Num->digit;
        return A;
    }
    
    BigInt operator-(BigInt A, BigInt B)
    {
        A.Num->digit -= B.Num->digit;
        return A;
    }
    
    istream& operator>>(istream& in, BigInt &B)
    {
    	in >> B.Num->digit;
    	return in;
    }
    
    ostream& operator<<(ostream& o, BigInt &B)
    {
    	o << B.Num->digit;
    	return o;
    }

  4. #4
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    That's because 'digit' is type char.. I assumed when you said you wanted Node *Num that you wanted to use 'Node.digit' for storage.. change digit to int if so.

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;/'_`&nbsp;\/\&nbsp;\/\_&nbsp;,`\&nbsp;&nbsp;/&nbsp;__`\\&nbsp;\&nbsp;\&nbsp;&nbsp;/\&nbsp;\/\&nbsp;\&nbsp;&nbsp;/'__`\&nbsp;
    /\&nbsp;\_\&nbsp;\&nbsp;\&nbsp;\/_/&nbsp;&nbsp;/_/\&nbsp;\_\&nbsp;\\_\&nbsp;\_\&nbsp;\&nbsp;\_/&nbsp;|/\&nbsp;&nbsp;__/&nbsp;
    \&nbsp;\___,_\&nbsp;\_\/\____\&nbsp;\____//\____\\&nbsp;\___/&nbsp;\&nbsp;\____\
    &nbsp;\/__,_&nbsp;/\/_/\/____/\/___/&nbsp;\/____/&nbsp;\/__/&nbsp;&nbsp;&nbsp;\/____/
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    74
    hmmm....I want to use Node.digit as storage, but as char...but output as int's...does that make sense??? I can't figure this darn thing out!!

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    74
    Ok, it's occurred to me I need some sort of insert function. I'm not a good coder, so please bear with me. This is my current code, and my errors follow:

    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    struct Node{
    	char digit;
    	Node *next;
    };
    
    class BigInt{
    	Node *Num;
    	int cap;
    	int top;
    public:
    
    	void insert ( int);
    	void remove (int);
    	int isEmpty () {return !top;};
    	int isElement (int);
    	int size() {return top;};
    	friend BigInt operator+(BigInt A, BigInt B);
    	friend BigInt operator-(BigInt A, BigInt B);
    	friend istream& operator>>(istream& in, BigInt &B);
    	friend ostream& operator<<(ostream& o, BigInt &B);
    };
    
    int BigInt::isElement (int target)
    {
    	for (int i=0; i < top; i++)
    		if (Num [i] == target) //<Error 1	
    
    	        return 1;
    
    	return 0;
    }
    
    void BigInt::insert (int target)
    {
    	if (isElement(target))
    		return;
    
    	if (top == cap) {
    		Node *temp = new Node [cap + 5];
    		for (int i=0; i<top; i++) temp [i] = Num->next [i];
    		cap =+5;
    		delete []Num;
    		Num->next = temp;
    	}
    	Num->next [top++] = Num.target; //<ERROR 2
    }
    
    void BigInt::remove (int target)
    {
    	char i;
    
    	if (! isElement (target))
    		return;
    
    	for (i = 0; i < cap; i ++)
    		if (Num[i] == target) //<ERROR 3
    			break;
    
    		while (i < cap -1){
    			Num [i] = Num [i+1];
    			i++;
    		}
    		top --;
    }
    
    BigInt operator+(BigInt A, BigInt B)
    {
        A.Num->digit += B.Num->digit;
        return A;
    }
    
    BigInt operator-(BigInt A, BigInt B)
    {
        A.Num->digit -= B.Num->digit;
        return A;
    }
    
    istream& operator>>(istream& in, BigInt &B)
    {
    	in >> B.Num->digit;
    	return in;
    }
    
    ostream& operator<<(ostream& o, BigInt &B)
    {
    	o << B.Num->digit;
    	return o;
    }
    
    
    
    void main()
    {
    	BigInt v1, v2, v3, v4, v5;
    
    	ifstream in;
    
    	in.open("sample.txt");
    
    	in >> v1 >> v2 >> v3 >> v4 >> v5;
    
    	cout << v1+v2 << endl;
    	cout << v3-v4 << endl;
    }
    Errors:

    ERROR 1>> error C2676: binary '==' : 'struct Node' does not define this operator or a conversion to a type acceptable to the predefined operator

    ERROR 2>> error C2228: left of '.target' must have class/struct/union type

    ERROR 3>> error C2676: binary '==' : 'struct Node' does not define this operator or a conversion to a type acceptable to the predefined operator
    Error executing cl.exe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM