How to overcome Run-Time Check Failure #0 issue? - c++

I'm new to coding and I don't know how to fix this problem.
I get the message:
"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."
This assignment was supposed to help us get the Chinese zodiac animal.
Here is my code, it's probably not correct:
#include <iostream>
#include <assert.h>
using namespace std;
sign getZNum(int day, int month, int year)
{
int animalnum;
enum sign { rat = 0, ox, tiger, rabbit, dragon, snake, horse, goat, monkey, rooster, dog, pig };
sign animal;
//figuring out if before or after feb so that I can make sure to bump down the year to the previous year
bool beforeFeb2;
beforeFeb2 = false;
if (!month > 2) {
if (month != 2 || (month == 2 && day == 1)) {
beforeFeb2 = true;
}
}
// The first date is Feb 2, 1924.
//calculating animalnum
animalnum = ((year + beforeFeb2) - 1924) % 13;
//assigning the animal based on animalnum
switch (animalnum)
{
case 0:
animal = rat;
break;
case 1:
animal = ox;
break;
case 2:
animal = tiger;
break;
case 3:
animal = ox;
break;
case 4:
break;
animal = rabbit;
break;
case 5:
animal = dragon;
break;
case 6:
animal = snake;
break;
case 7:
animal = horse;
break;
case 8:
animal = goat;
break;
case 9:
animal = monkey;
break;
case 10:
animal = rooster;
break;
case 11:
animal = dog;
break;
case 12:
animal = pig;
break;
default: // code to be executed if n doesn't match any cases
animal = rat;
}
cout<< animal;
return animal;
}
//int getZNum(int, int , int );
int main() {
getZNum(28, 10, 2001);
return 0;
}
Any help would be appreciated!

Related

How to build the simple change maker for a vending machine?

On the menu deposits n,d, q, o,f are indicated which are indication for nickel dime quarter , one dollar and five dollar. I should also indicate "C" as in cancel option when the user hits c. But i couldn't make it work. My program still runs even the user hit c. I am confused on this point?
switch(DepositIndication) {
case 'n': {
PurchasedPrice=PurchasedPrice-0.05;
NumOfNickels=NumOfNickels+1;
}
break;
case 'd': {
PurchasedPrice=PurchasedPrice-0.10;
NumOfdimes=NumOfdimes+1;
}
break;
case 'q': {
PurchasedPrice=PurchasedPrice-0.25;
NumOfquarters=NumOfquarters+1;
}
break;
case 'o': {
PurchasedPrice=PurchasedPrice-1.00;
NumOfOnes=NumOfOnes+1;
}
break;
case 'f': {
PurchasedPrice=PurchasedPrice-5.00;
NumOfFives=NumOfFives+1;
}
break;
}
the condition c is missing and I Put the break into to brackets;
switch (DepositIndication) {
case 'n':
{
PurchasedPrice = PurchasedPrice - 0.05;
NumOfNickels = NumOfNickels + 1;
break;
}
case 'd':
{
PurchasedPrice = PurchasedPrice - 0.10;
NumOfdimes = NumOfdimes + 1;
break;
}
case 'q':
{
PurchasedPrice = PurchasedPrice - 0.25;
NumOfquarters = NumOfquarters + 1;
break;
}
case 'o':
{
PurchasedPrice = PurchasedPrice - 1.00;
NumOfOnes = NumOfOnes + 1;
break;
}
case 'f':
{
PurchasedPrice = PurchasedPrice - 5.00;
NumOfFives = NumOfFives + 1;
break;
}
case 'c':
}
//you can print cancellation messages in here
break;
}
default:
break;
}
I should also indicate "C" as in cancel option when the user hits c
Your current code works fine the only thing you are missing to make that work right now is a default case or a case where you specifically check if the user inputed C.
Example:
switch(DepositIndication) {
// All other cases ...
case 'c': {
// The user canceled the interaction
std::cout << "Interaction was canceled." << std::endl;
break;
}
// Default case to ensure that even,
// if the user hits any other key we still handle his interaction
default {
std::cout << "No fitting interactional behaviour found." << std::endl;
break;
}
}
If you want to ensure that the user can even input big lettered chars and the switch case will still work you can use std::tolower and cast it back to a char.
Make the User-Input lowercase:
// Make DepositIndication lowercase
DepositIndication = std::tolower(DepositIndication, std::locale());

'Pointer to member' error C++

