CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2012
    Posts
    5

    C++ structure Unhandled exception

    Hi guys.

    I'm getting Unhandled exception when try to run option 3.

    I don't know what I'm doing wrong. Any help will be greatly appreciated. Right now my teacher is not on vector subject. This my code below.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    struct Vet
    {
    	string name;
    	int numOfAnimals;
    	string phoneNum;
    };
    
    struct CustomerAnimal
    {
    	int anID;
    	string name;
    	double weight;
    	char agrressive;
    	string species;
    };
    
    
    //(*p) is the same as p->
    //(*p).agrressive or p->agrressive is pointing to the CustomerAnimal type
    
    
    //gets the pet information
    void getPetInfo(CustomerAnimal *p, int count = 1)
    {
    	cout << "Pet number " << count <<": "<<endl;
    
    	cout << "What is the pet ID number? ";
    	cin >> p->anID;
    
    	cout << "What is your pet name? ";
    	cin.ignore();
    	getline(cin, p->name);
    
    	cout << "What is the weight of your pet? ";
    	cin >> p->weight;
    
    
    	while (p->agrressive != 'y' && p->agrressive != 'n')
    	{
    	  cout << "Is your pet aggressive? (y/n)";
    	  cin >> p->agrressive;
    	}
    
    	cout << "What is your pet species?";
    	cin.ignore();
    	getline(cin, p->species);
    
    	cout << endl;
    }
    
    //display the pet information
    void displayPetInfo(CustomerAnimal *p)
    {
    	cout << "Pet ID" << "     " << "Pet Name" <<"     " << "Pet Weight" << "Pet Aggressive" << "     " << "Pet Species	" << endl;
    	cout << p->anID << "     " << p->name << "     " << p->weight << "     " << p->agrressive << "     " << p->species << endl;
    }
    
    //get the vet information
    void getVetInfo(Vet &d)
    {
    	cout << "What is the name of the veterinary physician working on the pet? ";
    	cin.ignore();
    	getline(cin, d.name);
    
    	cout << "What is the veterinary physician emergency number? ";
    	cin.ignore();
    	getline(cin, d.phoneNum);
    
    	cout << endl;
    }
    
    int main()
    {
    	CustomerAnimal *pet;
    	Vet animalDoc;
    	int menuOption = -1;
    	int count;
    
    	cout << "Hi and welcome to Benjamin's animal clinic.\n" << endl;
    
    
    	cout << "Before we take in your pet in, you must first check in and provide us with the folowing informarion.\n" << endl;
    
        cout << "How many pets are you bringing in today? ";
    	cin >> animalDoc.numOfAnimals;
    
    	getVetInfo(animalDoc);
    
    	pet = new CustomerAnimal[animalDoc.numOfAnimals];
    
    	while (menuOption != 5)
    	{
    
    		cout << "1. Please enter your pet/s information." << endl;
    		cout << "2. Change your veterinary physician." << endl;
    		cout << "3. List Vet and Pet information." << endl;
    		cout << "4. Quit." << endl;
    
    		cin >> menuOption;
    
    		switch (menuOption)
    		{
    			case 1:
    
    				for (count = 0; count < animalDoc.numOfAnimals; count++)
    				{
    					int petNum = count + 1;
    					getPetInfo(pet, petNum);
    					pet++;
    				}
    
    				break;
    			case 2:
    				getVetInfo(animalDoc);
    				break;
    			case 3:
    				/* 
    				for (count = 0; count < animalDoc.numOfAnimals; count++)
    				{
    				displayPetInfo(pet);
    				}
    				*/
    
    				//Just for testing purposes //Top commented out code give the same runtime error
    				// Top Code I can't figure out how to cycle through the array to show the result.
    				cout << endl << pet[0].name << pet[1].name << endl;
    
    				break;
    			default:
    
    				break;
    		}
    
    	}
    
    	delete [] pet;
    
    	system("pause");
    
    	return 0;
    
    }
    Thank You
    Benjamin Betancourt
    Last edited by crazyben21; September 10th, 2012 at 01:48 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C++ structure Unhandled exception

    Did you debug your code? It is what you had to do first.
    Besides you create your CustomerAnimal array but do not initialize CustomerAnimal members!
    Victor Nijegorodov

  3. #3
    Join Date
    Sep 2012
    Posts
    5

    Re: C++ structure Unhandled exception

    Quote Originally Posted by VictorN View Post
    Did you debug your code? It is what you had to do first.
    Besides you create your CustomerAnimal array but do not initialize CustomerAnimal members!
    Hi Victor,

    I haven't debug my code because I not that familiar with debugging yet. So I have to ask talk my teacher to show me on how to debug.

    Also I'm going start by initializing it's members to see if that will help somewhat.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C++ structure Unhandled exception

    Here's what I saw.

    I entered one pet, then it blew up on this line.
    cout << endl << pet[0].name << pet[1].name << endl;

    pet[1] is invalid since the array only has length 1.

    You have lots of other problems, but that's why it crashed. You really need to learn the debugger.
    Last edited by GCDEF; September 10th, 2012 at 09:12 AM.

  5. #5
    Join Date
    Sep 2012
    Posts
    5

    Resolved Re: C++ structure Unhandled exception

    I manage to fix my code. Now it works.

    This is the updated code. I changed a lot of stuff. But my next mission is to learn how to debug.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    struct Vet
    {
    	string name;
    	int numOfAnimals;
    	string phoneNum;
    };
    
    struct CustomerAnimal
    {
    	int anID;
    	string name;
    	double weight;
    	char agrressive;
    	string species;
    };
    
    
    //(*p) is the same as p->
    //(*p).agrressive or p->agrressive is pointing to the CustomerAnimal type
    
    
    //gets the pet information
    void getPetInfo(CustomerAnimal *p, int anNum)
    {
        for (int count = 0; count < anNum; count++)
        {
         	cout << "Pet number " << (count + 1) <<": "<<endl;
    
            cout << "What is the pet ID number? ";
            cin >> p[count].anID;
    
            cout << "What is your pet name? ";
            cin.ignore();
            getline(cin, p[count].name);
    
            cout << "What is the weight of your pet? ";
            cin >> p[count].weight;
    
    
            do
            {
                cout << "Is your pet aggressive? (y/n)";
                cin >> p[count].agrressive;
            }
            while ((p[count].agrressive) != 'y' && (p[count].agrressive) != 'n');
    
    
            cout << "What is your pet species?";
            cin.ignore();
            getline(cin, p[count].species);
    
            cout << endl;
        }
    
    
    }
    
    //display the pet information
    void displayPetInfo(CustomerAnimal *p, int anNum)
    {
    	cout << "Pet ID" << "     " << "Pet Name" <<"     " << "Pet Weight" << "Pet Aggressive" << "     " << "Pet Species	" << endl;
    
    	for (int count = 0; count < anNum; count++)
    	{
    	cout << p[count].anID << "     " << p[count].name << "     " << p[count].weight << "     " << p[count].agrressive << "     " << p[count].species << endl;
    	}
    
    	cout << endl;
    }
    
    //get the vet information
    void getVetInfo(Vet &d)
    {
    	cout << "What is the name of the veterinary physician working on the pet? ";
    	cin.ignore();
    	getline(cin, d.name);
    
    	cout << "What is the veterinary physician emergency number? ";
    	cin.ignore();
    	getline(cin, d.phoneNum);
    
    	cout << endl;
    }
    
    void displayVetInfo(Vet &d)
    {
        cout << "Vet\'s Name" << "     " <<  "Number of Animals" << "     " << "Emergency Number" << endl;
        cout << d.name << "     " << d.numOfAnimals << "     " << d.phoneNum << endl << endl;
    }
    
    int main()
    {
    	CustomerAnimal *pet;
    	Vet animalDoc;
    	int menuOption = -1;
    	int count;
    
    	cout << "Hi and welcome to Benjamin's animal clinic.\n" << endl;
    
    
    	cout << "Before we take in your pet in, you must first check in and provide us with the folowing informarion.\n" << endl;
    
        cout << "How many pets are you bringing in today? ";
    	cin >> animalDoc.numOfAnimals;
    
    	getVetInfo(animalDoc);
    
    	pet = new CustomerAnimal[animalDoc.numOfAnimals];
    
    	//initialize members
    	for (count = 0; count < animalDoc.numOfAnimals; count++)
    	{
    	    pet[count].agrressive = 'n';
    	    pet[count].anID = 0;
    	    pet[count].name = "NONE";
    	    pet[count].species = "NONE";
    	    pet[count].weight = 0;
    	}
    
    
    	while (menuOption != 4)
    	{
    
    		cout << "1. Please enter your pet/s information." << endl;
    		cout << "2. Change your veterinary physician." << endl;
    		cout << "3. List Vet and Pet information." << endl;
    		cout << "4. Quit." << endl;
    
    		cin >> menuOption;
    
    		switch (menuOption)
    		{
    			case 1:
    					getPetInfo(pet, animalDoc.numOfAnimals);
    
    				break;
    			case 2:
                    getVetInfo(animalDoc);
    				break;
    
    			case 3:
                    displayVetInfo(animalDoc);
    				displayPetInfo(pet, animalDoc.numOfAnimals);
    
    				break;
    			default:
    
    				break;
    		}
    
    	}
    
    	delete [] pet;
    
    
    	return 0;
    
    }
    Last edited by crazyben21; September 10th, 2012 at 01:50 PM.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ structure Unhandled exception

    Quote Originally Posted by crazyben21 View Post
    But my next mission is to learn how to debug.
    That should have been at the top of your list.

    I will be blunt -- you shouldn't even consider writing any computer program unless you are ready to debug such a program. Debugging is part and parcel of learning how to write programs.

    Your program consists of structs, for loops, functions, passing parameters, etc., so this is no beginner program. At this stage, it is hard to believe that no one has stressed the importance of debugging to you.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Sep 2012
    Posts
    5

    Re: C++ structure Unhandled exception

    Quote Originally Posted by Paul McKenzie View Post
    That should have been at the top of your list.

    I will be blunt -- you shouldn't even consider writing any computer program unless you are ready to debug such a program. Debugging is part and parcel of learning how to write programs.

    Your program consists of structs, for loops, functions, passing parameters, etc., so this is no beginner program. At this stage, it is hard to believe that no one has stressed the importance of debugging to you.

    Regards,

    Paul McKenzie
    The Intro in c++ teacher I had wasn't a good teacher. I took this class around 2003 and I didn't really learn c++ much with this teacher. After I finished with this intro in c++ class I mostly tought my self c++. So now, I felt confident enough to take the advance c++ course. But this teacher never talked about debugging because he also expected me to know this. I'm sure I could easily learn debugging as I am this far in c++.

    Thank You
    Benjamin Betancourt

  8. #8
    Join Date
    Aug 2009
    Posts
    440

    Re: C++ structure Unhandled exception

    I'm sure I could easily learn debugging as I am this far in c++.
    It isn't really an option. Writing C++ and debugging, while two separate things, really need to be viewed as the same entity. If you are going to write non-trivial programs, then you will have errors. To fix those errors you use the debugger. Using a debugger is not difficult at all. Basic concept is set a break point at the problem section of code, then single step through the code. In the meantime you can watch the values of variables. When these values differ from what you expect, then you know where the problem section of code lies (most likely).

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ structure Unhandled exception

    Quote Originally Posted by crazyben21 View Post
    I'm sure I could easily learn debugging as I am this far in c++.
    All languages, not only C++, requires that you know how to diagnose problems.

    No one writes bug-free programs -- did you think that when we write programs, they are 100% flawless? Of course not. Ever see the "release notes" of your favorite software? Don't some of those entries contain information on bug fixes?

    That's why debugging any program, not just C++ programs, is a requirement in learning how to program. It is such a major requirement that honestly, it not even be formally taught -- learning computer programming forces you to figure out problems when your program doesn't work correctly.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured