Solved

# increment function

Posted on 2004-11-27
Medium Priority
204 Views
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;
}
0
Question by:Gipsy
• 2
• 2

LVL 55

Expert Comment

ID: 12687180
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 Comment

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

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

Author Comment

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

## Featured Post

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.
###### Suggested Courses
Course of the Month16 days, 3 hours left to enroll