How to make a piece of code run only one time?

Pages: 12
1
2
3
4
5

		const int l = strlen(pass) - 1;

		if (pass[l] == '\n')
			pass[l] = 0;


I use Windows which only uses \n for new line. Some other systems use \n\r for new line so on those, yes, you'd need to reduce the length by 2.

Ideally, you'd test the last char of the string to be either \n or \r and reduce l by 1, replace last char by 0 if it is, then test the next but last etc etc until the test isn't \n or \r. This would make it system independent.

Consider something like (not tried)

1
2
for (int l = strlen(pass) - 1; l >=0 && (pass[l] == '\n' || pass[l] == '\r'); --l)
    pass[l] = 0;

Last edited on
Topic archived. No new replies allowed.
Pages: 12