I'm writing a program that is supposed to add two linked lists together and display their sum in vigesimal digits. The problem is that when I have a carry it will not add to the next node, and also when I try to add in lowercase values as opposed to uppercase, my answer is different, when the uppercases and lowercases of the same letter should get the same answer. Not sure what exactly what I'm doing wrong, can someone point me in the right direction? I'm assuming my issue lies in my Add() function, but i can't figure it out.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284`` ``````#include #include #undef NULL using namespace std; typedef char element; const int NULL=0; const int BASE=20; const element SENTINEL='#'; class listnode{ public: element data; listnode*next; }; class LList{ private: listnode*head; listnode*tail; public: LList(); ~LList(); void Clean(); void ReadBackward(); void Print(); void InsertHead(element thing); void InsertTail(element thing); void Duplicate(LList&Source); void Add(); void Multiply(); void DisplayMenu(); void GetMenuChoice(); void Steal(LList&Victim); void GetCommand(); void Reverse(); int CharToInt(char userval); char IntToChar(int userval); }; int main(){ LList A; //L1 A.GetCommand(); } void LList::Print(){ //PRE: The N.O. LList is valid //POST: The N.O. LList is unchanged, and its elements have been //displayed Reverse(); int counter; listnode*temp; temp=head; while(temp!=NULL){ cout<< temp->data; temp=temp->next; } cout << endl <> userval; while(userval!=SENTINEL){ temp=new listnode; temp->data=userval; temp->next=head; if(head==NULL) tail=temp; else ; head=temp; cin>>userval; } cout <=48)||(ch<=57)) num=ch-'0'; else if((ch>=65)||(ch<=74)) num=ch-'A'+10; else if ((ch>=97)||(ch<= 106)) num=ch-'a'+10; //converts lower to upper, then to int return num; } char LList::IntToChar(int userval){ //Converts integer number to vigesimal number to display to the user int num; char ch; num=userval; if((num>=0)||(num<=9)) ch=num+'0'; else if ((num>9)||(num<=19)) //num is A-J ch=num+'A'-10; return ch; } void LList::Reverse(){ //PRE: NO LList is valid //POST: NO LList is unchanged, except its elements are now in reverse //order LList Helper; //calls constructor, makes head NULL listnode*temp; temp=head; while(temp!=NULL){ Helper.InsertHead(temp->data); temp=temp->next; } Steal(Helper); } void LList::Clean(){ //PRE: The N.O. LList is valid //POST: The N.O. LList is valid and empty, and all of the memory //for its listnodes has been returned to the system memry pool listnode*temp; while(head!=NULL){ temp=head; head=head->next; delete temp; } } LList::LList(){ //PRE: None //POST: The N.O. LList is valid and empty head=NULL; } LList::~LList(){ //PRE: The N.O. LList is valid //POST: The N.O. LList is valid and empty, and all memory for its //listnodes has been returned to the heap Clean(); } void LList::InsertHead(element thing){ //PRE: The N.O. LList is valid //POST: The N.O. LList is unchanged, except it now contains a new //listnode at its head end containing thing listnode*temp; temp=new listnode; temp->data=thing; temp->next=head; if(head==NULL) tail=temp; else ; head=temp; } void LList::InsertTail(element thing){ //PRE: The N.O. LList is valid //POST: The N.O. LList is unchanged, except it now contains a new //listnode at its tail end containing thing listnode*temp; temp=new listnode; temp->data=thing; temp->next=NULL; if(head==NULL) head=temp; else tail->next=temp; tail=temp; } void LList::Steal(LList & Victim){ Clean(); head=Victim.head; tail=Victim.tail; Victim.head=NULL; } void LList::Add(){ //This function adds two linked lists together, storing their sum in a //third linked list LList Result; //sum of both llists LList B; //2nd llist B.ReadBackward(); //Gets elements to add from user B.Print(); cout<< "Adding a new vigesimal number to the current vig. number" <data); sum2=CharToInt(temp2->data); temp1=temp1->next; temp2=temp2->next; sum=(sum1+sum2+carry)%BASE; carry=(sum1+sum2+carry)/BASE; Result.InsertTail(IntToChar(sum)); } while((temp1==NULL)&&(temp2!=NULL)){ sum2=CharToInt(temp2->data); temp2=temp2->next; sum=(sum2+carry)%BASE; carry=(sum2+carry)/BASE; Result.InsertTail(IntToChar(sum2)); } while((temp2==NULL)&&(temp1!=NULL)){ sum1=CharToInt(temp1->data); temp1=temp1->next; sum=(sum1+carry)%BASE; carry=(sum1+carry)/BASE; Result.InsertTail(IntToChar(sum1)); } Steal(Result); Print(); GetCommand(); } ``````

I deleted some irrelevant functions so the code would fit.
Last edited on
Lines 108,110,112: You're using an OR condition when you need an AND condition. e.g. Any value >= 48 will cause the first if to be true and will cause the second condition to be ignored.

Line 115: What happens is the character entered is not a vigesimal digit? You're going to return an uninitialized (garbage) value.

Lines 125,127: Same issue with OR condition. You need an AND condition here also.

Line 130: Same issue returning uninitialized value if userval is out of range.
Thanks that helped a lot! Here is my updated add function, now it works sometimes but if there is a carry value it will flip it the wrong way, but if there is not a carry its fine. For example if I add 34A+1 i get 34B, but if I add I+J I get H1 instead of 1H. Not sure where the error lies.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` `````` void LList::Add(){ //This function adds two linked lists together, storing their sum in a //third linked list cout <data); //gets integer value temp1=temp1->next; //pointer pointing to next //listnode in list } else // no more values to add sum1=0; if(temp2!=NULL){ //List B is not empty sum2=CharToInt(temp2->data); //gets integer value temp2=temp2->next; //pointer pointing to next listnode } else //no more values to add sum2=0; sum=sum1+sum2+carry;//sum is sum of NO + B + carry value carry=0;//but carry is currently 0 while(sum>19){ sum=sum-BASE; carry++; Result.InsertTail(IntToChar(carry)); } Result.InsertTail(IntToChar(sum)); sum=0; } cout<< "Adding completed." <
Last edited on
Topic archived. No new replies allowed.