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;
}