In my sojourn to triumph over the dark dragon of nothingness, I have encountered an element of vast uncertainty.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
switch (user_inputted_number_of_aisles_the_type_is_int)
case 1: // 1 aisle
for (int j = 0; j < columns; j++) {
if (j == columns/2)
cout << " | ";
cout << seating[i][j];
break;
case 2:
for (int j = 0; j < columns; j++) {
if (j == columns/1.5)
cout << " | ";
if (j == columns/3)
cout << " | ";
cout << seating[i][j];
break;
//etc, etc.
many of you may realize what has occurred here. This works, somewhat well, if a person enters 15 columns and then adds 2 aisles. It starts to break the lower the columns get. So 8 columns with 2 aisles, prints 0 aisles.
I need a better mechanism wherein I can insert 'aisles' at generally even intervals. I would like to allow the user to input 0-4 aisles, as the max amount of columns allowed is twenty.
In other words, I have a pie, I need a more reliable knife.
visual:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// User enters 8 columns
: ABCDEFGH
// User chooses to have 1 'aisle'
: ABCD | EFGH
// again. User enters 12 columns
: ABCDEFGHIJKL
//User chooses 2 'aisles'
: ABCD | EFGH | IJKL
// is how this should work.
case 2:
for (int j = 0; j < columns; j++) {
if (j == columns/1.5)
cout << " | ";
if (j == columns/3)
cout << " | ";
cout << seating[i][j];
break;
There is a problem there with types. j is int , but line 11 promotes the rhs to double because of the 1.5, so the comparison is false.
The types of columns and j are int, or really should be an unsignedint type or std::size_t
There is potential for integer division = 0 on line 13, albeit pedantically if columns == 2, or worse if columns == 1, but you need to consider these cases.
What's an rhs? I did want it to transfer to a double, because I have no idea how to get this to set an aisle any other way besides force it to divide an int type.
I'm pretty sure I could use a boat load of IF statements to check how many columns the user inserted (1-20), but that is a very poor, bulky way to do it.
if they choose one aisle, it pretty much works everytime if they choose more than 1 column, just take the columns and divide by two and it places an aisle in the general center of the columns.
But the comparison with == fails when you do that: ints and double are stored in a completely different format - they don't compare.
Instead of dividing by 1.5 and having a failure of the comparison , consider multiplying by 2 and dividing by 3 - that way you stay with integer math, but check that columns is big enough first. Always validate the information you have.
So if columns is 12, the answer is 8, which is what you want for the second separator.
I'm pretty sure I could use a boat load of IF statements to check how many columns the user inserted (1-20), but that is a very poor, bulky way to do it.
Not sure what you mean by that. If the input is a string ABCDEFGHIJKL you can easily get the length of it. std::string has a size function. A C string (constchar*) has the strlen function.
Wow Cire, that is impressive. I usually try to take the concept that I get here and make it my own, but I have no idea how to improve or change that. Should I give you credit for the function in my code? It's for class; not sure about the ethics of this. But it works perfectly.