L
L
Lavenderfilth2020-05-27 08:16:04
C++ / C#
Lavenderfilth, 2020-05-27 08:16:04

How to fix variable is being used without being initialized error?

When compiling, it constantly crashes Run-Time Check Failure #3 - The variable 'znak' is being used without being initialized.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i, j = 0, k, n = 0, r = 0, f = 0;
  char num2[30];
  char oper1[13];
  char oper2[13];
  char znak;
  int result[30], chys1[20], chys2[20];
  system("chcp 1251");
  printf("Введіть операцію: \n");
  gets_s(num2);
  for (i = 0; num2[i] != '\0'; i++)
  {
    if (num2[i] == ' ')
    {
      oper1[i] = '\0';
      znak = num2[i + 1];
      i = i + 3;
      for (i; num2[i] != ' ' && num2[i] != '\0'; i++)
      {
        oper2[j] = num2[i];
        j++;

      }
      oper2[j] = '\0';
      break;
    }
    else
    {
      oper1[i] = num2[i];
    }
  }
  while (oper1[f] != '\0') {
    f++;
  }
  for (k = 0; oper1[k] != '\0' && oper1[k] != '\0'; k++)
  {
    chys1[k] = oper1[k] - 48;
    chys2[k] = oper2[k] - 48;

  }
  for (n; n < f; n++)
  {
    if (znak == '^')
      result[n] = chys1[n] ^ chys2[n];
    if (znak == '&')
      result[n] = chys1[n] & chys2[n];
    if (znak == '|')
      (result[n] = chys1[n] | chys2[n]);
  }

  for (r; r < f; r++)
  {
    printf("%d", result[r]);
  }


  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-05-27
@NeiroNx

What is not clear in the phrase?

The variable 'znak' is being used without being initialized.

The 'znak' variable is used without initialization.
Initialization when declaring looks like this: , and not like yours - this is a declaration without initialization. You cannot access a variable until it has been assigned a value. char znak = 0;char znak;

C
CityCat4, 2020-05-27
@CityCat4

All speaks correctly - declared a variable char znak; but the values ​​were not assigned, and this is a potential source of errors and failures, especially since you are indexing on it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question