Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
driver file
#include "dhoover42_prog4.h"
int main()
{
int zooCapacity;
int choice;
int creatures[zooCapacity];
int numCreatures;
char userChoice;



cout << "What is the maximum capacity of your magical creature zoo?";
cin >>  zooCapacity;
cin.ignore();
numCreatures = 0;

Creatures createArray[zooCapacity];

cout << "\n What would you like to do?";

cout << "\n\t1. Enter some Magical Creatures." << endl;
cout << "\t2. Delete a Magical Creature." << endl;
cout << "\t3. List/Print Creatures." << endl;
cout << "\t4. Print Creature Costs." << endl;
cout << "\t5. End Program." << endl;
cout << "\tEnter 1, 2, 3, 4, or 5." << endl;

cout << "CHOICE: ";
cin >> choice;
cin.ignore();

    if( choice < 1 || choice > 5 ) 
do{    
    {
        cout << "Your choice was invalid. Choose a number 1 through 5." << endl;
        cout << "CHOICE: ";
        cin >> choice;
        cin.ignore();
    }
}while(choice < 1 || choice > 5);

    switch (choice)
    {
    case 1:
        enterCreatures(zooCapacity,  numCreatures, choice,  userChoice, createArray);
        break;

    case 2:
        cout << "2. Delete a Magical Creature." << endl;
        break;

    case 3:
        cout << "3. List/Print Creatures." << endl;
        break;

    case 4:
        cout << "4. Print Creature Costs." << endl;
        break;
    case 5:
        cout << "5. End Program." << endl;
        break;
    }

    }

header file
#ifndef DHOOVER42_PROG4_H
#define DHOOVER42_PROG4_H

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct Cost
{
    int hoursCare;
    double hourlyCost;
    double foodCost;
    double materialCost;

};

struct Creatures
{
    string creatureName;
    string creatureDesc;
    double avgLength;
    double avgHeight;
    string creatureOrigin;
    string creatureDanger;
    double costPerMember;
    Cost cost;
};

int enterCreatures(int &zooCapacity,  int numCreatures, int choice,  char userChoice, Creatures createArray[]);
#endif

functions file
#include "dhoover42_prog4.h"

