I have to make a table and I don't want to hard code it.
COORD point = { 30, 10 };
SetConsoleCursorPosition(hConsole, point);
printf("%s", a);
COORD point1 = { 30,12 };
SetConsoleCursorPosition(hConsole, point1);
printf("%s", b);
COORD point2 = { 30,14 };
SetConsoleCursorPosition(hConsole, point2);
printf("%s", c);
COORD point3 = { 30,16 };
SetConsoleCursorPosition(hConsole, point3);
printf("%s", d);
COORD point4 = { 30,18 };
SetConsoleCursorPosition(hConsole, point4);
printf("%s", e);
COORD point5 = { 50,10 };
SetConsoleCursorPosition(hConsole, point5);
printf("%s", f);
COORD point6 = { 50,12 };
SetConsoleCursorPosition(hConsole, point6);
printf("%s", g);
COORD point7 = { 50,14 };
SetConsoleCursorPosition(hConsole, point7);
printf("%s", h);
COORD point8 = { 50,16 };
SetConsoleCursorPosition(hConsole, point8);
printf("%s", k);
COORD point9 = { 50,18 };
SetConsoleCursorPosition(hConsole, point9);
printf("%d", l);
Basically I have this and I'm interested if I can make this through for loop or something like that
It is generally not a good idea to have many variables named
point1topoint9, as this will not allow you to use them in a loop. However, if you instead have these variables in a single array, then you can use them in a loop.The same applies to the variables named
atol.This is what your code could look like if you use arrays and loops instead:
However, you probably don't need to remember the coordinates after having passed them to
SetConsoleCursurPosition. In that case, you don't need an array of coordinates, so the code can be simplified to the following:Depending on the situation, you may also prefer to use a 2D array (i.e. an array of arrays), for example like in the following code. I have changed the code to use macro constants too, because that is cleaner.
The following code should be at the start of the file, outside any function, best below the
#includedirectives of the file:Then you can use for example the following code inside the function:
Note that the placeholder strings should be changed to be made shorter, because they won't actually fit in the column, because in your example, the column only has room for 20 characters.
The code above assumes that the
COORDstructure that you are using is this one from the Windows API.In the comments section, you stated that the variables
atoldid not all have the same type. You stated that one of the variables had a different type. In that case, the situation is more complicated, and it would probably be better to handle the variable with a different type outside the loop, or to maybe convert that variable to a string before the loop, so that they can all be handled the same way.