First time writing here and new to C. So I'm writing a program to calculate CPI and MIPS with a menu function with four options, one to input data, one to print that data in a table, one to calculate using the data, and one to quit the program. I'm using a switch-case loop to achieve this. Still, when the user goes to input data, the program is supposed to initialize and input the data into an array which it successfully does but when the array input loop finishes and the case breaks and goes back to the menu the array is gone from memory. Below is what I've written so far, the 2nd and 3rd cases are not completed yet so disregard them unless it's relevant. Any help would be much appreciated.
int main(int argc, const char * argv[]) {
//variables
int numOfClass = 0, machineMhz = 0, currentClass = 0, currentCPI = 0, currentCount = 0, quitFunction = 0, menuSelection;
//MENU;
while (quitFunction == 0) {
printf("Performance Assessment:\n-----------------------\n");
printf("1) Enter parameters\n");
printf("2) Print table of parameters\n");
printf("3) Print table of performance\n");
printf("4) Quit\n\n\n");
printf("Enter Selection: ");
scanf("%d", &menuSelection);
switch (menuSelection) {
case 1:{
printf("\nYou've selected \"Enter parameters\"\n");
//getting number of classes
printf("\nEnter the number of instruction classes: " );
scanf("%d", &numOfClass);
//initialize data arrays
int cpiArray[numOfClass]; //array holding CPI data
int instructionCountArray[numOfClass]; //array holding instruction count data
//getting frequency of machine in MHz
printf("\nEnter the frequency of the machine: ");
scanf("%d", &machineMhz);
//loop to get CPI and Instruction count
for (int i = 0; i < numOfClass; i++) {
printf("Enter CPI of Class %d: ", i+1);
scanf("%d", &cpiArray[i]);
printf("Enter instruction count of Class %d (millions): ", i+1);
scanf("%d", &instructionCountArray[i]);
printf("\n");
}
printf("\n\n");
break;
}
case 2: {
currentClass = 1; //class counter that increments every loop
printf("\nYou've selected \"Print table of parameters\"\n");
printf("----------------------\n");
printf("| Class | CPI | Count |\n");
do {
printf("| %5d | %3d | %4d |\n", currentClass, currentCPI, currentCount);
++currentClass;
}
while (currentClass == numOfClass);
break;
}
case 3:{
printf("\nYou've selected \"Print table of performance\"\n");
printf("function not built...sending you back to main menu...\n\n");
break;
}
case 4:{
printf("\nYou've selected \"Quit\"\n");
printf("Goodbye!\n\n");
quitFunction = 1;
break;
}
}
}
return 0;
}