I need help with a linked list program

Thread Solved
Reply

Join Date: Sep 2004
Posts: 42
Reputation: the b is an unknown quantity at this point 
Solved Threads: 0
the b the b is offline Offline
Light Poster

I need help with a linked list program

 
1
  #1
Dec 18th, 2004
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:
  1. #include <iostream.h>
  2.  
  3. struct temperature_node
  4. {
  5. int day;
  6. float temperature;
  7. temperature_node *next;
  8. };
  9.  
  10. int day_counter = 0;
  11.  
  12. temperature_node *head_ptr;
  13. temperature_node *current_ptr;
  14.  
  15. int get_temp(int days, float &temp);
  16. void show_temps();
  17. void remove_temps();
  18. void add_temp(int days, float temp);
  19. void move_c_to_end();
  20.  
  21. int main()
  22. {
  23. int days;
  24. float temp;
  25.  
  26. cout << "enter the days for which you have data ";
  27. cin >> days;
  28.  
  29. if(get_temp(days, temp))
  30. {
  31. head_ptr = new temperature_node;
  32. head_ptr->day = days;
  33. head_ptr->temperature = temp;
  34. head_ptr->next = NULL;
  35. while(get_temp(days, temp))
  36. {
  37. add_temp(days, temp);
  38. }
  39. show_temps();
  40. remove_temps();
  41. }
  42. return 0;
  43. }
  44.  
  45. int get_temp(int days, float &temp)
  46. {
  47. int keep_data = 1;
  48.  
  49. if(day_counter == days)
  50. {
  51. keep_data = 0;
  52. }
  53. else
  54. {
  55. days = day_counter + 1;
  56. cout << days;
  57. cout << "enter the temperature for day " << day_counter + 1 << endl;
  58. cin >> temp;
  59. day_counter++;
  60. }
  61. return(keep_data);
  62. }
  63.  
  64. void add_temp(int days, float temp)
  65. {
  66. temperature_node *new_rec_ptr;
  67. new_rec_ptr = new temperature_node;
  68.  
  69. new_rec_ptr->day = days;
  70. new_rec_ptr->temperature = temp;
  71. new_rec_ptr->next = NULL;
  72.  
  73. move_c_to_end();
  74. current_ptr->next = new_rec_ptr;
  75. }
  76.  
  77. void move_c_to_end()
  78. {
  79. current_ptr = head_ptr;
  80.  
  81. while(current_ptr->next != NULL)
  82. {
  83. current_ptr = current_ptr->next;
  84. }
  85. }
  86.  
  87. void show_temps()
  88. {
  89. current_ptr = head_ptr;
  90.  
  91. do
  92. {
  93. cout << current_ptr->day << " ";
  94. cout << current_ptr->temperature << endl;
  95. } while(current_ptr != NULL);
  96. }
  97.  
  98. void remove_temps()
  99. {
  100. temperature_node *temporary_ptr;
  101.  
  102. current_ptr = head_ptr;
  103.  
  104. do
  105. {
  106. temporary_ptr = current_ptr->next;
  107. delete current_ptr;
  108. current_ptr = temporary_ptr;
  109. }while(temporary_ptr != NULL);
  110. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 871
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: I need help with a linked list program

 
1
  #2
Dec 18th, 2004
show_temps() does not walk through the list!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: I need help with a linked list program

 
0
  #3
Dec 18th, 2004
Ya, you need a

current_ptr = current_ptr->next;

in there...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 42
Reputation: the b is an unknown quantity at this point 
Solved Threads: 0
the b the b is offline Offline
Light Poster

Re: I need help with a linked list program

 
0
  #4
Dec 19th, 2004
Originally Posted by Chainsaw
Ya, you need a

current_ptr = current_ptr->next;

in there...
Thank you that helped me out alot
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC