?
Solved

increment function

Posted on 2004-11-27
4
Medium Priority
?
204 Views
Last Modified: 2010-04-01
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;
}
0
Comment
Question by:Gipsy
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12687180
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
 

Author Comment

by:Gipsy
ID: 12687223
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 160 total points
ID: 12687288
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
 

Author Comment

by:Gipsy
ID: 12694766
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question