C++ to print to a file with cases and arrays -


ok, program has been shortened make easier read. there more case statements go along amount of variables there are. question why program error every time runs. fine when compiles not when run it.

#include <iostream> #include <fstream> #include <cmath> #include <string> using namespace std;  int main() {     string thearray[22960];     char bigfor[10];     int arrayvar = 0;     int finishedarray;     int a;     int b;     int c;     int d;     int e;     int f;     int g;     int h;     int i;     int j;      // constants      int variable1 = 0;     int variable2 = 1;     int variable3 = 2;     int variable4 = 3;     int variable5 = 4;     int variable6 = 5;     int variable7 = 6;     int variable8 = 7;     int variable9 = 8;     int variable10 = 9;         (a = 0; < 36; a++)     {         switch (a)          {         case 0:             bigfor[variable1] = '0';             break;         case 1:             bigfor[variable1] = '1';             break;         case 2:             bigfor[variable1] = '2';             break;         case 3:             bigfor[variable1] = '3';             break;         case 4:             bigfor[variable1] = '4';             break;         case 5:             bigfor[variable1] = '5';             break;         case 6:             bigfor[variable1] = '6';             break;         case 7:             bigfor[variable1] = '7';             break;         case 8:             bigfor[variable1] = '8';             break;         case 9:             bigfor[variable1] = '9';             break;         }          thearray[arrayvar] = bigfor[variable1] + bigfor[variable2] + bigfor[variable4] +                              bigfor[variable5] + bigfor[variable6] + bigfor[variable7] +                               bigfor[variable8] + bigfor[variable9] + bigfor[variable10];           arrayvar = arrayvar + 1;     }      finishedarray = arrayvar + 1;     ofstream myfile;     myfile.open("codes.txt");      (arrayvar = 0; < finishedarray; a++)     {         myfile << thearray[arrayvar] << endl;     }      myfile.close();     return 0; } 

one problem reading uninitialized values here:

thearray[arrayvar] = bigfor[variable1]+ bigfor[variable2] + bigfor[variable4] + bigfor[variable5] + bigfor[variable6] + bigfor[variable7] + bigfor[variable8] +  bigfor[variable9] + bigfor[variable10]; 

this undefined behaviour. @ point in program, bigfor[variable1] initialized.

whatever trying do, have feeling there simpler way it.


Comments