Link to home
Start Free TrialLog in
Avatar of Gipsy
Gipsy

asked on

increment function

Hello,

Briefly to describe what i am trying to do: The user enters a number and the function increment  , increments the number by 1. The numbers a user enters are added to the front of the list, which makes the incrementation easier and plus some other operations. Current display function is for helping to see how incrementation work.
Can you please have a look at the increment function. I have put 3 options:
1. if number = null
2. if value in the first node is from 0 to 8
3. if value in the first node is 9

Please try to input

52, it will return 53
59 will return 60
but 58 ill return 60 - and that is the problem i am having.

if i put the if statement (if value in the first node is from 0 to 8) after statement (if value in the first node is 9), then when user inputs 59, it gives them an error. Cant find why it is occuring so ?
Can you pls advise where is the bug?

#include<iostream.h>
#include<string>
typedef int ListItemType;

class Newstring

{
private:

    struct digit // a node on the List
    {
      ListItemType item; // data item on the list
      digit *next; // pointer to the next node
      };

     digit *number;
     digit *head;    // Points to front end of list.
     digit *tail;    // Points to a node one past the end of list.
   
public:

Newstring():head(NULL),tail(NULL){}
     
void addtofront ( ListItemType );
void increment ();
void display ();

~Newstring();
};


void Newstring::addtofront (ListItemType n)
{    
     digit* pNewDigit = new digit();
     pNewDigit->item = n;
     pNewDigit->next = NULL;

     if (head == NULL)  
       {
          head = pNewDigit;

     }
      
       else
       {
          digit* pCurrentHead = head;
          head = pNewDigit;
          pNewDigit->next = pCurrentHead;
     }

}

void Newstring::increment ()

{
      digit *current=head;

if (head==NULL)
      {
            digit *current=head;
                  current->item =1;
      }

if(current->item<9 && current->item >=0)
{
                  current->item=current->item++;
}


if(current->item==9 && current->next!=NULL){

for( current=head; current!=NULL;current=current->next) {

if(current->item==9)
      current->item=0;

else
current->item++;

}
}


}


Newstring::~Newstring()
{
     digit* pTemp = head;

     while(pTemp)
     {
          head = head->next;
          delete pTemp;
          pTemp = head;
     }
     head = NULL;    
}

void Newstring::display()
{
     digit* pFirst = head;

     while (pFirst) {
          cout << pFirst->item << endl;
          pFirst = pFirst->next;
     }
}



int main ()
{

Newstring number1;

ListItemType n;

cout<<"Insert number: ";
cin>>n;
while (n!=-1)
{
cout<<"Insert number: ";
number1.addtofront(n);
cin>>n;
}
number1.display ();
number1.increment();
cout<<"After incrementation: "<<endl;
number1.display ();

      return 0;
}
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

I don't have much clear you question but I guess you are missing some 'else's:

if (head==NULL)
     {
          digit *current=head;
               current->item =1;
     }
else if (current->item<9 && current->item >=0)
{
               current->item=current->item++;
}
else if (current->item==9 && current->next!=NULL)
{
   for( current=head; current!=NULL;current=current->next) {

       if(current->item==9)
             current->item=0;
        else
             current->item++;
    }
}
Avatar of Gipsy
Gipsy

ASKER

Thanks a lot, that helped. Would you be so kind please to advise me on:

i want to also take into account 999, in which case i will have to add one node. The bleow code doesnt do what it is supposed to at the moment ...

else if (current->item==9 && current->next==NULL) {

 digit* pNewDigit = new digit();
 pNewDigit->item = 1;
 pNewDigit->next = NULL;
 current->next = pNewDigit;
}
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gipsy

ASKER

Thanks, I have reworked the idea into the non recursive function and that worked !

void Newstring::increment ()

{
     digit *current=head;

if (head==NULL)
     {

 addtofront(1);

     }
else if (current->item<9 && current->item >=0)
{
               current->item=current->item++;
}

else if (current->item==9 ) {


   for( current=head; current!=NULL;current=current->next) {

       if(current->item==9)
         {
             current->item=0;
                   if (current->next == NULL)
                         addtofront(1);
         }
         else
         {
             current->item++;
                   break;
         }
    }

}

}