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;
}
GipsyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
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++;
    }
}
0
GipsyAuthor Commented:
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;
}
0
Jaime OlivaresSoftware ArchitectCommented:
Ok, I have read deeper you increment function, I think this will be enough:

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

void Newstring::increment()
{
     digit *current=head;

     if (head==NULL) {
          addtofront(1);
     } else {
           if (recursive_incr(head))
                 addtofront(1);
     }
}

bool Newstring::recursive_incr(digit *current)   // returns true if carries increment
{
      if (current->next == NULL || recursive_incr(current->next)) {
            if (current->item==9) {
                  current->item = 0;
                  return true;
            } else
                  current->item++;
      }
      return false;
}

Have not tested, please read carefully.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GipsyAuthor Commented:
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;
         }
    }

}

}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.