# 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

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:

void increment ();
void display ();

~Newstring();
};

{
digit* pNewDigit = new digit();
pNewDigit->item = n;
pNewDigit->next = NULL;

{

}

else
{
}

}

void Newstring::increment ()

{

{
current->item =1;
}

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

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

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

else
current->item++;

}
}

}

Newstring::~Newstring()
{

while(pTemp)
{
delete pTemp;
}
}

void Newstring::display()
{

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: ";
cin>>n;
}
number1.display ();
number1.increment();
cout<<"After incrementation: "<<endl;
number1.display ();

return 0;
}
###### 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.

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

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

if(current->item==9)
current->item=0;
else
current->item++;
}
}
0
Author 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
Software ArchitectCommented:
Ok, I have read deeper you increment function, I think this will be enough:

{
digit* pNewDigit = new digit;
pNewDigit->item = n;
}

void Newstring::increment()
{

} else {
}
}

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;
}

0

Experts Exchange Solution brought to you by

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

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

void Newstring::increment ()

{

{

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

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

if(current->item==9)
{
current->item=0;
if (current->next == NULL)
}
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.