vendredi 7 février 2020

Array is storing the wrong input in each element

I am writing a C program to store values in an array and later do calculations with them. My program was working fine until i made changes. I undid the work to the point that it was previously working but now it is not storing my numbers in the array, but instead it is storing 0s in each element.

After running the program if my input is "a.exe 1 2 3"; the output should be

Index 1: 1 
Index 2: 2
Index 3: 3

But is instead coming out to

Index 1: 0 
Index 2: 0
Index 3: 0

Code:

int main(int argc, char* argv[]) {
  //Variables for making various calculations. Names are correlated with the function of the variables.
  int i, sum = 0, mini, maxi, minTemp, maxTemp;
  float avg;

  // Creates the array to store numbers from argv[i] in
  double dubStack[20];

  //The main for loop that is used to iterate through the number of command line arguments when the program is running
  for (i=0; i <= argc; i++){

    //If the user enter "--help", the usage information is displayed
    if (strcmp(argv[1], "--help") == 0 )
    {
    usage(argv[0]);
    }

    else if(strcmp(argv[1], "-postfix") == 0 )
    {
        postfix(argv[0]);
    }

    else if(strcmp(argv[i], "+") == 0)
    {
        printf("f");
    }

    else
    {
       dubStack[i] = atof(argv[i]);

    }
    for (i=0; i < argc; i++){
        printf("Index %d: %.2f\n", i, dubStack[i]);
    }
    }


      return 0;
}

Aucun commentaire:

Enregistrer un commentaire