T
T
tostr0072018-03-16 18:23:50
C++ / C#
tostr007, 2018-03-16 18:23:50

I get an error Error C4700 uninitialized local variable used how to fix?

#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#pragma warning(disable : 4996)

// do when win
double winWay(double a, double b)
{
  return a + b;
}
// do when lose
double loseWay(double a, double b)
{
  return a - b;
}
// randomizer
double dr(double zb)
{
  return zb = (rand() % 100) / (100 * 1.0);
}

// Program
void main()
{
  printf("make a stake 100 on 1, or 0, your money 1000");
  double a, b, e, d;
    scanf("%lf", &e);
    a = 1000;
    b = 100;
    printf("%lf", dr(double zb));
    if (d == e)
    {
      printf("Win! your cash:%lf", winWay(a, b));
      double a = winWay(a, b);
    }
    else
    {
      printf("Lose your cash:%lf", loseWay(a, b));
      double a = loseWay(a, b);
    }
  system("pause");
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2018-03-16
@tostr007

#include "stdio.h"
#include "stdlib.h"

It is better to write it in angle brackets.
double dr(double zb)
{
    return zb = (rand() % 100) / (100 * 1.0);
}

Are we assigning something to the zb parameter to screw it up? The parameter will live until the end of the function and WILL NOT RETURN to the calling program (parameters are passed by copy).
By the way, the zb parameter is not used for anything: whatever you pass there from the outside, the subroutine will not look into this parameter.
It didn't compile in G++. Well, I wrote dr(0), since the parameter is not used for anything.
if (d == e)
{
    printf("Win! your cash:%lf", winWay(a, b));

C:\TestApps\ErrUninitLocal\main.cpp|30|warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]|
Indeed, the variable d is not assigned to anyone anywhere.
One more thing. I don't know how Visual C works, but the l modifier for the %f format is needed for scanf, but not needed for printf. This is how the whole thing is set up.
double a = winWay(a, b);
}

What are we doing? Assign to a local variable whose name overlaps with an earlier variable so that we can screw it up right away? It will live to the nearest closed parenthesis.
C:\TestApps\ErrUninitLocal\main.cpp|33|warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]|
It's actually fun to work here. double a— we consider that a has been determined. = winWay(a, b) - we believe that SHE is used, and not the earlier a. Naturally, uninitialized.
(similar to loseway)
C:\TestApps\ErrUninitLocal\main.cpp|38|warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]|

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question