I need some help with this error, I don't understand what has to be done to get rid of it. I am trying to build this code - the last exercise, Graduation. Here is my code:
// Bunnies.cpp : Defines the entry point for the console application.
// Male = 0; Female = 1
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include<stdio.h>
#include<time.h>
using namespace std;
int num;
class bunny {
int val;
int gender;
int colour;
int age;
int type;
bool radioBun;
string name;
public:
void create() {
val = num;
age = 1;
gender = rand() % 2;
switch (rand() % 100) {
case 1: radioBun = true;
break;
case 2: radioBun = true;
break;
default: radioBun = false;
break;
}
if (gender = 0) {
switch (rand() % 20) {
case 0: name = "Bob";
break;
case 1: name = "Alexis";
break;
case 2: name = "William";
break;
case 3: name = "Cleo";
break;
case 4: name = "Mark";
break;
case 5: name = "Jarod";
break;
case 6: name = "Billie";
break;
case 7: name = "Nathan";
break;
case 8: name = "Richard";
break;
case 9: name = "Thomas";
break;
case 10: name = "Rudolf";
break;
case 11: name = "Troy";
break;
case 12: name = "Wesley";
break;
case 13: name = "Jacob";
break;
case 14: name = "Cody";
break;
case 15: name = "Gavin";
break;
case 16: name = "Norris";
break;
case 17: name = "Matt";
break;
case 18: name = "Colton";
break;
case 19: name = "Daniel";
break;
}
}
else {
switch (rand() % 20) {
case 0: name = "Cecila";
break;
case 1: name = "Scarlet";
break;
case 2: name = "Abby";
break;
case 3: name = "Sandra";
break;
case 4: name = "Melissa";
break;
case 5: name = "Lizabeth";
break;
case 6: name = "Susie";
break;
case 7: name = "Cherly";
break;
case 8: name = "Kaitlin";
break;
case 9: name = "Debbie";
break;
case 10: name = "Evalyn";
break;
case 11: name = "Amalia";
break;
case 12: name = "Mendy";
break;
case 13: name = "Nora";
break;
case 14: name = "Brigitte";
break;
case 15: name = "Ebony";
break;
case 16: name = "Beatrice";
break;
case 17: name = "Tiffany";
break;
case 18: name = "Ying";
break;
case 19: name = "Kesha";
break;
}
}
}
void printCreate() {
cout << "Bunny ";
cout << val;
cout << " was zapped into existence!" << endl;
}
};
int main()
{
srand(time(0));
for (int x = 0; x == 10; x++) {
num = x;
bunny num;
num.create();
num.printCreate;
}
system("pause");
return 0;
}
I am working out a way to create 10 bunnies at the start of the code, by creating a variable num. The for loop runs 10 times, everytime increasing num by +1. Each time creating a new instance of the class bunnies and assigning the value of num to the name of that instance.
The void create() is not giving me any problems, its when I try to do void printCreate() that I get this error. It just comes up with "Severity Code Description Project File Line Suppression State
Error C3867 'bunny::printCreate': non-standard syntax; use '&' to create a pointer to member Bunnies c:\users\bob\source\repos\bunnies\bunnies\bunnies.cpp 145
". I have looked this up, but I can't understand how to fix this error. Could someone please tell me how to fix this in simple terms?
Replace num.printCreate; in your loop by num.printCreate();.

How to construct a class with variadic pointer functions as members?

I want to know whether the following is possible in C++. I need to construct a class that will store as a data member a std::map, with keys being of type std::string, and values being function pointers. The thing is that I want these function pointers to be variadic in the sense that they should point to functions accepting an arbitrary number of arguments, i.e. pointing to functions of the form
template<class... Args>
f(Args...);
The important bit is that I want to be able to have different arguments for the different function pointers in the map of a given instance of my class. For example, I might want to create an object of my class and have its map contain two pairs, one corresponding to a function having (double, int) as arguments, and another having (std::vector, int, int) as arguments. And I want to be able to make this general, in the sense that I want to be able to add new elements to the map, with possibly different argument lists (although I would only do this at compile-time, I still need to code the class without knowing about the types since I want to add the new elements from other files/clients).
What is the best way to implement this?
For all those saying you cant, you actually can, it's not pretty:
This is an example code from the output of the moc tool, that does exactly that: It stores an arbitrary amount of function / method pointers, with an arbitrary number of arguments.
Easiest Solution: Just use Qt's moc tool to generate that for you,
If you cannot or don't want to use Qt, you still can analize the code below on how they achieve it.
int AtCore::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QObject::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
if (_c == QMetaObject::InvokeMetaMethod) {
if (_id < 25)
qt_static_metacall(this, _c, _id, _a);
_id -= 25;
} else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {
if (_id < 25)
*reinterpret_cast<int*>(_a[0]) = -1;
_id -= 25;
}
return _id;
}
void AtCore::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
AtCore *_t = static_cast<AtCore *>(_o);
Q_UNUSED(_t)
switch (_id) {
case 0: _t->printProgressChanged((*reinterpret_cast< const float(*)>(_a[1]))); break;
case 1: _t->receivedMessage((*reinterpret_cast< const QByteArray(*)>(_a[1]))); break;
case 2: _t->stateChanged((*reinterpret_cast< PrinterState(*)>(_a[1]))); break;
case 3: _t->print((*reinterpret_cast< const QString(*)>(_a[1]))); break;
case 4: _t->stop(); break;
case 5: _t->pause((*reinterpret_cast< const QString(*)>(_a[1]))); break;
case 6: _t->resume(); break;
case 7: _t->home((*reinterpret_cast< uchar(*)>(_a[1]))); break;
case 8: _t->home(); break;
case 9: _t->setExtruderTemp((*reinterpret_cast< uint(*)>(_a[1])),(*reinterpret_cast< uint(*)>(_a[2]))); break;
case 10: _t->setExtruderTemp((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 11: _t->setExtruderTemp(); break;
case 12: _t->move((*reinterpret_cast< uchar(*)>(_a[1])),(*reinterpret_cast< uint(*)>(_a[2]))); break;
case 13: _t->setBedTemp((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 14: _t->setBedTemp(); break;
case 15: _t->setFanSpeed((*reinterpret_cast< uint(*)>(_a[1])),(*reinterpret_cast< uint(*)>(_a[2]))); break;
case 16: _t->setFanSpeed((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 17: _t->setFanSpeed(); break;
case 18: _t->setAbsolutePosition(); break;
case 19: _t->setRelativePosition(); break;
case 20: _t->setPrinterSpeed((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 21: _t->setPrinterSpeed(); break;
case 22: _t->setFlowRate((*reinterpret_cast< uint(*)>(_a[1]))); break;
case 23: _t->setFlowRate(); break;
case 24: _t->close(); break;
default: ;
}
} else if (_c == QMetaObject::IndexOfMethod) {
int *result = reinterpret_cast<int *>(_a[0]);
void **func = reinterpret_cast<void **>(_a[1]);
{
typedef void (AtCore::*_t)(const float & );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&AtCore::printProgressChanged)) {
*result = 0;
return;
}
}
{
typedef void (AtCore::*_t)(const QByteArray & );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&AtCore::receivedMessage)) {
*result = 1;
return;
}
}
{
typedef void (AtCore::*_t)(PrinterState );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&AtCore::stateChanged)) {
*result = 2;
return;
}
}
}
}

Test Condition for a Boost Function in c++

The boost function rowclicked refers to a pointer to a function that returns a void and takes an integer as a parameter. I am trying to pass an IF condition for this pointer, basically saying if rowclicked gets activated,I wish to assign some values
boost::function<void(int)> rowClicked;
userClass->getTable()- >rowClicked=boost::bind(&UserController::onCellTableclicked,this);
this is the If condition
if(userClass->getTable()->rowClicked)
{
std::cout<<"Row Clicked";
m=i;n=j;
}
}
What changes do I make so as to pass an IF condition for a boost function that has been binded already in the class constructor..
userClass->getTable()->rowClicked=boost::bind(&UserController::onCellTableclicked,this);
Fullcode from one of the main cpp files
:
#include "UserController.h"
// needed to bind the boost function
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include "UserClass.h"
// needed for used classes
#include <SUIButton.h>
#include <SUIProgressBar.h>
#include <SUIDialog.h>
#include <SUILabel.h>
#include <SUIGraphicsView.h>
#include <SUITableWidget.h>
// needed for the timer
#include <SUITimer.h>
//needed for time
#include <SUITime.h>
//needed for date
#include <SUIDate.h>
//needed for date time
#include <SUIDateTime.h>
//void getMonth( int,UserClass*);
//int co=1;
int cou=1;
int m,n;
// needed for output
#include <iostream>
UserController::UserController(UserClass *userClass) :
userClass(userClass), // assign the instance of userClass
timer(SUI::Timer::createTimer()), // instantiate the timer
timeTimer(SUI::Timer::createTimer()),
time(SUI::Time::createTime()),
date(SUI::Date::createDate())
{
// bind the Button clicked callback
userClass->getStartButton()->clicked = boost::bind(&UserController::onStartButtonClicked,this);
userClass->getStopButton()->clicked = boost::bind(&UserController::onStopButtonClicked,this);
// bind the ProgreimeoutssBar valueChanged callback
userClass->getProgressBar()->progressChanged = boost::bind(&UserController::onProgressBarValueChanged,this);
//std::cout<<userClass->getTable()->cellClicked;
//std::cout<<row;
userClass->getTable()->rowClicked=boost::bind(&UserController::onCellTableclicked,this);
//std::cout
userClass->getleftarrow()->clicked=boost::bind(&UserController::onLeftArrowClicked,this);
userClass->getrightarrow()->clicked=boost::bind(&UserController::onRightArrowClicked,this);
// userClass->getimvimage1()->getGraphicsScene()
// setup the timer
timer->setSingleShot(true);
timer->setInterval(100);
timeTimer->setSingleShot(false);
timeTimer->setInterval(1);
// bind the Timer timeout callback
timer->timeout = boost::bind(&UserController::onTimerTimeout,this);
timeTimer->timeout = boost::bind(&UserController::onTimeTimerTimeout,this);
//start the time
time->start();
//initialise date
date->currentDate();
userClass->getDateLabel()->setText(date->toString("dd.MM.yyyy"));
date->setDate(date->getYear(),date->getMonth(),1);
//std::cout<<co;
// std::cout<<date->getDayOfWeek();
//dislay month on calendar label
int mon=date->getMonth();
//enum month={"January"=1,"February","March","April","May","June","July","August","September","October","November","December"};
switch(mon)
{
case 1: userClass->getCalendarLabel()->setText("January");break;
case 2: userClass->getCalendarLabel()->setText("February");break;
case 3: userClass->getCalendarLabel()->setText("March");break;
case 4: userClass->getCalendarLabel()->setText("April");break;
case 5: userClass->getCalendarLabel()->setText("May");break;
case 6: userClass->getCalendarLabel()->setText("June");break;
case 7: userClass->getCalendarLabel()->setText("July");break;
case 8: userClass->getCalendarLabel()->setText("August");break;
case 9: userClass->getCalendarLabel()->setText("September");break;
case 10: userClass->getCalendarLabel()->setText("October");break;
case 11: userClass->getCalendarLabel()->setText("November");break;
case 12: userClass->getCalendarLabel()->setText("December");break;
}
//int mo=date->getMonth();
//getMonth(mo);
userClass->getyearlabel()->setText(std::to_string(date->getYear()));
int c=date->getDayOfWeek();
int count=1;
int i=1;
for(int j=c;j<=6;j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
}
for (int i=2;i<=6;i++)
{
for(int j=0;j<=6 && count<=date->getDaysInMonth();j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
if(count<=date->getDaysInMonth())
continue;
else break;
/* if(userClass->getTable()->rowClicked)
{
std::cout<<"Row Clicked";
m=i;n=j;
}*/
}
}
//userClass->getimvimage1()->getGraphicsScene(GraphicsScene)
//show the dialog
userClass->getDialog()->show();
userClass->getStopButton()->setEnabled(false);
}
UserController::~UserController()
{
delete userClass;
}
void UserController::onStopButtonClicked() {
std::cout << "Stop clicked" << std::endl;
// stop the timers
timer->stop();
timeTimer->stop();
// enable/disable the buttons
userClass->getStopButton()->setEnabled(false);
userClass->getStartButton()->setEnabled(true);
}
void UserController::onProgressBarValueChanged() {
// spam output when the ProgressBar value changed
std::cout << "ProgressBar value changed to: " << userClass->getProgressBar()->getValue() << std::endl;
}
void UserController::onTimerTimeout() {
// when the timer timed out increase the value of the ProgressBar with 1
userClass->getProgressBar()->setValue(userClass->getProgressBar()->getValue() + 1);
// if the ProgressBar value is smaller than 75 restart the timer
if (userClass->getProgressBar()->getValue() < 75)
timer->start();
}
void UserController::onTimeTimerTimeout() {
// every interval, add 1 msec
time->addMSecs(1);
timer->setSingleShot(true);
// set the label text
userClass->getTimeLabel()->setText(time->toString("HH:mm:ss.zzz"));
}
void UserController::onStartButtonClicked() {
// on clicked start the timer
std::cout << "Start clicked" << std::endl;
timer->start();
timeTimer->start();
// restart the time to current time
time->restart();
// enable/disable the buttonsuserClass->getTimeLabel()->setText(time->toString("HH:mm:ss.zzz"));
userClass->getStopButton()->setEnabled(true);
userClass->getStartButton()->setEnabled(false);
}
void UserController::onLeftArrowClicked()
{
std::cout<<"Left Arrow"<<std::endl;
for(int i=1;i<=6;i++)
{
for(int j=0;j<=6;j++)
{
userClass->getTable()->setItemText(i,j," ");
}
}
int mo=date->getMonth()-1;
int year=date->getYear();
if(mo==0)
{
year=date->getYear()-1;
mo=12;
}
date->setDate(year,mo,1);
//std::cout<<mo<<std::endl;
int c=date->getDayOfWeek();
int count=1;
int i=1;
for(int j=c;j<=6;j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
}
for (int i=2;i<=6;i++)
{
for(int j=0;j<=6 && count<=date->getDaysInMonth();j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
if(count<=date->getDaysInMonth())
continue;
else break;
}
}
switch(mo)
{
case 1: userClass->getCalendarLabel()->setText("January");break;
case 2: userClass->getCalendarLabel()->setText("February");break;
case 3: userClass->getCalendarLabel()->setText("March");break;
case 4: userClass->getCalendarLabel()->setText("April");break;
case 5: userClass->getCalendarLabel()->setText("May");break;
case 6: userClass->getCalendarLabel()->setText("June");break;
case 7: userClass->getCalendarLabel()->setText("July");break;
case 8: userClass->getCalendarLabel()->setText("August");break;
case 9: userClass->getCalendarLabel()->setText("September");break;
case 10: userClass->getCalendarLabel()->setText("October");break;
case 11: userClass->getCalendarLabel()->setText("November");break;
case 12: userClass->getCalendarLabel()->setText("December");break;
}
userClass->getyearlabel()->setText(std::to_string(date->getYear()));
}
void UserController::onRightArrowClicked()
{
std::cout<<"Right Arrow"<<std::endl;
for(int i=1;i<=6;i++)
{
for(int j=0;j<=6;j++)
{
userClass->getTable()->setItemText(i,j," ");
}
}
int mo=date->getMonth()+1;
int year=date->getYear();
if(mo==13)
{
year=date->getYear()+1;
mo=1;
}
date->setDate(year,mo,1);
date->setDate(date->getYear(),mo,1);
int c=date->getDayOfWeek();
int count=1;
int i=1;
for(int j=c;j<=6;j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
}
for (int i=2;i<=6;i++)
{
for(int j=0;j<=6 && count<=date->getDaysInMonth();j++)
{
userClass->getTable()->setItemText(i,j,std::to_string(count));
count++;
if(count<=date->getDaysInMonth())
continue;
else break;
}
}
switch(mo)
{
case 1: userClass->getCalendarLabel()->setText("January");break;
case 2: userClass->getCalendarLabel()->setText("February");break;
case 3: userClass->getCalendarLabel()->setText("March");break;
case 4: userClass->getCalendarLabel()->setText("April");break;
case 5: userClass->getCalendarLabel()->setText("May");break;
case 6: userClass->getCalendarLabel()->setText("June");break;
case 7: userClass->getCalendarLabel()->setText("July");break;
case 8: userClass->getCalendarLabel()->setText("August");break;
case 9: userClass->getCalendarLabel()->setText("September");break;
case 10: userClass->getCalendarLabel()->setText("October");break;
case 11: userClass->getCalendarLabel()->setText("November");break;
case 12: userClass->getCalendarLabel()->setText("December");break;
}
userClass->getyearlabel()->setText(std::to_string(date->getYear()));
}
/*void getMonth( int mon,UserClass *userClass)
{
switch(mon)
{
case 1: userClass->getCalendarLabel()->setText(date->toString("January"));break;
case 2: userClass->getCalendarLabel()->setText(date->toString("February"));break;
case 3: userClass->getCalendarLabel()->setText(date->toString("March"));break;
case 4: userClass->getCalendarLabel()->setText(date->toString("April"));break;
case 5: userClass->getCalendarLabel()->setText(date->toString("May"));break;
case 6: userClass->getCalendarLabel()->setText(date->toString("June"));break;
case 7: userClass->getCalendarLabel()->setText(date->toString("July"));break;
case 8: userClass->getCalendarLabel()->setText(date->toString("August"));break;
case 9: userClass->getCalendarLabel()->setText(date->toString("September"));break;
case 10: userClass->getCalendarLabel()->setText(date->toString("October"));break;
case 11: userClass->getCalendarLabel()->setText(date->toString("November"));break;
case 12: userClass->getCalendarLabel()->setText(date->toString("December"));break;
}
}*/
void UserController::onCellTableclicked() {
//date->currentDate();
//std::cout<<(userClass->getTable()->isEnabled());
// std::cout<<"Just Print:"<<cou<<std::endl;
// cou++;
// userClass->getTable()->clearFocus();
// userClass->getTable()->getCellID(int row, int col);
// int day=userClass->getTable()->getItemText(row,col);
//userClass->getTable()->toBaseWidget(this)->getWidget()->setText("Cl");
//ObjectFactory::getInstance()->toBaseWidget(this)->getWidget()->hasFocus();
// getWidget()->currentIndex().row()
//std::cout<<userClass->getTable()->rowClicked();
//std::string day=userClass->getTable()->getItemText(m,n);
userClass->getTable()->
//userClass->getTable()->currentIndex().row();
//std::cout<<getWidget()->currentIndex().row();
//date->setDate(1991,1,std::atoi(day.c_str()));
//userClass->getDateLabel()->setText(date->toString("dd.MM.yyyy"));
//userClass->getTable()->getWidget()->setText("Cl");
}

C++ Array of Struct: Functions aren't working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hey guys I'm pretty new into coding...
so my code won't run and i don't know what is wrong.
one of the errors that Im getting is that in my
void animal::petDataInfo(animal maxEntry[], int counter)
the counter is saying that it isn't being initialized...
basically i don't know whats wrong....
animal.h
#ifndef Database_animal_h
#define Database_animal_h
struct animal
{
char petName[20];
char lastName[30];
char color[12];
int month, day, year;
void petDataInfo(animal [], int &counter);
void displayInfo(animal [], int &counter);
}; animal maxEntry[100];
#endif
main.cpp
#include <iostream>
#include <string>
using namespace std;
#include "animal.h"
enum animalSpecies {Dog, Cat, Bird, Reptile, Other};
animalSpecies getAnimal();
void print(animalSpecies entered);
void menuInfo(animal [], int &counter);
void petDataInfo(animal [], int &counter);
void displayInfo(animal [], int &counter);
void quitInfo(int &counter);
int main()
{
int counter=0;
counter++;
menuInfo(maxEntry, counter);
system ("PAUSE");
return 0;
}
/********************************************************************************************************/
void menuInfo(animal maxEntry[], int& counter)
{
char letter;
do
{
cout<<"\nWhat would you like to do?"<<endl;
cout<<"(E)nter"<<endl;
cout<<"(S)earch"<<endl;
cout<<"(D)isplay"<<endl;
cout<<"(C)lear"<<endl;
cout<<"(Q)uit"<<endl;
cout<<"\n";
cout<<"Please enter a letter: "<<endl;
cin>>letter;
cout<<"\n";
cout<<"\n";
if (!((letter=='e')|| (letter=='E') || (letter=='s') || (letter=='S') || (letter=='d') || (letter=='D') || (letter=='c') || (letter=='C') || (letter=='q') || (letter=='Q')))
{
cout<<"Not a valid letter. Try again."<<endl;
cout<<"\n";
}
}
while (!((letter=='e')|| (letter=='E') || (letter=='s') || (letter=='S') || (letter=='d') || (letter=='D') || (letter=='c') || (letter=='C') || (letter=='q') || (letter=='Q')));
{
switch (letter)
{
case'e': case 'E':
petDataInfo(maxEntry, counter);
if ((letter=='e') || (letter=='E'))
{
cout<<"Entry number: "<<counter++;
}
break;
case 's': case 'S':
cout<<"goes to search database"<<endl;
break;
case 'd': case 'D':
displayInfo(maxEntry, counter);
break;
case 'c': case 'C':
if ((letter=='c') || (letter=='C'))
{
counter--;
}
break;
case 'q': case 'Q':
quitInfo(counter);
break;
default:
cout<<"Try again. \n";
}
}
}
/********************************************************************************************************/
void animal::petDataInfo(animal maxEntry[], int counter)
{
for(int i=0; i<counter; i++)
{
cout<<"What is the pets first name? "<<endl;
cin>>maxEntry[i].petName;
cout<<"What is the owners last name? "<<endl;
cin>>maxEntry[i].lastName;
getAnimal();
cout<<"What month is your pets date of birth? "<<endl;
cin>>maxEntry[i].month;
cout<<"What day is your pets date of birth? "<<endl;
cin>>maxEntry[i].day;
cout<<"What year is your pets date of birth? "<<endl;
cin>>maxEntry[i].year;
cout<<"What color is your pet? "<<endl;
cin>>maxEntry[i].color;
}
menuInfo(maxEntry, counter);
}
/********************************************************************************************************/
animalSpecies getAnimal()
{
animalSpecies entered = Dog;
char species;
cout<<"Species of Animal:\n (D)og\n (C)at\n (B)ird\n (R)eptile\n (O)ther\n "<<endl;
cin>>species;
switch (species)
{
case 'd':
case 'D':
entered = Dog;
break;
case 'c':
case 'C':
entered = Cat;
break;
case 'b':
case 'B':
entered = Bird;
break;
case 'r':
case 'R':
entered = Reptile;
break;
case 'o':
case 'O':
entered = Other;
break;
default:
cout<<"Error: Try again. "<<endl;
}
return entered;
}
/********************************************************************************************************/
void print(animalSpecies entered)
{
switch (entered)
{
case Dog:
cout<<"Dog"<<endl;
break;
case Cat:
cout<<"Cat"<<endl;
break;
case Bird:
cout<<"Bird"<<endl;
break;
case Reptile:
cout<<"Reptile"<<endl;
break;
case Other:
cout<<"Other"<<endl;
break;
}
}
/********************************************************************************************************/
void animal::displayInfo(animal maxEntry[], counter)
{
for(int i=0; i<100; i++)
{
cout<<"Pet name: "<<maxEntry[i].petName<<"\n";
cout<<"Owners last name: "<<maxEntry[i].lastName<<"\n";
cout<<"Pet species: ";
animalSpecies entered;
entered = getAnimal();
print(entered);
cout<<"Pets DOB: ";
if (month<10)
{
cout<<"0"<<maxEntry[i].month<<"/"; //if month is less than 10 it will add a '0' in front
}
else
{
cout<<maxEntry[i].month<<"/";
}
if (day<10)
{
cout<<"0"<<maxEntry[i].day<<"/";
}
else
{
cout<<maxEntry[i].day<<"/";
}
cout<<maxEntry[i].year<<"\n";
menuInfo(maxEntry, counter); //goes back to the menu
}
}
/********************************************************************************************************/
void quitInfo(int& counter)
{
int ans;
do
{
cout<<"Are you sure you want to quit? (y/n)"<<endl;
cin>>ans;
if ((ans=='n') || (ans=='N'))
{
menuInfo(maxEntry, counter); //goes back to the menu function
}
else if ((ans=='y') || (ans=='Y'))
{
exit(0); //exits the program
}
else
{
cout<<"Error: Try again. "<<endl;
}
}
while (!(ans=='n') || !(ans=='N') || !(ans=='y') || !(ans=='Y'));
}
Issues I found in your code:
You have declared petDataInfo and displayInfo in two places, once inside the definition of animal and once in main.cpp. Looking at your code, I don't think you need them inside animal.
You are missing the type of counter in the definition of displayInfo.
void animal::displayInfo(animal maxEntry[], counter)
You need to add the type in that line. Also, since the function is not a member function of animal, it can be written as:
void displayInfo(animal maxEntry[], int& counter)
The type of counter is different in the declaration and the definition of petDataInfo. I the declaration, you are using int& counter. In the definition, you are using int counter. Make sure that they match. Also, the class scope can be removed from the definition since it's no longer a member function of animal.
void petDataInfo(animal maxEntry[], int& counter)
You are missing the object when trying to access the month and day. Change the line
if (month<10)
to
if (maxEntry[i].month<10)
and change
if (day<10)
to
if (maxEntry[i].day<10)
I haven't verified the logic of your program. If there are any, hopefully you can track and fix them.