944,044 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1898
  • C++ RSS
Jul 11th, 2005
0

i really hate pointers

Expand Post »
im confused as to how to get the pointers to save values... anyone with helpful advice??? this is where i have gone so far....

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int main () {
  7.  
  8.  
  9. typedef struct dataNode {
  10. char arriveCity[30];
  11. char departCity[30];
  12. int totalPassengers;
  13. int passengers;
  14. int flightNumber;
  15. struct dataNode *next;
  16. } dataNode;
  17.  
  18. dataNode *newNode;
  19. newNode = new dataNode;
  20.  
  21. int option1 = 0, option2 = 0;
  22. char searchCity [30];
  23.  
  24. search1: // for the goto statement on invalid choice
  25.  
  26. option1 = 0;
  27. cout << endl;
  28. cout << "Welcome to the JHD International Airport! How can we be of assistance?" << endl;
  29. cout << "Please select the best option for you:" << endl;
  30. cout << " 1. Create a new flight record." << endl;
  31. cout << " 2. Delete an existing flight record." << endl;
  32. cout << " 3. Search for a flight." << endl;
  33. cout << " 4. End current program session." << endl;
  34. cin >> option1;
  35. cout << endl;
  36.  
  37.  
  38. if (option1 == 1) {
  39.  
  40.  
  41. flight: // start of flight creation
  42.  
  43. cout << "Please input the six digit flight number (100000 - 999999): ";
  44. cin >> (*newNode).flightNumber;
  45.  
  46. if (newNode -> flightNumber < 100000 || newNode -> flightNumber > 999999) {
  47. cout << "Invalid flight number reference" << endl << endl;
  48. goto flight; // restarts flight creation
  49. }
  50.  
  51. cout << endl << "What is the departure city? ";
  52. cin >> (*newNode).departCity;
  53. cout << endl << "What city is the flight destination? ";
  54. cin >> (*newNode).arriveCity;
  55. cout << endl << "What is the maximum capacity for this flight? ";
  56. cin >> (*newNode).totalPassengers;
  57. cout << endl << "How many passengers currently have tickets? ";
  58. cin >> (*newNode).passengers;
  59. cout << endl;
  60.  
  61. system("cls");
  62. goto search1; //returns you to main menu
  63.  
  64. }
  65.  
  66. else if (option1 == 2) {
  67.  
  68. // reenter:
  69.  
  70. cout << "Which flight would you like to delete? ";
  71.  
  72. /* cin >> ;
  73.  
  74.   if ( < 100000 || > 999999) {
  75.  
  76.   cout << "Invalid entry, please re-enter. "
  77.   goto reenter; // restarts deletion
  78.  
  79.   }
  80.  
  81.   else {
  82.  
  83.   }
  84.  
  85. */
  86.  
  87. }
  88.  
  89. else if (option1 == 3) {
  90.  
  91. search2: // for the goto statement on invalid choice
  92.  
  93. cout << "Please select the search option of your choice:" << endl;
  94. cout << " 1. Display all flight records." << endl;
  95. cout << " 2. Display all departing flights from a city." << endl;
  96. cout << " 3. Display all open flights." << endl;
  97. cout << " 4. Go back to the main menu." << endl;
  98. cin >> option2;
  99. cout << endl;
  100.  
  101.  
  102. if (option2 == 1) {
  103.  
  104. if (newNode != NULL) {
  105.  
  106. while (newNode != NULL){
  107.  
  108. cout << newNode -> flightNumber << " " ;
  109. cout << newNode -> departCity[30] << " ";
  110. cout << newNode -> arriveCity[30] << " ";
  111. cout << newNode -> passengers << " ";
  112. cout << newNode -> totalPassengers << " " << endl;
  113. newNode = newNode -> next;
  114.  
  115. }
  116. cin >> option2;
  117. }
  118.  
  119. else {
  120.  
  121. cout << "No flights have been entered. " << endl;
  122.  
  123. goto search1;
  124.  
  125. }
  126.  
  127. }
  128.  
  129. else if (option2 == 2) {
  130.  
  131. cout << "What city do you wish to search for?" << endl;
  132. cin >> searchCity [30];
  133. cout << endl;
  134.  
  135. if (searchCity[30] == (*newNode).departCity[30]) {
  136.  
  137. cout << newNode -> departCity[30] << endl;
  138.  
  139. goto search1;
  140.  
  141. }
  142.  
  143. else {
  144.  
  145. cout << "I'm sorry. We currently have no flights out of that city." << endl << endl;
  146.  
  147. goto search1; // takes you back to the main menu
  148.  
  149. }
  150.  
  151. }
  152.  
  153. else if (option2 == 3) {
  154.  
  155. }
  156.  
  157. else if (option2 == 4) {
  158.  
  159. system("cls");
  160. goto search1; // takes you back to the main menu
  161.  
  162. }
  163.  
  164. else {
  165.  
  166. cout << "You have selected an invalid option. Please choose again." << endl << endl;
  167.  
  168. goto search2; // restarts flight search menu
  169.  
  170. }
  171.  
  172. }
  173.  
  174.  
  175. else if (option1 == 4) {
  176.  
  177. cout << "Thank you. Goodbye." << endl;
  178.  
  179. goto end; // ends the program
  180.  
  181. }
  182.  
  183. else {
  184.  
  185. cout << "You have selected an invalid option. Please choose again." << endl << endl;
  186.  
  187. system("cls");
  188. goto search1; // restarts main search menu
  189.  
  190. }
  191.  
  192.  
  193. end: // for the goto statement to end program
  194.  
  195. return 0;
  196.  
  197. }
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Jul 11th, 2005
0

Re: i really hate pointers

This looks like C to C++. Classes and structures are quite close so maybe you should consider using a class.. Pointers to structures use -> to address structure elements. Since you'll have many similar structures you actually have to consider a dataNode array(in case we stick to structures). Goto is almost never a good choice, maybe you can use a for(; cycle where such is needed (for menus for example) and break from it when necessary. There is probably a more elegant option but this remains for you Consider using switch instead of an if-else constructions.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
freemind is offline Offline
62 posts
since Jun 2005
Jul 11th, 2005
0

Re: i really hate pointers

Your main problem is program design. If you designed the structure of your programs better (using subroutines, no gotos), your problems with pointers (and everything else) would diminish. The main reason that you can't maintain this program is that you have designed it for unmaintainability.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Jul 11th, 2005
0

Re: i really hate pointers

While i appreciate both of your inputs... i have no problems with the structure of my program.

the whole thing i am asking about is pointers, how do i get it to store the information and then not make infinte loops.

here are segments of code.


C++ Syntax (Toggle Plain Text)
  1. typedef struct dataNode {
  2. char arriveCity[30];
  3. char departCity[30];
  4. int totalPassengers;
  5. int passengers;
  6. int flightNumber;
  7. struct dataNode *next;
  8. } dataNode;
  9.  
  10. dataNode *newNode;
  11. newNode = new dataNode;





C++ Syntax (Toggle Plain Text)
  1. cout << "Please input the six digit flight number (100000 - 999999): ";
  2. cin >> (*newNode).flightNumber;
  3.  
  4. if (newNode -> flightNumber < 100000 || newNode -> flightNumber > 999999) {
  5. cout << "Invalid flight number reference" << endl << endl;
  6. goto flight; // restarts flight creation
  7. }
  8.  
  9. cout << endl << "What is the departure city? ";
  10. cin >> (*newNode).departCity;
  11. cout << endl << "What city is the flight destination? ";
  12. cin >> (*newNode).arriveCity;
  13. cout << endl << "What is the maximum capacity for this flight? ";
  14. cin >> (*newNode).totalPassengers;
  15. cout << endl << "How many passengers currently have tickets? ";
  16. cin >> (*newNode).passengers;
  17. cout << endl;

