So I have a program which simplifies a Boolean Expression. What I'm trying to achieve is that at the end of simplification of the first Expression I'd want the user to choose whether he would want to simplify another Expression or just Exit the Console Application ( The programme ).
Here's the code for the Main function
int main(int argc, char *argv[]) {
/* allow command line calling with arguments -m -b X
where X is a number. order or -m and -b X does not
matter*/
cout << "\Designed By a Student For the Students :)\n";
char choice;
do
{
cout << "\nEnter the number of variables to be Minimized\n";
cin >> m;
if (argc >= 2)
{
string arg = argv[1];
if (arg.find("-m") != -1) {
show_mid = true;
if (argc >= 3) {
arg = argv[2];
if (arg.find("-b") != -1)
MIN_BIT = atoi(argv[3]);
}
}
else if (arg.find("-h") != -1) {
cout << "-b X\tminimum bits should be X.\n"
<< "-m \tshow mid process computation.\n"
<< "-h \tshow this.\n";
return 0;
}
else {
if (arg.find("-b") != -1 && argc >= 3)
MIN_BIT = atoi(argv[2]);
if (argc >= 4) {
arg = argv[3];
if (arg.find("-m") != -1)
show_mid = true;
}
else
{
cout << "Invalid argument\n"
<< "-b X\tminimum bits should be X.\n"
<< "-m \tshow mid process computation.\n"
<< "-h \tshow this.\n";
return 0;
}
}
}
getinput();
init();
cout << "Press 'y' to Reduce Another Expression or 'n' to Close this Application";
cin >> choice;
} while (choice == 'y');
WINPAUSE;
return 0;
}
As you can see above i've used a do while Loop for the purpose, but i face two issues here i.e The programme terminates without taking user input and if I use WINPAUSE the programme exits when i PRESS ANY KEY. Is recursion an answer, please suggest a workaround.
Note: I use VS2017 IDE..:)
Edit: new code
int main(int argc, char *argv[]) {
/* allow command line calling with arguments -m -b X
where X is a number. order or -m and -b X does not
matter*/
cout << "\Designed By a Student For the Students :)\n";
char choice;
if (argc >= 2)
{
string arg = argv[1];
if (arg.find("-m") != -1) {
show_mid = true;
if (argc >= 3) {
arg = argv[2];
if (arg.find("-b") != -1)
MIN_BIT = atoi(argv[3]);
}
}
else if (arg.find("-h") != -1) {
cout << "-b X\tminimum bits should be X.\n"
<< "-m \tshow mid process computation.\n"
<< "-h \tshow this.\n";
}
else {
if (arg.find("-b") != -1 && argc >= 3)
MIN_BIT = atoi(argv[2]);
if (argc >= 4) {
arg = argv[3];
if (arg.find("-m") != -1)
show_mid = true;
}
else
{
cout << "Invalid argument\n"
<< "-b X\tminimum bits should be X.\n"
<< "-m \tshow mid process computation.\n"
<< "-h \tshow this.\n";
}
}
}
do
{
cout << "\nEnter the number of variables to be Minimized\n";
cin >> m;
getinput();
init();
cout << "Press 'y' to Reduce Another Expression or 'n' to Close this Application";
cin >> choice;
} while (choice == 'y');
WINPAUSE;
return 0;
}
edit : Here's the code for getinput() and init()
void getinput() {
unsigned in;
int num_bits = 0;
cout << "\nInput value followed by ENTER[^D ends input]\n> ";
while (cin >> in) {
input_values.push_back(in);
num_bits = count_bits(in);
if (num_bits>MIN_BIT)
MIN_BIT = num_bits;
cout << "> ";
}
}
/*return min number of bits a number is represented by. used for best output*/
unsigned count_bits(unsigned n) {
short bit = 0;
int count = 0;
while (n>0) {
bit = n % 2;
n >>= 1;
count++;
}
return count;
}
void init() {
table.resize(1);
p_group.resize(1);
final_group.resize(1);
create_table();
print_table();
create_p_group();
if (show_mid)
print_p_group();
create_final_group();
print_final_group();
}