| | |
i really hate pointers
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
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)
#include <iostream> #include <cstdlib> using namespace std; int main () { typedef struct dataNode { char arriveCity[30]; char departCity[30]; int totalPassengers; int passengers; int flightNumber; struct dataNode *next; } dataNode; dataNode *newNode; newNode = new dataNode; int option1 = 0, option2 = 0; char searchCity [30]; search1: // for the goto statement on invalid choice option1 = 0; cout << endl; cout << "Welcome to the JHD International Airport! How can we be of assistance?" << endl; cout << "Please select the best option for you:" << endl; cout << " 1. Create a new flight record." << endl; cout << " 2. Delete an existing flight record." << endl; cout << " 3. Search for a flight." << endl; cout << " 4. End current program session." << endl; cin >> option1; cout << endl; if (option1 == 1) { flight: // start of flight creation cout << "Please input the six digit flight number (100000 - 999999): "; cin >> (*newNode).flightNumber; if (newNode -> flightNumber < 100000 || newNode -> flightNumber > 999999) { cout << "Invalid flight number reference" << endl << endl; goto flight; // restarts flight creation } cout << endl << "What is the departure city? "; cin >> (*newNode).departCity; cout << endl << "What city is the flight destination? "; cin >> (*newNode).arriveCity; cout << endl << "What is the maximum capacity for this flight? "; cin >> (*newNode).totalPassengers; cout << endl << "How many passengers currently have tickets? "; cin >> (*newNode).passengers; cout << endl; system("cls"); goto search1; //returns you to main menu } else if (option1 == 2) { // reenter: cout << "Which flight would you like to delete? "; /* cin >> ; if ( < 100000 || > 999999) { cout << "Invalid entry, please re-enter. " goto reenter; // restarts deletion } else { } */ } else if (option1 == 3) { search2: // for the goto statement on invalid choice cout << "Please select the search option of your choice:" << endl; cout << " 1. Display all flight records." << endl; cout << " 2. Display all departing flights from a city." << endl; cout << " 3. Display all open flights." << endl; cout << " 4. Go back to the main menu." << endl; cin >> option2; cout << endl; if (option2 == 1) { if (newNode != NULL) { while (newNode != NULL){ cout << newNode -> flightNumber << " " ; cout << newNode -> departCity[30] << " "; cout << newNode -> arriveCity[30] << " "; cout << newNode -> passengers << " "; cout << newNode -> totalPassengers << " " << endl; newNode = newNode -> next; } cin >> option2; } else { cout << "No flights have been entered. " << endl; goto search1; } } else if (option2 == 2) { cout << "What city do you wish to search for?" << endl; cin >> searchCity [30]; cout << endl; if (searchCity[30] == (*newNode).departCity[30]) { cout << newNode -> departCity[30] << endl; goto search1; } else { cout << "I'm sorry. We currently have no flights out of that city." << endl << endl; goto search1; // takes you back to the main menu } } else if (option2 == 3) { } else if (option2 == 4) { system("cls"); goto search1; // takes you back to the main menu } else { cout << "You have selected an invalid option. Please choose again." << endl << endl; goto search2; // restarts flight search menu } } else if (option1 == 4) { cout << "Thank you. Goodbye." << endl; goto end; // ends the program } else { cout << "You have selected an invalid option. Please choose again." << endl << endl; system("cls"); goto search1; // restarts main search menu } end: // for the goto statement to end program return 0; }
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.
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. •
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
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.
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)
typedef struct dataNode { char arriveCity[30]; char departCity[30]; int totalPassengers; int passengers; int flightNumber; struct dataNode *next; } dataNode; dataNode *newNode; newNode = new dataNode;
C++ Syntax (Toggle Plain Text)
cout << "Please input the six digit flight number (100000 - 999999): "; cin >> (*newNode).flightNumber; if (newNode -> flightNumber < 100000 || newNode -> flightNumber > 999999) { cout << "Invalid flight number reference" << endl << endl; goto flight; // restarts flight creation } cout << endl << "What is the departure city? "; cin >> (*newNode).departCity; cout << endl << "What city is the flight destination? "; cin >> (*newNode).arriveCity; cout << endl << "What is the maximum capacity for this flight? "; cin >> (*newNode).totalPassengers; cout << endl << "How many passengers currently have tickets? "; cin >> (*newNode).passengers; cout << endl;
C++ Syntax (Toggle Plain Text)
while (newNode != NULL) { if ((*newNode).passengers < (*newNode).totalPassengers) { cout << newNode -> flightNumber << " " ; cout << newNode -> departCity[30] << " "; cout << newNode -> arriveCity[30] << " "; cout << newNode -> passengers << " "; cout << newNode -> totalPassengers << " " << endl; newNode = newNode -> next;
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
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
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.
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)
typedef struct dataNode { string arriveCity; string departCity; int totalPassengers; int passengers; int flightNumber; struct dataNode *next; } dataNode; dataNode *head = 0, *tail = 0, *curr = 0; head = new dataNode; tail = head; curr = head;
C++ Syntax (Toggle Plain Text)
curr = new dataNode; tail->next = curr; tail = tail->next; cout << "Please input the six digit flight number (100000 - 999999): "; cin >> (*curr).flightNumber; if (curr -> flightNumber < 100000 || curr -> flightNumber > 999999) { cout << "Invalid flight number reference" << endl << endl; goto flight; // restarts flight creation } cout << endl << "What is the departure city? "; cin >> (*curr).departCity; cout << endl << "What city is the flight destination? "; cin >> (*curr).arriveCity; cout << endl << "What is the maximum capacity for this flight? "; cin >> (*curr).totalPassengers; cout << endl << "How many passengers currently have tickets? "; cin >> (*curr).passengers; cout << endl;
C++ Syntax (Toggle Plain Text)
while (curr != NULL) { if ((*curr).passengers < (*curr).totalPassengers) { cout << curr -> flightNumber << " " ; cout << curr -> departCity << " "; cout << curr -> arriveCity << " "; cout << curr -> passengers << " "; cout << curr -> totalPassengers << " " << endl; curr = curr -> next; } }
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.
C++ Syntax (Toggle Plain Text)
struct { string arriveCity; string departCity; int totalPassengers; int passengers; int flightNumber; } *dataNode; // ..... cout << "Please input the six digit flight number (100000 - 999999): "; cin >> dataNode->flightNumber; // .....
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
•
•
Join Date: Apr 2005
Posts: 105
Reputation:
Solved Threads: 3
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?
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.
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)
dataNode *temp = head; curr = head; if ((*curr).passengers < (*curr).totalPassengers) { while (curr != NULL && (*curr).passengers < (*curr).totalPassengers) { curr = curr -> next; cout << temp -> flightNumber << " " ; cout << temp -> departCity << " "; cout << temp -> arriveCity << " "; cout << temp -> passengers << " "; cout << temp -> totalPassengers << " " << endl; temp = curr; } }
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.
![]() |
Similar Threads
- Floating point numbers (C++)
- adding data into an char array (C++)
Other Threads in the C++ Forum
- Previous Thread: Is this the forum I use for help?
- Next Thread: Some Help with a simple C++ class.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






