Does anyone know what's wrong with this function? The program that uses it runs but does not display the average. I also tried using the sum variable instead of ptr3 and had the same results.

``````void calculate(int days)
{
float sum = 0;
float average;

temperature_node *ptr2;
temperature_node *ptr3;

ptr2 = new temperature_node;
ptr3 = new temperature_node;

ptr2 = current_ptr->next;

do
{
ptr3->temperature = current_ptr->temperature + ptr2->temperature;
current_ptr = ptr2->next;
}while(ptr2 != NULL);

cout << ptr3->temperature;
average = ptr3->temperature / days;
cout << average;

}``````

Does the variable in your loop condition ever change its value?

Does the variable in your loop condition ever change its value?

yup, I changed the loop to this:

``````do
{
ptr3->temperature = current_ptr->temperature + ptr2->temperature;
current_ptr = ptr2->next;
ptr2 = current_ptr->next;
}while((current_ptr != NULL) || (ptr2 != NULL));``````

still doesn't work though. :(

It's hard to debug incomplete code.

I put this together the other day.

``````#include <stdio.h>

struct node
{
double        temperature;
struct node  *next;
};

struct node list[] =
{
{ 90.1, &list },
{ 85.6, &list },
{ 87.5, &list },
{ 72.9, &list },
{ 80.4, NULL     },
};

int main ( void )
{
int count = 0;
double sum = 0;
struct node *node;
for ( node = list; node; node = node->next )
{
printf("node->temperature = %g\n", node->temperature);
sum += node->temperature;
++count;
}
printf("average = %g\n", sum / count);
return 0;
}

/* my output
node->temperature = 90.1
node->temperature = 72.9
node->temperature = 85.6
node->temperature = 87.5
node->temperature = 80.4
average = 83.3
*/``````

It's hard to debug incomplete code.

Sorry, here is the entire program.

``````#include <iostream.h>

struct temperature_node
{
int day;
float temperature;
temperature_node *next;
};

int day_counter = 1;

temperature_node *current_ptr;

int get_temps(int &days, int day_entered, float &temp);
void move_to_end();
void show_temps();
void remove_temps();
void calculate(int &days);

int main()
{
float temp;
int days = 1;
int day_entered;

cout << "Enter the number of days you have the temperature for ";
cin >> day_entered;

if(get_temps(days, day_entered, temp) == 1)
{

while(get_temps(days, day_entered, temp) == 1)
{
}
show_temps();
calculate(days);
remove_temps();
}
return 0;
}

int get_temps(int &days, int day_entered, float &temp)
{
int keep_data = 1;

if (day_entered < day_counter)
{
keep_data = 0;
}
else
{
days = day_counter;
cout << "enter the temperature for day " << day_counter << endl;
cin >> temp;
day_counter++;
}
return(keep_data);
}

{
temperature_node *rec_ptr;
rec_ptr = new temperature_node;

rec_ptr->day = days;
rec_ptr->temperature = temp;
rec_ptr->next = NULL;

move_to_end();
current_ptr->next = rec_ptr;
}

void move_to_end()
{

while(current_ptr->next != NULL)
{
current_ptr = current_ptr->next;
}
}

void show_temps()
{

cout << "Day:" <<  "        " ;
cout << "Temperature:" << endl;

do
{
cout << current_ptr->day << "           ";
cout << current_ptr->temperature << endl;
current_ptr = current_ptr->next;
} while (current_ptr != NULL);
}

void calculate(int &days)
{
cout << days;
float sum = 0;
float average;

temperature_node *ptr2;
temperature_node *ptr3;

ptr2 = new temperature_node;
ptr3 = new temperature_node;

ptr2 = current_ptr->next;

do
{
ptr3->temperature = current_ptr->temperature + ptr2->temperature;
current_ptr = ptr2->next;
ptr2 = current_ptr->next;
}while((current_ptr != NULL) || (ptr2 != NULL));

average = ptr3->temperature / days;
cout << "The average temperature for " << days << "  days is: " << average << endl;
}

void remove_temps()
{
temperature_node *temporary_ptr;

do
{
temporary_ptr = current_ptr->next;
delete current_ptr;
current_ptr = temporary_ptr;
} while (temporary_ptr != NULL);
}``````

[disclaimer]Again, I'm a little space today.

``````void calculate(int &days)
{
float average, sum = 0;
for ( temperature_node *node = head_ptr; node; node = node->next )
{
sum += node->temperature;
}
average = sum / days;
cout << "The average temperature for " << days
<< "  days is: " << average << endl;
}

/* my input/output
Enter the number of days you have the temperature for 5
enter the temperature for day 1
90.1
enter the temperature for day 2
72.9
enter the temperature for day 3
85.6
enter the temperature for day 4
87.5
enter the temperature for day 5
80.4
Day:        Temperature:
1           90.1
2           72.9
3           85.6
4           87.5
5           80.4
The average temperature for 5  days is: 83.3
*/``````

I just fixed it now, although I did not use a for loop. I look back at the problem and I think "duh, how could I not figure it out before." Anyway here is what it looks like now

``````void calculate(int &days)
{
float sum = 0;
float average;

do
{
sum = sum + current_ptr->temperature;
current_ptr = current_ptr->next;
}while(current_ptr != NULL);

average = sum / days;
cout << "The average temperature for " << days << " days is " << average << " degrees.\n";
}``````

Thanks alot for helping me :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.