|
-
May 6th, 2007, 11:51 AM
#1
Problem with a function
I am trying to make the following work (firs the .h file followed by the problem):
The .h file:
Code:
#ifndef MYLIST_H
#define MYLIST_H
struct Air
{
string person;
Air *next;
};
class JGOp
{
public:
JGOp();// Constructor
//~JGOp();//Destructor
void InsertPassenger(); // Enters a passenger name.
private:
int size;
//static const int MAXsize = 20;
Air *head;
//string person;
string input;
};
#endif
The problem:
Code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
using std::ostream;
#include "MyList.h"
JGOp::JGOp()
{
size = 0;
head = NULL;
}
void JGOp::InsertPassenger()
{
Air *ptr = new Air;
cout << "Enter passenger: ";
cin >> input;
Add(input);
}
void JGOp::Add(string input)
{
Air *current = new Air;
Air *ptr = new Air;
Air *previous = new Air;
ptr->person = input;
if (head == NULL)
{
head = ptr->next;
}
current = head;
previous = head;
if(ptr->person < current->person)
{
current = previous->next;
while (current != NULL && previous != NULL)
{
if(ptr->person < current->person)
{
ptr->next = current;
previous->next = ptr;
}
current = current->next;
previous = previous->next;
}
}
else;
current = head;
previous = head;
current = previous->next;
while(current != NULL)
{
current = current->next;
previous = previous->next;
}
previous->next = ptr;
}
Main:
Code:
#include <iostream>
#include <string>
using namespace std;
#include "MyList.h"
int main()
{
JGOp JG;
int choice, End;
End = 1;
while (End != 0)
{
cout << "Welcome to JetGreen Airlines, the Number #1 choice for" << endl;
cout << "quality travel for Spring Break.\n"<< endl;
cout << "Press 1 to reserve a flight on one of our luxurious aircrafts." << endl;
cout << "Press 9 to exit our system." << endl << endl;
cout << "What would you like to do?: ";
cin >> choice;
cout << "\n";
switch (choice)
{
case 1:
JG.InsertPassenger();
/* Asks the user for passenger name and flight. It then adds the passenger
to the appropriate flight.*/
break;
break;
case 9:
cout << "Thank you for choosing Jet Green!" << endl << endl ;
End = 0;
}
}
return 0;
}
The point of this program is the following:
When the program loads, I want to put in a name, and have that name be saved to a string, which I can later print. The code complies, but once I run it, enter a passenger name, and press enter, I get an error. Can someone tell me what I am doing wrong here?
Last edited by Colox10; May 6th, 2007 at 03:00 PM.
-
May 6th, 2007, 12:05 PM
#2
Re: Problem with a function
Please provide enough that we can compile and test - hunting an error out by reading your code is an interesting mental exercise, but I'm not that awake yet today.
-
May 6th, 2007, 12:12 PM
#3
Re: Problem with a function
Can someone tell me what I am doing wrong here?
Two things....
1) you are not using the debugger to analyze what is going on.
2) Yoiu are posting incomplete information which makes it impossible for anyone but a mind reader to help you...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 6th, 2007, 01:54 PM
#4
Re: Problem with a function
Alright, I have edited the post. I hope it made the problem more clear. If not, let me know I'll add more details. Thanks
-
May 6th, 2007, 02:18 PM
#5
Re: Problem with a function
You should learn how to use the debugger. I would put a breakpoint in the code (right after the cin >>) then step my way through the code using F10/F11 until the application fails. Then analyze the variable etc. trying to determine what went wrong.
Remember to clean up your resources. For every new (or new[]) there should be a delete (or delete []).
- petter
-
May 6th, 2007, 02:23 PM
#6
Re: Problem with a function
I don't really know how to debug. That's why I was hoping someone could tell me what's wrong.
-
May 6th, 2007, 02:38 PM
#7
Re: Problem with a function
 Originally Posted by Colox10
I don't really know how to debug. That's why I was hoping someone could tell me what's wrong.
As the old saying goes "give a man a fish, and you feed him for a day. Teach a man to fish, and he is fed for a lifetime".
The debugger is very easy to use. Just hit F10 and single step through the program. It is a requirement for any programmer to know how to debug a program. Without this knowledge, how will you be able to write any programs and know what's wrong if something doesn't work correctly?
You won't be able to post problem after problem without a few us getting frustrated that you have not debugged the program yourself to figure out what's wrong. That's why you need to learn to fish (debug), and not have us give you the fish.
See the homework FAQ here, and read the second entry:
http://www.codeguru.com/forum/showthread.php?t=366302
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 6th, 2007 at 02:43 PM.
-
May 6th, 2007, 02:48 PM
#8
Re: Problem with a function
Well the debugging really isn't that bad, just like you said. However I still don't know what the problem is.
-
May 6th, 2007, 02:53 PM
#9
Re: Problem with a function
 Originally Posted by Colox10
Well the debugging really isn't that bad, just like you said. However I still don't know what the problem is.
1)You never stated what the error is.
2)What line of code causes the error to occur? Did you single step through the program using F10 and F11 (step into a function)? I highly doubt that inputing into a string causes an error.
3) You didn't provide a main() function that shows us how, when, and where you're using your classes.
Regards,
Paul McKenzie
-
May 6th, 2007, 03:03 PM
#10
Re: Problem with a function
First of all I want to thank you Paul for answering my posts. This is my first time ever posting on a forum, so I am not quite sure how to do it most effectively.
Also, I added the main function.
I did go through every line, and I don't think inputin into the string is the problem.
The problem is that when I run it and follow the program, after inputing the name and hitting enter, I get a pop up of Internet Explorer, saying the program had to close. I have no idea what that means.
I hope this information makes it clearer.
-
May 6th, 2007, 03:15 PM
#11
Re: Problem with a function
Code:
if (head == NULL)
{
head = ptr->next; // ptr->next isn't initialized. head now points to garbage
}
current = head; // assign head (which is garbage) to current
previous = head; // assign head (which is garbage) to previous
if(ptr->person < current->person) // crash because current is garbage
- petter
-
May 7th, 2007, 05:42 AM
#12
Re: Problem with a function
Hi
How did u even compile it
I see that Add is not a method of the class JGOp
but u define it as JGOp::Add
have you missed it ?
Regards
-
May 7th, 2007, 05:54 AM
#13
Re: Problem with a function
yes i think you fail to initialise the head struct in the constructor !!!
which leads to current and previous being junk
Code:
JGOp::JGOp()
{
size = 0;
head = NULL;
}
.....
if (head == NULL)
{
head = ptr->next;
}
current = head;
previous = head;
......
may be you meant
Code:
if (ptr == NULL)
{
head = ptr->next;
}else
{
head = ptr;
}
current = head;
previous = head;
even then your code will crash at
Code:
while(current != NULL)
{
current = current->next;
previous = previous->next;
}
-
May 7th, 2007, 06:01 AM
#14
Re: Problem with a function
 Originally Posted by Colox10
First of all I want to thank you Paul for answering my posts. This is my first time ever posting on a forum, so I am not quite sure how to do it most effectively.
Also, I added the main function.
I did go through every line, and I don't think inputin into the string is the problem.
So again, what function was called when the error occurred? If you really debugged line-by-line, you will see that the problem is in the function Add(). If you put a breakpoint in that function and single-stepped into it, you will see that things are not right, as others have pointed out.
The problem is that when I run it and follow the program, after inputing the name and hitting enter,
This is proof you are not going through the code line by line. The next function that is called after you input a value is Add(). Did you debug this function?
Regards,
Paul McKenzie
-
May 7th, 2007, 11:07 AM
#15
Re: Problem with a function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|