I am writing a program that uses a singly linked list. The program is suposed to ask the user for the number of days they have temperatures for and for the corresponding temperature. I seem to be having a problem with the get_temp function. When I run the program it asks for the number of days and for the temperatures but when it comes to the point where it is supposed to display them it displays the last day, and the first temperature only.
Code:

#include <iostream.h>

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

int day_counter = 0;

temperature_node *head_ptr;
temperature_node *current_ptr;

int get_temp(int days, float &temp);
void show_temps();
void remove_temps();
void add_temp(int days, float temp);
void move_c_to_end();

int main()
{
	int days;
	float temp;

	cout << "enter the days for which you have data ";
    cin >> days;

	if(get_temp(days, temp))
	{
		head_ptr = new temperature_node;
		head_ptr->day = days;
		head_ptr->temperature = temp;
		head_ptr->next = NULL;
	while(get_temp(days, temp))
		{
			add_temp(days, temp);
		}
	show_temps();
	remove_temps();
	}
return 0;
}

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

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

void add_temp(int days, float temp)
{
	temperature_node *new_rec_ptr;
	new_rec_ptr = new temperature_node;

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

	move_c_to_end();
	current_ptr->next = new_rec_ptr;
}

void move_c_to_end()
{
	current_ptr = head_ptr;

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

void show_temps()
{
	current_ptr = head_ptr;

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

void remove_temps()
{
	temperature_node *temporary_ptr;

	current_ptr = head_ptr;

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

Recommended Answers

All 3 Replies

show_temps() does not walk through the list!

Ya, you need a

current_ptr = current_ptr->next;

in there...

Ya, you need a

current_ptr = current_ptr->next;

in there...

Thank you that helped me out alot :)

Be a part of the DaniWeb community

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