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.
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.
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.
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++.
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).
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.
Bookmarks