I need to write a generic menu that can be easily modified for future use. This is what I have. It works and is bug free that I can tell but is there a better way to do this. Or easier way? Please offer suggestion and example code only. I am new to c/c++ and would like to do this on my own, just need dirrection.
//Generic menu.
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
void read_data();
void write_data();
int main()
{ //start main()
//-------------------------------------- initializations ---------------------
int compare1, compare2, compare3;
const int max = 20;
char answer_str[max];
//----------------------------------------------------------------------------
while((compare1 != 0) && (compare2 != 0) && (compare3 != 0))
{ //start while loop
system("cls");
//-------------------------------------- create menu -------------------------
cout<<"1 - Enter new data."<<endl;
cout<<"2 - View existing data."<<endl;
cout<<"exit"<<endl<<endl;
//----------------------------------------------------------------------------
cin.getline(answer_str, max); //reads answer_str as a string of char data
//-------------------------------------- compare answer_str to a constant ----
compare1 = strcmp(answer_str, "1");
compare2 = strcmp(answer_str, "2");
compare3 = strcmp(answer_str, "exit");
//----------------------------------------------------------------------------
//-------------------------------------- print the result of strcmp ----------
//cout<<"compare1 = "<<compare1<<endl; for troubleshooting
//cout<<"compare2 = "<<compare2<<endl;
//cout<<"compare3 = "<<compare3<<endl;
//----------------------------------------------------------------------------
} // end while loop
//-------------------------------------- deside what to do -------------------
if(compare1 == 0) write_data();
if(compare2 == 0) read_data();
if(compare3 == 0) return 0;
//----------------------------------------------------------------------------
return 0;
} //end main
//-----------------------------------------------------------------------------
void read_data()
{
cout<<"read some data"<<endl;
system("pause");
main();
}
//-----------------------------------------------------------------------------
void write_data()
{
cout<<"write some data"<<endl;
system("pause");
main();
}
thanks,
sizzle_chest