#why cant i move to the left in my code?
26 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
your for loop goes from smallest to greatest and you're moving the o to the left when you press a
it will work once it gets to the sixth index but after that I think it won't check that position
Btw, don't use
#include <bits/stdc++.h>
use this instead
#include <iostream>
I use #include <bits/stdc++.h> brcause its easyer to remember compared to everything else
Oh
I replied to the wrong message
!f
I kinda gave it away but can you fix the code in this part
if (ch == 'a') {
system("clear");
for (int i = 0; i < 11; i++) {
if (v[i] == 'o') {
v[position] = ' ';
position--;
v[position] = 'o';
break;
} else if (v2[i] == 'o') {
v2[position] = ' ';
position--;
v2[position] = 'o';
break;
}
}
} else if (ch == 'd') {
system("clear");
for (int i = 0; i < 11; i++) {
if (v[i] == 'o') {
v[position] = ' ';
position++;
v[position] = 'o';
break;
} else if (v2[i] == 'o') {
v2[position] = ' ';
position++;
v2[position] = 'o';
break;
}
}
What did you change in the code?
Look at how the variable i in the for loops
int i;
std::cout << i;
// this will be undefined behavior because i is not initalizied or assigned a value.
So i=0 was the thing that made my code work?
Just using
int i;
``` was what was wrong
Things will only be automatically initialized in C++ if
-
it's a class/struct instance in which the default constructor initializes all primitive types; like MyClass instance;
-
you use array initializer syntax, e.g. int a[10] = {} (all zeroed) or int a[10] = {1,2}; (all zeroed except the first two items: a[0] == 1 and a[1] == 2)
-
same applies to non-aggregate classes/structs, e.g. MyClass instance = {};
-
it's a global/extern variable
-
the variable is defined static (no matter if inside a function or in global/namespace scope)
Got all this from Stackoverflow
The one you used was automatic storage duration, so you always have to initialize it
Np
!solved