Incrementing multiple times depending on value of command line argument

I have to increment the value assigned to portb depending on the value of the third command line arguement by the value of the second command line argument.

e.g. labtest -5 3
This will decrement portb by -5, 3 times

EDIT: The value had to also wrap around to 0 and continue with the operation after reaching 255, as well as wrapping around back to 255 after reaching 0

e.g.
if the increment value is 10 and was incremented 3 times and the current value of portb is 238 then the output would be

248
3
13

This is my attempt at a code below

1
2
3
4
5
6
7
8
9
10
11
        int portb = 0;
        int x = atoi(argv[1]);
	int y = atoi(argv[2]);
	
	for (y; y > 0; y--);
	{
		portb = portb + x;
		cout << "This was read from portb = " << portb << endl;
	}
	


Any help will be appreciated!
Last edited on
Is the idea to increase the value by x, y times?

portb = portb + (y*x);
Ahh I forgot to mention that the value had to wrap around to 0 once it reached a value of over 255, also that if the value reached 0, it had to wrap around back to 255
portb = (portb + (y*x)) % 256;
if (portb == 0) {portb = 255;}
Last edited on
The value outputted still seems to pass 255 when I run the code
It doesn't when I run it. So I expect your code is wrong. Sadly, I can't see your monitor, so it's going to be hard to tell you exactly what mistake you made.

https://ideone.com/Xsuzz6
Last edited on
Sorry!, I made a mistake in my initial explanation

I should have specified that the value had to wrap around to 0 and also continue incrementing from the previous operation after reaching 255

e.g.
if the increment value is 10 and was incremented 3 times and the current value of portb is 238 then the output would be

248
3
13

(vice-versa for negative numbers)


Last edited on
I should have specified that the value had to wrap around to 0 and also continue incrementing from the previous operation after reaching 255


So it has to go from 255 to 0, and also, at the same time, go from 255 to 256? This is impossible.
From 255 to 0 and from any negative number back to 255
Thanks for the help!
Topic archived. No new replies allowed.