-5
void mult(int number2, int argc, char** argv)
{
    for (int i = 4; i < argc; i++) {
        double number3 = atof(argv[i]);
        double number2 = number2 * number3;
        cout << number2 << endl;
    }
}

my input is

./calc1 * 1 2 4 5 6

when I run this program ,output is :

2.07418e-317
4.14837e-317
1.65935e-316
8.29674e-316
4.97804e-315

ı am using gedit.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

1
double number2 = number2 * number3;

you need to initialize number2 with some value otherwise it contains a garbage value which is getting multiplied with number3

split above statement into two statements

double number2 = /*some value to initialize number2*/;
number2 *= number3;    //now perform multiplication

Additionally, number2 is being passed into function mult as an argument, and you are also declaring a new variable with the same name number2 inside mult function.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
0
double number2 = number2 * number3;

You're accessing number2 before initializing it, which causes undefined behavior.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157