Converting an integer in an array to an integer

Hi, so I am trying to create a program where I can give it arguments from a batch file, which contains an x and y coordinates that will change the console cursor to that position. I can take that argument look at it, but I can use it well as it is stored in an array and I can't use an array to set the console cursor. Viewing the code in Visual Studio 2019, it doesn't seem to have any errors, but when running the code using 'Local Windows Debugger', there is some sort of exception on line 15, but not 14 (the code is below). It must be a problem with how I am trying to convert the parts of the array to integers. If anyone could help that would be greatly appreciated. Also if you need me to explain anything or expand on anything, please let me know.

Thanks,
Exro

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <Windows.h>
#include <stdlib.h>

int posX, posY;

void SetCursorPosition(int x, int y);


int main(int argc, char** argv) {

	for (int i = 1; i < argc; ++i)
	
	int posX = strtol(argv[1], nullptr, 0);
	int posY = strtol(argv[2], nullptr, 0);

	SetCursorPosition(posX, posY);
	std::cout << posX << std::endl;

	return 0;
}

void SetCursorPosition(int x, int y) {

	HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x,y };
	SetConsoleCursorPosition(output, pos);
}
1
2
3
4
for (int i = 1; i < argc; ++i)
	
	int posX = strtol(argv[1], nullptr, 0);
	int posY = strtol(argv[2], nullptr, 0);

How many of these lines belong to the for loop?

Ohhh I didn't realise that, that makes a lot of sense now. I'll fix it up and get back to you in a minute.
So I just realised that I was using the loop for some other testing and I had left it in there on accident. Thank you so much. Have a wonderful day.
At risk of sounding like a broken record - this is why you should always put braces around a block of code that's part of a loop, or a conditional statement, even when that block is a single line long.

Because when you come to add a second line to that block, there will be times when you forget to add the braces you need. Much better to put them in right from the beginning.
Topic archived. No new replies allowed.