C++ Syntax (Toggle Plain Text)
  1. while (newNode != NULL) {
  2.  
  3. if ((*newNode).passengers < (*newNode).totalPassengers) {
  4.  
  5. cout << newNode -> flightNumber << " " ;
  6. cout << newNode -> departCity[30] << " ";
  7. cout << newNode -> arriveCity[30] << " ";
  8. cout << newNode -> passengers << " ";
  9. cout << newNode -> totalPassengers << " " << endl;
  10.  
  11. newNode = newNode -> next;
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Jul 11th, 2005
0

Re: i really hate pointers

On the contrary, you do have problems with the structure of your program. Is it immediately obvious how your program flows? Not to the people who didn't code it. That means there's a problem.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Jul 11th, 2005
0

Re: i really hate pointers

I never see you assign to the your node structure's next pointer. Thus, you are using uninitialized pointers. That's your problem.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Jul 11th, 2005
0

Re: i really hate pointers

i suppose i need to update where i am at now... after receiving help from a friend... here is a post i made on the other forum i use...



Here is the short version of what the program goal is just to clear any misconceptions

You will write a program to manage a linked list. You must write functionality to add data nodes to the list, delete data nodes from the list, and display nodes from the list given specific criteria. Your program should also be set up to clean memory allocated upon exiting. You must implement a simple menu interface to prompt the user with options to create or delete records and specify search criteria. This linked list will be modelled as an airport flight manager. Your records will be structs that contain specific flight information.


and i am in no way asking for you to do this for me.. i have a test in a few days and need to know this stuff for myself... just to make that clear....

but anyways here are some snippets now... after changing off the newNode state of mind to the head tail and current pointers that Bugdude has suggested

C++ Syntax (Toggle Plain Text)
  1. typedef struct dataNode {
  2. string arriveCity;
  3. string departCity;
  4. int totalPassengers;
  5. int passengers;
  6. int flightNumber;
  7. struct dataNode *next;
  8. } dataNode;
  9.  
  10.  
  11. dataNode *head = 0, *tail = 0, *curr = 0;
  12. head = new dataNode;
  13. tail = head;
  14. curr = head;


C++ Syntax (Toggle Plain Text)
  1. curr = new dataNode;
  2. tail->next = curr;
  3. tail = tail->next;
  4.  
  5. cout << "Please input the six digit flight number (100000 - 999999): ";
  6. cin >> (*curr).flightNumber;
  7.  
  8. if (curr -> flightNumber < 100000 || curr -> flightNumber > 999999) {
  9. cout << "Invalid flight number reference" << endl << endl;
  10. goto flight; // restarts flight creation
  11. }
  12.  
  13. cout << endl << "What is the departure city? ";
  14. cin >> (*curr).departCity;
  15. cout << endl << "What city is the flight destination? ";
  16. cin >> (*curr).arriveCity;
  17. cout << endl << "What is the maximum capacity for this flight? ";
  18. cin >> (*curr).totalPassengers;
  19. cout << endl << "How many passengers currently have tickets? ";
  20. cin >> (*curr).passengers;
  21. cout << endl;




C++ Syntax (Toggle Plain Text)
  1. while (curr != NULL) {
  2.  
  3. if ((*curr).passengers < (*curr).totalPassengers) {
  4.  
  5. cout << curr -> flightNumber << " " ;
  6. cout << curr -> departCity << " ";
  7. cout << curr -> arriveCity << " ";
  8. cout << curr -> passengers << " ";
  9. cout << curr -> totalPassengers << " " << endl;
  10.  
  11. curr = curr -> next;
  12.  
  13. }
  14.  
  15. }

my question is... how do i save the old information... i can get it to print new information i store in... but if i store 2 different ones... the first is lost... is it in the way i have the while loop set up or am i not calling the function correctly?? or is my code just so hard to follow even the computer is looking at me stupid.

Thanks everybody.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Jul 12th, 2005
0

Re: i really hate pointers

C++ Syntax (Toggle Plain Text)
  1. struct {
  2. string arriveCity;
  3. string departCity;
  4. int totalPassengers;
  5. int passengers;
  6. int flightNumber;
  7. } *dataNode;
  8.  
  9. // .....
  10.  
  11. cout << "Please input the six digit flight number (100000 - 999999): ";
  12. cin >> dataNode->flightNumber;
  13.  
  14. // .....

Good morning first! This is how you write to structure's members through a pointer. In general it's up to you to change the structure of your program and prefer one style to the other one, But believe me or not - if you consider the suggestions made the code that you write will be less messy. When you need help (at least) this is important
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
freemind is offline Offline
62 posts
since Jun 2005
Jul 12th, 2005
0

Re: i really hate pointers

Why does this code only return values on the flights before you have a full flight... but the next one isnt full and it wont return values....

EX:

flight 1 has 23 passengers out of 26
flight 2 has 23 passengers out of 23
flight 3 has 12 passengers out of 26

it would only return flight 1 and not flight 3... any help on fixing this?


C++ Syntax (Toggle Plain Text)
  1. dataNode *temp = head;
  2. curr = head;
  3.  
  4. if ((*curr).passengers < (*curr).totalPassengers) {
  5.  
  6. while (curr != NULL && (*curr).passengers < (*curr).totalPassengers) {
  7.  
  8. curr = curr -> next;
  9. cout << temp -> flightNumber << " " ;
  10. cout << temp -> departCity << " ";
  11. cout << temp -> arriveCity << " ";
  12. cout << temp -> passengers << " ";
  13. cout << temp -> totalPassengers << " " << endl;
  14. temp = curr;
  15.  
  16. }
  17.  
  18. }

also sometimes after i display a value and all the values work through... it returns a line of 0 0 0 at the end of output... how do i make that go away?

thanks.
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Jul 13th, 2005
0

Re: i really hate pointers

got it... thanks for all the help
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Is this the forum I use for help?
Next Thread in C++ Forum Timeline: Some Help with a simple C++ class.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC