void HighestLowest()
{
node *cur;
cur = head;
double lowestvalue,highestvalue;
highestvalue = lowestvalue = cur->number; //set the high&low to first element
// loop through the linked list
for (cur = cur->next; cur != NULL; cur = cur->next)
{
if (cur->number > highestvalue)
highestvalue = cur->number;
else if (cur->number < lowestvalue)
lowestvalue = cur->number;
}
cout << "The highest value is " << highestvalue<< "\n\n";
cout << "The lowest value is " << lowestvalue<< "\n\n";
}
Can anyone help me with this code?? This code give me the highest value but it didn't give me the lowestvalue. Please can anyone help me?????
Did you not see my request to use code tags? If you want people to go to the trouble of checking out your code you should present it in a clearly formatted way! It not only makes it easier for someone to read your code, it also makes it possible to refer to the line where an error is found.
(You could go back and tag/reformat your earler posts so this thread is more readable in tbe future?)
But it does look like you are adding a node with uninit number member when temp == 0 (you should check whether you want to terminate before adding the new node, not after.)
(I would point out which lines the problem was on but I can't be bothered to count!)
Also be aware that testing doubles with == can be problematic. 0 is safe if you set it specifically (well, if this wasn't the case you're loop would never have temrinated!), but if you are expecting the result of a calculation to be 0 you could come unstruck as it might really be some really small number due to rounding errors. Dor this reason, double value are usually tested to determine it they're within some 'epsilon' of each other, than for equality.)
while(temp != 0)
{
num = new node; // Here you add the node
cur->next = num;
cur = num;
cout<<"Enter a value:(0 to print/exit) \n";
cin>>temp;
if(temp != 0) // Here you check whether the value should be set
// If the user enters 0: The node is added but no value is set (hence you get some garbage value like -6.27744e+066)
{
cur->number = temp;
}
}
what do you mean by terminate the add node before and not after????
I said "you need to check whether you want to terminate before adding the node" as in "terminate the loop".
Now you have line numbers... (even tho' the indenting is straggly!)
You should not be creating a new node on line 32. By doing so you always end up with an extra node on the end of your list with uninit number member.
Instead, it should be created within the if block starting on line 40 (with knock on adjustments elsewhere.)
Asctually, you also have the same problem for the first node. It's too early to create the node on line 20. Get the number first and only create a new node it the numer isn't 0.
void InsertValue()
{
node *cur;
node *num;
double temp;
cout<<"Enter a value:(0 to print/exit) \n";
cin>>temp;
num = new node; // what happens if user enters 0?
head = num;
cur = head;
if(temp != 0)
cur->number = temp;
while(temp != 0)
{
cout<<"Enter a value:(0 to print/exit) \n";
cin>>temp;
if(temp != 0)
{
num = new node;
cur->next = num;
cur = num;
cur->number = temp;
}
}
cur->next = NULL;
cur = NULL;
num = NULL;
//return;
}
The loop looks OK, but the code now on line 11-13 should only be run if the value entered (temp) is not 0.
And
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void show_list()
{
cout << endl;
node *cur = head;
// in C++ there is no point declaring on one line
// and inititlizing on another!
cout<<"The list contains: \n";
while(cur != NULL) // not cur->next != NULL (do you see why?)
{
cout << cur->number << endl;
cur = cur->next;
}
//return; pointless it at end of function (but useful for ealy returns)
}