int enterCreatures(int &zooCapacity, int numCreatures, int choice,  char userChoice, Creatures createArray[])
{
cout << "What do you want to do?" << endl;
cout << "\t1. Load my creatures from a file." << endl;
cout << "\t2. Enter one creature manually." << endl;

cout << "CHOICE: ";
cin >> choice;
cin.ignore();

string inputFile, stringConvert;
ifstream inFile;
int numCreatureTotal = 0;

switch (choice)
{
case 1:
    cout << "What is the name of the file with your list of creatures? (ex: filename.txt)" << endl;
    cout << "FILENAME: ";   
    cin >> inputFile;
    inFile.open(inputFile);
    if (inFile.is_open())
    {
        while (!inFile.eof())
        {
            getline(inFile, createArray[numCreatures].creatureName, '#');
            getline(inFile, createArray[numCreatures].creatureDesc, '#');
            getline(inFile, stringConvert, '#');
            createArray[numCreatures].avgLength = stod(stringConvert);
            getline(inFile, stringConvert, '#');
            createArray[numCreatures].avgHeight = stod(stringConvert);
            getline(inFile, createArray[numCreatures].creatureOrigin, '#');
            getline(inFile, stringConvert, '#');
            createArray[numCreatures].creatureDanger = stoi(stringConvert);
            getline(inFile, stringConvert, '#');
            createArray[numCreatures].cost.hoursCare = stoi(stringConvert);
            getline(inFile, stringConvert, '#');
            createArray[numCreatures].cost.hourlyCost = stod(stringConvert);
            getline(inFile, stringConvert, '#');
            createArray[numCreatures].cost.foodCost = stod(stringConvert);
            getline(inFile, stringConvert, '#');
            createArray[numCreatures].cost.materialCost = stod(stringConvert);

            cout << "\n" << createArray[numCreatures].creatureName << " was added to the zoo";
            numCreatures++;
            numCreatureTotal++;
            cout << "\n" << numCreatureTotal << " creatures from " << inputFile << " have been added to the zoo.";
        }
        if (numCreatures >= zooCapacity)
            cout << "\nYou are now at maximum capacity of " << zooCapacity << " in your zoo.";
    }
    inFile.close();
case 2:
do{
    cout << "NAME: ";
    getline(cin, createArray[numCreatures].creatureName);
    cout << "\nDESCRIPTION: ";
    getline(cin, createArray[numCreatures].creatureDesc);
    cout << "\nAVERAGE LENGTH (in feet): ";
    cin >> createArray[numCreatures].avgLength;
    cin.ignore();
    cout << "\nAVERAGE HEIGHT (in feet): ";
    cin >> createArray[numCreatures].avgHeight;
    cin.ignore();
    cout << "\nLOCATION: ";
    getline(cin, createArray[numCreatures].creatureOrigin);
    cout << "\nIS IT A DANGEROUS CREATURE? (y or n): ";
    getline(cin, createArray[numCreatures].creatureDanger);
    cout << "\nHow many hours do you spend caring for the " << createArray[numCreatures].creatureName << " ? ";
    cin >> createArray[numCreatures].cost.hoursCare;
    cin.ignore();
    cout << "\nWhat is the cost per hour for caring for the " << createArray[numCreatures].creatureName << " ? $";
    cin >> createArray[numCreatures].cost.hourlyCost;
    cin.ignore();
    cout << "\nHow much money do you spend on food for the " << createArray[numCreatures].creatureName << " ? $";
    cin >> createArray[numCreatures].cost.foodCost;
    cin.ignore();
    cout << "\nHow much money do you spend on grooming and medical supplies for the " << createArray[numCreatures].creatureName << " ? $";
    cin >> createArray[numCreatures].cost.materialCost;
    cin.ignore();
    cout << "The " << createArray[numCreatures].creatureName<< " has been added." << endl;
    cout << "Want to add more creatures? (y or n) ";
    cin >> userChoice;
    cin.ignore();

}while(userChoice == 'Y' || userChoice == 'y');
}
return numCreatures;
}


What I have tried:

nothing yet it was working before
Posted
Updated 27-Nov-22 15:56pm
Comments
Rick York 27-Nov-22 23:30pm    
I recommend that you never, ever have a 'using' statement in a header file. If you want to shorten the expression std::string then consider a type defintion.

Quote:
nothing yet it was working before

You should have a look at the changes you made since last time it was working.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
It looks like you might want to add a while loop that surrounds the menu input. As the program stands now, it will show the menu once, process the request and then exit.

Some other observations:

The use of an inner loop (or at least it will be once you put a while loop around the entire menu) if the choice is invalid is not a good design, as you are duplicating the code to input a menu choice. You'll also have to remember to change this loop if you change the data type of choice

A header file should be very careful about using #include directives. In this case you only need to #include <string> for the definitions of your string members. And a header file should almost never include using namespace std; as this can cause unexpected results for clients that use the header. If you want to add that to the implementation file, then that's a matter of style and/or coding standards. In general, though, you will find you'll get used to adding std:: to standard library functions, objects, containers, etc fairly quickly.

You seem to be missing a break statement at then end of case 1 in your implementation file.

You're essentially duplicating your data input, once for a file read, and again for a user prompt. If you want to change the Creature class, you'll have to remember to make the changes in both places. Assuming that you're reading the object data in the same order, regardless of the source, then maybe a function that could read a single creature's data, and knows whether to show a prompt or not might be a better way to go. Such a function might have a signature that looks like
C++
bool getCreature(Creature& creature, std::istream& input, bool showprompt)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900