-1

I am new to C++, and only a few hours into my lessons. I usually take what I learn and build different, yet simple, programs on the subjects. I also have a long term program that I add to and incorporate what I have learned, so I can see it working with other stuff.

While I haven't learned about functions and void. A person helped me cleaning up part of my code and introduced it to me. It is a little ahead of my lessons, but I kind of got my head around it, some what.

Anyway, I have a problem, where I need a calculation from the void function outside of it, this is fine, I add a & in the void function int& (var_name) like so. When the user enters the value and the calcualtion takes that value, everything is fine. However, the user adding a value is optional, and so if the user doesn't add a value, the variable stays unassigned, and so the compiler assigns a random value, and the end result can be anything.

Now, if I assign the int a value, the calculation I run, doesn't take the value from the function, it instead takes the value from the assigned int outside of the function, and this screws up the end result. The two user inputs I need to be calculated outside of the function are numberAnimals and reducedAnimals.

I am sure I will learn how to solve this later in my lessons, but I like to eager-beaver it, and someone said why not ask here, it has a good community eager to help.

#include <iostream>
#include <windows.h>

using std::cout;
using std::cin;
using std::string;
using std::flush;

void DropOffAnimal(string animalKind, int numberAnimals, int& newAnimals, int& reduceAnimals) {
    cout << "We currently have: " << flush;
    cout << numberAnimals << "\n";
    int totalSpace = 10;
    cout << "We have: " << totalSpace << " animal pens free\n";
    cout << "How many " << animalKind << " are you leaving today? \n";
    cin >> newAnimals;
    int overFlow = 0;
    overFlow = (newAnimals - totalSpace);
    do {
        if (newAnimals > totalSpace) {
            cout << "Please reduce your number of " << animalKind << " by " << overFlow << " please\n";
            cin >> reduceAnimals;
            totalSpace = (newAnimals - reduceAnimals);
            cout << "\n";
            cout << "Total animal pens filled: " << totalSpace << "\n";
        }
        else if (newAnimals < totalSpace) {
            break;
        }
    } while (newAnimals < totalSpace);
    cout << "Thank you!" << "\n";

}



int main() {
    //added new code from here
    const string password = "Hello";
    string inputPassword;
    int n = 0;
    int count = 0;

    do {
        cout << "Enter password > " << flush;
        cin >> inputPassword;

        if (inputPassword == password)
        {
            break;
        }
        else if (n == 2) {
            count += 1;
            cout << count << "\n";
            Sleep(3000);
            n = 0;
        }
        else {
            cout << "Access denied... " << "\n";
            n++;
        }
    }
    while (true);
    cout << "Access granted... " << "\n";

    int i = 0;
    while (i < 1) {
        //to here

        string input;
        cout << "Hello \n";
        cout << "Are you dropping off Cats or Dogs\n";
        cin >> input;


        int numberCats = 0;
        int numberDogs = 0;
        int newCats = 0;
        int newDogs = 0;
        int numberAnimals = numberCats + numberDogs + newCats + newDogs;
        int newAnimals;
        int reduceAnimals;

        if (input == "Cats") {
            DropOffAnimal("Cats", numberAnimals, newCats, reduceAnimals);
        }
        else if (input == "Dogs") {
            DropOffAnimal("Dogs", numberAnimals, newDogs, reduceAnimals);
        }
        else if (input == "Exit") {
            cout << "Thank you, Goodbye \n";
            i++;
        }
        numberCats = (numberAnimals - reduceAnimals);

        cout << "You dropped off " << (numberCats) << " Cats" << "\n" << "You dropped off " << (numberDogs) << " Dogs \n";
        cout << "________________________ \n" << "\n";
    }

    system("pause>0");
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Welcome! Your question has a lot of text and code to go through, you should try and keep your questions concise, take a read of [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Lucan May 07 '21 at 16:36
  • 1
    You should not read from an uninitialized variable. You get **undefined behavior** if you do that. One easy way around that is to initialize the variable when you define it. – Eljay May 07 '21 at 16:38
  • *'and so the compiler assigns a random value'* – technically wrong. It does not assign anything at all, the variable's value corresponds to anything that was in its memory location before. Reading uninitialised variables results in [undefined behaviour](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior), your programme renders invalid then. – Aconcagua May 07 '21 at 16:39
  • You need to assign `reduceAnimals` a value, either when it is declared or in `DropOffAnimal` before you use it (or both places). Consider: What value should be in `reduceAnimals` if `DropOffAnimal` did not need to reduce them? What if that do/while loop runs multiple times, with multiple inputs to `reduceAnimals`? – 1201ProgramAlarm May 07 '21 at 16:53
  • its a mistake I see sometimes with beginners. They say "I wrote a simple program" and what I see is not simple. When you face a problem in your code you can create a new project that has nothing but that one problem. Don't be afraid to write code just to throw it away once you understood what it does. Then you can again turn to your bigger project. Do one thing at a time. A simple example for your current issue (if I understood correctly) could look like this: https://godbolt.org/z/Y5rGT1s89 – 463035818_is_not_an_ai May 07 '21 at 17:04
  • @1201ProgramAlarm I looked at assigning the reduceAnimals in the *** } else if (newAnimals < totalSpace) { reduceAnimals = 0; break; } *** but the calculation still takes the 0 as the end result, any time I assign reduceAnimals, it will give the value as the end result. So I am at a loss. – Tipperty May 07 '21 at 17:15

1 Answers1

0

DropOffAnimal() takes in its numberAnimals parameter by value. Any new value you assign to numberAnimals inside of DropOffAnimal() will not be reflected back to the caller (unlike the newAnimals and reduceAnimals parameters, which are being taken in by reference, so they will reflect back to the caller).

main() is initializing its local numberAnimals variable to 0 just before calling DropOffAnimal(), so numberAnimals inside of DropOffAnimal() is always 0.

In main(), you have this code (in a loop, which means this code resets on every iteration):

int numberCats = 0;
int numberDogs = 0;
int newCats = 0;
int newDogs = 0;
int numberAnimals = numberCats + numberDogs + newCats + newDogs;

That means numberAnimals is initially 0. You later assign new values to numberCats, newCats, and newDogs, but no new value to numberAnimals, so it remains 0. Maybe you think that updating numberCats/numberDogs/newCats/newDogs will automatically update numberAnimals? That is not how variables work.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • So how do I get the answer to print here cout << "You dropped off " << (newCats) << " Cats" << "\n" << "You dropped off " << (numberDogs) << " Dogs \n"; If the user doesn't need to input a reducedAnimal value? – Tipperty May 07 '21 at 18:46
  • Well, for starters, you never update `numberDogs` anywhere. But `newCats` and `reduceAnimals` are output by `DropOffAnimal()` based on user input, and then you use them to calculate a new `numberCats`, which is fine. What''s the problem? That the value is being lost on the next loop iteration when your variables reset? – Remy Lebeau May 07 '21 at 18:52
  • I need to rewrite all this code, as testing a few other things, I guess the loops I added broke more than I thought, as for the dogs not getting updated. I was trying to limit my fixes to one thing at a time. Thanks for your help. I will use your info, and hopefully navigate out the problems as I build each part again I guess this is the problem with juping ahead of my lessons – Tipperty May 07 '21 at 19:45