Click here to Skip to main content
15,908,455 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Problem with CPropertySheet/CPropertyPage Pin
Bartosz Bien4-May-03 10:41
Bartosz Bien4-May-03 10:41 
GeneralRe: Problem with CPropertySheet/CPropertyPage Pin
Nirav Doshi6-May-03 23:23
Nirav Doshi6-May-03 23:23 
GeneralDraw Icon Pin
StuartHall3-May-03 22:00
StuartHall3-May-03 22:00 
GeneralMFC vs VB Pin
atif_3-May-03 21:38
atif_3-May-03 21:38 
GeneralRe: MFC vs VB Pin
jhaga4-May-03 0:02
professionaljhaga4-May-03 0:02 
GeneralRe: MFC vs VB Pin
Brian Shifrin4-May-03 2:43
Brian Shifrin4-May-03 2:43 
GeneralRe: MFC vs VB Pin
João Paulo Figueira4-May-03 6:54
professionalJoão Paulo Figueira4-May-03 6:54 
GeneralMenu driven console program --- professional suggestion needed. Pin
Link26003-May-03 17:17
Link26003-May-03 17:17 
//This is a console program using C++ language syntax.
//Please reply me in C++ language syntax only.
//Thank you very much.

I have written a GCD program and it is a menu driven program. Below
is a main menu. Instead of always looping back to main menu, I
have designed it so that I could always go back one step. For example,
when I press 1, the program will starts the GCD, after finishing it, I want
it to continue to run, not go back to main. If you copy the code and
run it for yourself, you will see what I mean.

My problem is, I have to put a lot of complicated control code in the
main() function, it is fine for this simple. But when I design a big
complicated program (I'm going to write a converter program), this
technique would not be good. So I wonder how a professional
programmer would design it.(remember: I don't want my program
always going back to main menu.)

main menu:
------------------------------------------------------
Welcome to the Greatest Common Divisor program.
To use the program, please enter one of the following
choices.
1. Start the Greatest Common Divisor (GDC)
2. Learn more about this program
3. Exit the program
>>
--------------------------------------------------------


press 1 and the program will start the GCD:
-------------------------------------------------------
Please enter the first number: 22
Please enter the second number: 44
The GCD is: 22.

Do you want to continue? [Y/N]
>>
---------------------------------------------------------

then if I press 1, the program will start it over again,
but not go back to menu.
----------------------------------------------------------
Please enter the first number:
----------------------------------------------------------



The code is below:
---------------------------------------------------------
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;

int main_screen();
void GCDstart();
void aboutProgram();


int main()
{
int choice = 0;
while(choice != 3)
{
choice = main_screen();

//Choice 1 to start the GCD
if(choice == 1)
{

while(choice != 'N' && choice != 'n')
{
char choice = 'N';
GCDstart();
cout << "Do you want to continue? [Y/N]" << endl;
cout << ">> ";
cin >> choice;
if(choice == 'Y' || choice == 'y')
{
system("cls");
}
else if(choice == 'N' || choice == 'n')
{
cout << "Program will go back to main menu." << endl;
getch();
system("cls");
break;
}
else
{
cout <<"Input Error:" << endl;
break;
}
}
}

//Choice 2 to learn more about the program
else if(choice == 2)
{
aboutProgram();
}

//Choice 3 to break the loop
else if(choice == 3)
{
break;
}

//Display error
else
{
cout <<"Input Error: please try again!" << endl;
getch();
system("cls");
}

}

cout << "Bye!" << endl;

return 0;
}


int main_screen()
{
//Display choices of the main menu
cout << "Welcome to the Greatest Common Divisor program." << endl
<< "To use the program, please enter one of the following" << endl
<< "choices." << endl
<< "1. Start the Greatest Common Divisor (GDC)" << endl
<< "2. Learn more about this program" << endl
<< "3. Exit the program" <<endl;

get="" user's="" choice
="" int="" choice;
="" cout="" <<="" "="">> ";
cin >> choice;
return choice;
}


///////////////////////////////////////////
//GCD MAIN FUNCTION
void GCDstart()
{
system("cls");
int num_1, num_2;
int remainder = 1, GCD;

cout <<"Please enter the first number: ";
cin >> num_1;
cout <<"Please enter the second number: ";
cin >> num_2;

num_1 = abs(num_1);
num_2 = abs(num_2);

if (num_1 >= num_2)
{

while (remainder != 0)
{
remainder = num_1 % num_2;
num_1 = num_2;
num_2 = remainder;
GCD = num_1;
}
}
else if (num_2 >= num_1)
{
while (remainder != 0)
{
remainder = num_2 % num_1;
num_2 = num_1;
num_1 = remainder;
GCD = num_2;
}
}

cout << "The GCD is: " << GCD <<"." << endl << endl;

}


///////////////////////////////////////////////
//ABOUT PROGRAM FUNCTION

void aboutProgram()
{
system("cls");
cout << "This is a GCD program. It is a program that" << endl;
cout << "finds the Greatest Common Factors. Enter two" << endl;
cout << "integers and the program will tell you the GCD." << endl;
cout << "Please press any key to continue." << endl;
getch();
system("cls");
}
GeneralRe: Menu driven console program --- professional suggestion needed. Pin
jhaga4-May-03 1:14
professionaljhaga4-May-03 1:14 
GeneralDialog application opinion needed Pin
Steve Messer3-May-03 15:51
Steve Messer3-May-03 15:51 
GeneralRe: Dialog application opinion needed Pin
jhaga4-May-03 2:17
professionaljhaga4-May-03 2:17 
GeneralRe: Dialog application opinion needed Pin
Steve Messer4-May-03 2:50
Steve Messer4-May-03 2:50 
GeneralRe: Dialog application opinion needed Pin
jhaga4-May-03 3:19
professionaljhaga4-May-03 3:19 
GeneralRe: Dialog application opinion needed Pin
Steve Messer4-May-03 3:30
Steve Messer4-May-03 3:30 
GeneralRe: Dialog application opinion needed Pin
Brian Shifrin4-May-03 2:59
Brian Shifrin4-May-03 2:59 
GeneralRe: Dialog application opinion needed Pin
Steve Messer4-May-03 3:27
Steve Messer4-May-03 3:27 
GeneralRe: Dialog application opinion needed Pin
Brian Shifrin4-May-03 23:58
Brian Shifrin4-May-03 23:58 
GeneralRe: Dialog application opinion needed Pin
Steve Messer5-May-03 5:29
Steve Messer5-May-03 5:29 
GeneralRe: Dialog application opinion needed Pin
Joan M4-May-03 22:13
professionalJoan M4-May-03 22:13 
GeneralRe: Dialog application opinion needed Pin
Steve Messer5-May-03 5:31
Steve Messer5-May-03 5:31 
GeneralRe: Dialog application opinion needed Pin
Joan M5-May-03 5:45
professionalJoan M5-May-03 5:45 
GeneralRe: Dialog application opinion needed Pin
Steve Messer5-May-03 7:45
Steve Messer5-May-03 7:45 
GeneralRe: Dialog application opinion needed Pin
Joan M5-May-03 21:09
professionalJoan M5-May-03 21:09 
GeneralRe: Dialog application opinion needed Pin
Anonymous6-May-03 11:18
Anonymous6-May-03 11:18 
GeneralRe: Dialog application opinion needed Pin
Joan M7-May-03 3:28
professionalJoan M7-May-03 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.