Seeing as i got a few "If you have to use system..." then is there any other ways to execute a program from a variable?
The program is supposed to have a menu, with the options to add a program to the list, run a program from the list, remove a program from the list and save the list to the harddrive, all functions are yet to be implemented.
EDIT, also, here is the code (i am quite the newcomer so it is quite bulky, but it does what it should do atm).
Any tips or tricks on what could be improved are welcome.
Code:
/* A program written by Afro for users to add
and start various applications from a list */
/*
TODO list
-Switch-case main menu and sub menu if possible
-"Run the program" function
-"delete latest program" function
-"exit" function -- DONE
-"save data" function
-Replace long IF-firstprogramexist with a function if possible
-Improve layout and add screen clearing
*/
/*------------------------------------------------------------------*/
/*----------------------- Headers ----------------------------------*/
/*------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
/*------------------------------------------------------------------*/
/* ---------------------- Functions --------------------------------*/
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
/* -------------------- Menu Variables -----------------------------*/
/*------------------------------------------------------------------*/
int firstChoice;
int secondChoice;
int programnumber;
/*------------------------------------------------------------------*/
/*------------------- Program variables ----------------------------*/
/*------------------------------------------------------------------*/
/* First program */
char firstprogramname[30];
char firstprogrampath[50];
int firstprogramexist;
/* Second program */
char secondprogramname[30];
char secondprogrampath[50];
int secondprogramexist;
/*Third Program */
char thirdprogramname[30];
char thirdprogrampath[50];
int thirdprogramexist;
/*Fourth Program */
char fourthprogramname[30];
char fourthprogrampath[50];
int fourthprogramexist;
/*Fifth Program */
char fifthprogramname[30];
char fifthprogrampath[50];
int fifthprogramexist;
/*------------------------------------------------------------------*/
/* ---------------- End of the beginning ---------------------------*/
/*------------------------------------------------------------------*/
int main()
{
programnumber=1;
/* Print info about the program */
printf("This is a program-starter written by Afro\n");
printf("It has a maximum capacity of five stored programs\n");
/*------------------------------------------------------------------*/
/*----------------------- MAIN MENU --------------------------------*/
/*------------------------------------------------------------------*/
for(;;)
{
printf("\nWhat do you want to do?\n\n");
printf("Run a program (Enter 1)\n");
printf("Show the programs in the list (Enter 2)\n");
printf("Add a program to the list (enter 3)\n");
printf("Delete the latest added program (enter 4)\n");
printf("Exit (enter 5)\n\n");
firstChoice=getchar();
fflush(stdin);
/*------------------------------------------------------------------*/
/*---------------------- Execute the programs ----------------------*/
/*------------------------------------------------------------------*/
if (firstChoice==49)
{
printf("Enter the corresponding number \n");
printf("of the program you want to run.\n");
fflush(stdin);
secondChoice=getchar();
if(secondChoice==49)
{
printf("Running %s",firstprogramname);
//system("%s",firstprogrampath);
}
else if(secondChoice==50)
{
printf("Running %s",secondprogramname);
//system("");
}
else if(secondChoice==51)
{
printf("Running %s",thirdprogramname);
//system("");
}
else if(secondChoice==52)
{
printf("Running %s",fourthprogramname);
//system("");
}
else if(secondChoice==53)
{
printf("Running %s",fifthprogramname);
//system("");
}
else
{
printf("Please enter a correct number between 1-5.");
}
} //end of the run the program "if"
/*------------------------------------------------------------------*/
/*---------------------- List the programs -------------------------*/
/*------------------------------------------------------------------*/
if (firstChoice==50)
{
printf("You wish to run a program\n");
printf("These are the programs that the computer has stored:\n");
if(firstprogramexist==1)
{
printf("Number 1:\t %s \n",firstprogramname);
}
if(secondprogramexist==1)
{
printf("Number 2:\t %s \n",secondprogramname);
}
if(thirdprogramexist==1)
{
printf("Number 3:\t %s \n",thirdprogramname);
}
if(fourthprogramexist==1)
{
printf("Number 4:\t %s \n",fourthprogramname);
}
if(fifthprogramexist==1)
{
printf("Number 5:\t %s \n",fifthprogramname);
}
else printf("There are no programs in the list.\n");
} //end of the list the programs "IF"
/*------------------------------------------------------------------*/
/*------------- The adding of the five possible programs -----------*/
/*------------------------------------------------------------------*/
else if (firstChoice==51 && programnumber==1)
{
printf("You wish to add a program\n");
printf("Please enter the name of the program:\n\n");
gets(firstprogramname);
printf("\n\nPlease enter the path to the program:\n");
fflush(stdin);
gets(firstprogrampath);
printf("The program will be shown as:\n %s\n\n",firstprogramname);
printf("The path to the program is:\n %s\n\n",firstprogrampath);
programnumber++;
firstprogramexist=1;
}
else if (firstChoice==51 && programnumber==2)
{
printf("You wish to add a program\n");
printf("Please enter the name of the program:\n\n");
gets(secondprogramname);
printf("\n\nPlease enter the path to the program:\n");
fflush(stdin);
gets(secondprogrampath);
printf("The program will be shown as:\n %s\n\n",secondprogramname);
printf("The path to the program is:\n %s\n\n",secondprogrampath);
programnumber++;
secondprogramexist=1;
}
else if (firstChoice==51 && programnumber==3)
{
printf("You wish to add a program\n");
printf("Please enter the name of the program:\n\n");
gets(thirdprogramname);
printf("\n\nPlease enter the path to the program:\n");
fflush(stdin);
gets(thirdprogrampath);
printf("The program will be shown as:\n %s\n\n",thirdprogramname);
printf("The path to the program is:\n %s\n\n",thirdprogrampath);
programnumber++;
thirdprogramexist=1;
}
else if (firstChoice==51 && programnumber==4)
{
printf("You wish to add a program\n");
printf("Please enter the name of the program:\n\n");
gets(fourthprogramname);
printf("\n\nPlease enter the path to the program:\n");
fflush(stdin);
gets(fourthprogrampath);
printf("The program will be shown as:\n %s\n\n",fourthprogramname);
printf("The path to the program is:\n %s\n\n",fourthprogrampath);
programnumber++;
fourthprogramexist=1;
}
else if (firstChoice==51 && programnumber==5)
{
printf("You wish to add a program\n");
printf("Please enter the name of the program:\n\n");
gets(fifthprogramname);
printf("\n\nPlease enter the path to the program:\n");
fflush(stdin);
gets(fifthprogrampath);
printf("The program will be shown as:\n %s\n\n",fifthprogramname);
printf("The path to the program is:\n %s\n\n",fifthprogrampath);
programnumber++;
fifthprogramexist=1;
}
/* Program-adding error (too many programs)*/
else if (firstChoice==51 && programnumber>5)
{
printf("You have reached the maximum amount of programs");
}
/*------------------------------------------------------------------*/
/*----------------- END OF PROGRAM ADDING --------------------------*/
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
/*----------------- DELETE LATEST ADDED PROGRAM --------------------*/
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
/*--------------------------- EXIT ---------------------------------*/
/*------------------------------------------------------------------*/
else if (firstChoice==53)
{
break;
}
/*False Character error */
else
{
printf("Enter a correct number.\n");
}
} // End of Loop
return(0);
} // End of program