Hello Deadweight77,
Line 11 as a global variable it should be avoided. Right now you only have one function that uses the variable "wanted" and "taken" is never used.
Line 16 defines "wanted" and "taken" as local variables to "main". If they were used they would overshadow the global variables. It would take awhile and most likely someone else to see that the local variables in main are being used and not the global variables.
For the real problem on line 18. I have a feeling that you have the misconception that when using an array that you have to start at zero. This is not true. A good example is:
std::string months[12]{"Jan", ..., "Dec"};
. This sets up "Jan" as starting in element zero and there are many built in functions that can use this. But sometimes you have a program where the user (or file) will work with "Jan" as being "1" not "0". So you would write the array as:
std::string months[13]{"", "Jan", ..., "Dec"};
. Notice that the first element is a blank so that "Jan" starts in element "1".
Since your seat numbers are (1 - 5) and (6 - 10) by making the array one larger than what you did allow you to skip element zero. So when you enter a seat number like "3" the result is stored in element "3" and not element 2 as it is with your array.
I know I talked about initializing your variables, so this is what your code looks line on my computer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// <--- Above "main"
constexpr int MAXSEATS{ 11 };
char anwser = -52; // <--- Which might be the character 'ì'.
int wanted = -858993460,
taken = -858993460,
fcseats = -858993460,
counter = -858993460;
int choice = -858993460;
bool seats[MAXSEATS]
{
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true
};
|
In my IDE it is presented to me as:
[0] | true (204),
[1] | true (204),
[2] | true (204),
[3] | true (204),
[4] | true (204),
[5] | true (204),
[6] | true (204),
[7] | true (204),
[8] | true (204),
[9] | true (204),
[10] | true (204)
|
What happens here is that the compiler will set aside memory space when the variable is defined. At run time, uninitialized, the program tries to use what information is in the space for that variable.
For the "char" the information stored at that memory location is used as a number that is outside the range of a signed "char", (-128 to 127), which in this case becomes (-52).
Most often an uninitialized "int" ends up with the value of "-858993460". In the case of "counter" you pass this variable to the function and add one which would leave you with "-858993459". One less than what you started with. Not what you want.
And for the array the garbage at the memory locations tends to come out with a value of (204). With a bool normally you have two choices "false" or (0) and "true" or (1), but any value greater than zero is considered "true". So what you have here is the entire array is set to "true" before you even get started.
When you initialize the variables when defined you at least have the knowledge that they do not contain garbage.
When I did this:
1 2 3 4
|
char anwser{};
int fcseats{}, counter{};
int choice{};
bool seats[MAXSIZE]{};
|
The {}s or uniform initializer will set the "char" to "\0", the "int"s to zero and the array to (0) or false. In the case of a "double" it is "0.0". Strings and other containers like a vector are empty to start with and do not need initialized unless you need something there to begin with.
Notice in the above code piece I removed the variables "wanted" and "taken". "taken" is never used in the program anywhere and "wanted" is better defined in the function where it is used. "counter" should be replaced with the variables" fcSeats" and "coachSeats" or "fcCounter" and "coachCounter" as you will eventually need both counters. Trying to use just one "counter" for two separate things is difficult to work with. And yes eventually you will have to pass both counters to the function.
When I initialized the variables especially the array it worked as it should. When you reach the function "first" the array has the value of (0) or false for each element and it starts with every seat as available.
For now I have left "counter" alone, but it should be "fcseats".
Some hints for you:
Using "camelCase" with the second or more words capitalized is the more often used method. Some will use the old DOS underscore to denote a space between words. Or you could just use all lower case letters. Whichever method you choose be consistent. Mixing things can be confusing. And the use of the underscore as a prefix or suffix to a variable name should be left to the header files, the ones that come with the compiler not the ones you write.
Most of your code looks like this:
except the do/while loops which are:
Again be consistent. The first bit of code is much easier to read and work with when the {}s are in the same column and the code is properly indented.
It is still a good idea to verify that the input in "display" is valid before the function ends and returns. Waiting until you get to the function "first" is to late and the wrong place to check the value of choice. Also if you enter a letter for "choice" "cin" fill be in a failed state and unusable until it is reset. Going to a different function will not change this.
Loose line 11, define "wanted" in the function "first" where it is used. There is no need to pass this variable to the function and no reason to keep it in "main".
Initialize your variables in "main" and you will have a lot less problems.
Hope that helps,
Andy