Y
Y
Yusup Nogoyev2013-11-13 21:55:08
C++ / C#
Yusup Nogoyev, 2013-11-13 21:55:08

Point out a bug to a 2-day programmer?

Hello. I'm quite new to programming, no experience.
In general, the compiler shows the error "error C4700: an uninitialized local variable "a" and also "b" was used.
I can not understand what to do, please point out the error.

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{	


  double a, b, sum, raz, umn, del;
  sum = a + b;
  raz = a - b;
  umn = a*b;
  del = a / b;

  cout << "vvedite dannie \n";
  cin >> a, b;
  if (a + b)
    cout << sum << "\n";
  else
  if (a - b)
    cout << raz << "\n";
  else
  if (a*b)
    cout << umn << "\n";
  else
  if (a / b)
    cout << del << "\n";
  else
    cout << "hernyu ne nesi, dyatel. \n";
  system("pause");
  return 0;

    
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
afiskon, 2013-11-13
@nogoyev

You are adding a and b without assigning a value to them. In C/C++, in this case, arbitrary garbage can appear in the variables. You need to write double a = 0; double b = 0;

B
brevis, 2013-11-13
@brevis

Because the question has already been answered - let me joke the old joke:

Подходит 2-х дневный программист к senior'у и показывает неработающую программу: 
- Подскажите, пожалуйста, где у меня ошибка? 
- В ДНК, - вздыхает senior. 

(no offense, just for fun)

V
Viktor Kazakov, 2013-11-13
@kasperckiy

#include <iostream>
using namespace std;

int main()
{	


    double a, b, sum, raz, umn, del;

  cout << "vvedite dannie \n";
    cin >> a;
  cin >> b;

    sum = a + b;
    raz = a - b;
    umn = a*b;
    del = a / b;

    
    if (a + b)
        cout << sum << "\n";
    else
    if (a - b)
        cout << raz << "\n";
    else
    if (a*b)
        cout << umn << "\n";
    else
    if (a / b)
        cout << del << "\n";
    else
        cout << "hernyu ne nesi, dyatel. \n";
    system("pause");
    return 0;

        
}</iostream>

as an option

O
odd2013, 2013-11-13
@odd2013

It would also be nice to add a check that b is not equal to 0. And then division by zero and all that. An error may pop up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question