#why cant i move to the left in my code?

26 messages · Page 1 of 1 (latest)

undone musk
#

I made a simple code where you can move the letter "o" with w a s d through 2 vectors, and i cant move it to the left more than 1 time,can anybody help me fix this issue?

molten oasisBOT
#

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.

dim tundra
#

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>
undone musk
#

I use #include <bits/stdc++.h> brcause its easyer to remember compared to everything else

dim tundra
#

Alright, I found the actual problem lol

#

your i is not initalizied

undone musk
#

I replied to the wrong message

dim tundra
#

!f

molten oasisBOT
#

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;
    }
  }

nou
undone musk
#

What did you change in the code?

dim tundra
#

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.
undone musk
#

So i=0 was the thing that made my code work?

dim tundra
#

Just using

int i;
``` was what was wrong
undone musk
#

Int i doesnt mean that i is 0?

#

so just because "=0" was missing my code didnt work

dim tundra
#

Things will only be automatically initialized in C++ if

  1. it's a class/struct instance in which the default constructor initializes all primitive types; like MyClass instance;

  2. 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)

  3. same applies to non-aggregate classes/structs, e.g. MyClass instance = {};

  4. it's a global/extern variable

  5. the variable is defined static (no matter if inside a function or in global/namespace scope)

Got all this from Stackoverflow

undone musk
#

oh ok

#

im kind of new to cpp and i didnt know this

#

tysm for helping me

dim tundra
#

The one you used was automatic storage duration, so you always have to initialize it

dim tundra
undone musk
#

!solved