| | |
I need help with a linked list program
Thread Solved
![]() |
•
•
Join Date: Sep 2004
Posts: 42
Reputation:
Solved Threads: 0
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:
Code:
C++ Syntax (Toggle Plain Text)
#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); }
![]() |
Similar Threads
- how to create linked list? (C)
- Need of linked list program (C)
- Major Problem with Doubly linked list program (C++)
- Linked List program HELP (C++)
- Doubly Linked List Problem (C++)
- Adding to linked list from external file (C)
- printing a linked list (C++)
Other Threads in the C++ Forum
- Previous Thread: An logical error in C++
- Next Thread: how to start another programme from exe created in C++
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






