Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Help. Trying to get my program to loop back to main menu on demand?
New Posts  All Forums:Forum Nav:

C++ Help. Trying to get my program to loop back to main menu on demand?

post #1 of 10
Thread Starter 
I want to go back to the beginning of int main() when the user requests to go back to main menu. For some reason, when I call int main(), the program ends.

Here is the PROG:
Code:
#include <iostream>
using namespace std;

int main()
{
        double a(0), b(0), c(0); int option(0);
        const char QUIT ('Q');
        void getNumbers(double& a, double& b); 
        void getNumber(double& a); 
        void temps(double& a, double& b, int& option);
        void measurements(double& a, double& b, int& option);
        void calc(double& a, double& b, int& option);
        void currency(double& a, double& b, int& option);

        cout << "Welcome\n";
        cout << "Please select an option below\n\n";
        cout << "1. Convert Temperatures\n";
        cout << "2. Convert Measurements\n";
        cout << "3. Basic Calculator\n";
        cout << "4. Currency Converter (Updated as of April 2014)\n"; //NOT IMPLEMENTED YET
        cout << "Press Q to quit\n";
        cin >> option;
        
        if (option == 1)
                temps(a, b, option);
        if (option == 2)
                measurements(a, b, option);
        if (option == 3)
                calc(a, b, option);     
}


void getNumbers(double& a, double& b)
{
        cout << "Input First Number\n";
        cin >> a; 
        cout << "Input Second Number\n";
        cin >> b;
}

void getNumber(double& a)
{
        cout << "Enter a value \n";
        cin >> a;
}

void temps(double& a, double& b, int& option)
{
        void CtoF(double& a, double& b);
        void FtoC(double& a, double& b);
        
        cout << "Please select an option below\n\n";
        cout << "1: Convert celcius to farenheit\n";
        cout << "2: Convert farenheit to celcius\n";
        cout << "3: Main Menu\n";
        cin >> option;

        if (option == 1)
        {
                getNumber(a);
                CtoF(a, b);
                cout << a << " Celcius = " << b << " Farenhiet\n";
                temps (a, b, option);
        }
        if (option == 2)
        {
                getNumber(a);
                FtoC(a, b);
                cout << a << " Farenhiet = " << b << " Celcius \n";
                temps (a, b, option);
        }
        if (option == 3)
        {
                int main(); //NOT WORKING!
        }
}

void CtoF(double& a, double& b)
{
        b = (((a * 9) / 5) + 32); 
}

void FtoC(double& a, double& b)
{
        b = (((a - 32) * 5) / 9);
}

void measurements(double& a, double& b, int& option)
{
        void mmTocm (double& a, double& b);
        void cmTomm (double& a, double& b);
        void cmToinches (double& a, double& b);
        void inchesTocm (double& a, double& b);
        void metersTomiles (double& a, double& b);
        void milesTometers (double& a, double& b);
        void metersToyards (double& a, double& b);
        void yardsTometers (double& a, double& b);
        void circleCircumferece (double& a, double& b);
        void circleArea (double& a, double& b);
        void areaPerimeterOfSquareRectange (double& a, double& b);

        cout << "Please select an option below\n\n";
        cout << "1:  Convert mm to cm\n";
        cout << "2:  Convert cm to mm\n";
        cout << "3:  Convert cm to Inches\n";
        cout << "4:  Convert Inches to cm\n";
        cout << "5:  Convert Meters to Miles\n";
        cout << "6:  Convert Miles to Meters\n";
        cout << "7:  Convert Meters to Yards\n";
        cout << "8:  Convert Yards to Meters\n";
        cout << "9:  Calculate Circumference (cm) of a Circle\n";
        cout << "10: Calculate Area (cm2) of a Circle\n";
        cout << "11: Calculate Area (cm2) and Perimeter (cm) of a Square/Rectangle\n";
        cout << "12: Main Menu\n";
        cin >> option;

        if (option == 1)
        {
                getNumber(a);
                mmTocm(a, b);
                cout << a << "mm/s = " << b <<"cm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 2)
        {
                getNumber(a);
                cmTomm(a, b);
                cout << a << " cm/s = " << b <<" mm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 3)
        {
                getNumber(a);
                cmToinches(a, b);
                cout << a << " cm/s = " << b <<" Inche/s\n\n";
                measurements( a, b, option);
        }
        if (option == 4)
        {
                getNumber(a);
                inchesTocm(a, b);
                cout << a << " Inche/s = " << b <<" cm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 5)
        {
                getNumber(a);
                metersTomiles(a, b);
                cout << a << " Meter/s = " << b <<" Mile/s\n\n";
                measurements( a, b, option);
        }
        if (option == 6)
        {
                getNumber(a);
                milesTometers(a, b);
                cout << a << " Mile/s = " << b <<" Meters/s\n\n";
                measurements( a, b, option);
        }
        if (option == 7)
        {
                getNumber(a);
                metersToyards(a, b);
                cout << a << " Meter/s = " << b <<" Yard/s\n\n";
                measurements( a, b, option);
        }
        if (option == 8)
        {
                getNumber(a);
                yardsTometers(a, b);
                cout << a << " Yard/s = " << b <<" Meter/s\n\n";
                measurements( a, b, option);
        }
        if (option == 9)
        {
                circleCircumferece(a, b);
                cout << "Circumference (cm/s) of the Circle = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 10)
        {
                circleArea(a, b);
                cout << "Area (cm2) of the Circle = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 11)
        {
                areaPerimeterOfSquareRectange (a, b);
                cout << "Area (cm2) = " << a << " Perimeter (cm) = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 12)
                int main(); //NOT WORKING!
}

void mmTocm (double& a, double& b)
{
        b = (a / 10);
}
void cmTomm (double& a, double& b)
{
        b = (a * 10);
}
void cmToinches (double& a, double& b)
{
        b = (a / 2.50);
}
void inchesTocm (double& a, double& b)
{
        b = (a * 2.50);
}
void metersTomiles (double& a, double& b)
{
        b = (a / 0.000621371192);
}
void milesTometers (double& a, double& b)
{
        b = (a * 1609.344);
}
void metersToyards (double& a, double& b)
{
        b = (a * 1.0936133);
}
void yardsTometers (double& a, double& b)
{
        b = (a / 0.9144);
}
void circleCircumferece (double& a, double& b)
{
        double const PI = 3.14;
        cout << "Enter Diameter (cm/s)\n";
        cin >> a;
        b = (a * PI);
}
void circleArea (double& a, double& b)
{
        double const PI = 3.14;
        cout << "Enter Diameter (cm/s)\n";
        cin >> a;
        b = (a / 2); 
        b = (b * b * PI);       
}
void areaPerimeterOfSquareRectange (double& a, double& b)
{
        double temp(0);
        cout << "Enter Lenght (cm/s)\n";
        cin >> a;
        cout << "Enter Width (cm/s)\n";
        cin >> b;
        temp = a;
        a = (a * b);
        b = (temp * 2) + (b * 2);
}
void calc(double& a, double& b, int& option)
{
        int Addition(double& a, double& b);
        int Subtraction(double& a, double& b);
        int Multiplication(double& a, double& b);
        int Division(double& a, double& b);

        cout << "Please select an option below\n\n";
        cout << "1: Addition\n";
        cout << "2: Subtraction\n";
        cout << "3: Multiplication\n";
        cout << "4: Division\n";
        cout << "5: Main Menu\n";
        cin >> option;

         if (option == 1)
         {
                 getNumbers(a, b);
                 cout << a << " + " << b << " = " << Addition(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 2)
         {
                 getNumbers(a, b);
                 cout << a << " - " << b << " = " << Subtraction(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 3)
         {
                 getNumbers(a, b);
                 cout << a << " x " << b << " = " << Multiplication(a, b) << "\n\n";
                 calc (a, b, option);

         }
         if (option == 4)
         {
                 cout << a << " / " << b << " = " << Division(a, b) << "\n\n";
                 getNumbers(a, b);
                 calc (a, b, option);
         }
         if (option == 5)
         {
                 int main(); //NOT WORKING!
         }
}

int Addition(double& a, double& b)
{
        return a + b;
}
int Subtraction(double& a, double& b)
{
        return a - b;
}
int Multiplication(double& a, double& b)
{
        return a * b;
}
int Division(double& a, double& b)
{
        return a / b;
}

Thank you!
Edit: Ignore the error on the Division function (Calc) wink.gif
Edited by BradleyW - 4/11/14 at 4:33pm
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #2 of 10
I cannot write out all the code for you right now, but basically, you'll need to put your program logic into a while(condition) loop so that it can continue to run as long as the Quit option isn't selected.

I would start the loop right before the first cout statement, and close it right before the end of main(). Define a bool variable quit to false outside the loop, start the loop with while(!quit), and set quit to true when the user presses "Q" when prompted for choosing an option.

Also, you should never need to call main() inside of anything, especially itself. The main function always gets called first in C++ and is the entry point to your entire program. And it's a good practice to not define the std namespace and just use it explicitly
post #3 of 10
Thread Starter 
Quote:
Originally Posted by ccRicers View Post

I cannot write out all the code for you right now, but basically, you'll need to put your program logic into a while(condition) loop so that it can continue to run as long as the Quit option isn't selected.

I would start the loop right before the first cout statement, and close it right before the end of main(). Define a bool variable quit to false outside the loop, start the loop with while(!quit), and set quit to true when the user presses "Q" when prompted for choosing an option.

Also, you should never need to call main() inside of anything, especially itself. The main function always gets called first in C++ and is the entry point to your entire program. And it's a good practice to not define the std namespace and just use it explicitly

Thanks a lot. I will give this a try. smile.gif
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #4 of 10
Thread Starter 
Right, the only issue I face is getting back to the start of the program now. take int Main () for instance. It gives options. If I select an option, it calls another procedure with even more options. the issue is, I can't get back to the main menu on demand when I arrive at the other menus. Maybe I'm not implementing the WHILE correctly smile.gif
Thank you!

Edit: Got it working now, thank you. Decided to implement the while loop a different way smile.gif
Code:
//Written By Bradley Worthington - Copyright Protected
#include <iostream>
using namespace std;
int main()
{
        double a(0), b(0); int option(0);
        void getNumbers(double& a, double& b); 
        void getNumber(double& a); 
        void temps(double& a, double& b, int& option);
        void measurements(double& a, double& b, int& option);
        void calc(double& a, double& b, int& option);
        void currency(double& a, double& b, int& option);

        cout << "Main Menu\n\n";
        cout << "Please select an option below\n\n";
        cout << "1. Convert Temperatures\n";
        cout << "2. Convert Measurements\n";
        cout << "3. Basic Calculator\n";
        cout << "4. Currency Converter (Updated as of April 2014)\n"; 
        cout << "5. Quit\n";
        cin >> option;

        while ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }

        if (option == 1)
                temps(a, b, option);
        if (option == 2)
                measurements(a, b, option);
        if (option == 3)
                calc(a, b, option);     
        if (option == 4)
                currency(a, b, option);
        if (option == 5) 
                system ("pause");
}

void getNumbers(double& a, double& b)
{
        cout << "Input First Number\n";
        cin >> a; 
        cout << "Input Second Number\n";
        cin >> b;
}

void getNumber(double& a)
{
        cout << "Enter a value \n";
        cin >> a;
}

void temps(double& a, double& b, int& option)
{
        void CtoF(double& a, double& b);
        void FtoC(double& a, double& b);
        
        cout << "Please select an option below\n\n";
        cout << "1: Convert celcius to farenheit\n";
        cout << "2: Convert farenheit to celcius\n";
        cout << "3: Main Menu\n";
        cin >> option;
        while ((option != 1) && (option != 2) && (option != 3))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }
        if (option == 1)
        {
                getNumber(a);
                CtoF(a, b);
                cout << a << " Celcius = " << b << " Farenhiet\n\n";
                temps (a, b, option);
        }
        if (option == 2)
        {
                getNumber(a);
                FtoC(a, b);
                cout << a << " Farenhiet = " << b << " Celcius \n\n";
                temps (a, b, option);
        }
        else 
                option = 0;
                main();
}

void CtoF(double& a, double& b)
{
        b = (((a * 9) / 5) + 32); 
}

void FtoC(double& a, double& b)
{
        b = (((a - 32) * 5) / 9);
}

void measurements(double& a, double& b, int& option)
{
        void mmTocm (double& a, double& b);
        void cmTomm (double& a, double& b);
        void cmToinches (double& a, double& b);
        void inchesTocm (double& a, double& b);
        void metersTomiles (double& a, double& b);
        void milesTometers (double& a, double& b);
        void metersToyards (double& a, double& b);
        void yardsTometers (double& a, double& b);
        void circleCircumferece (double& a, double& b);
        void circleArea (double& a, double& b);
        void areaPerimeterOfSquareRectange (double& a, double& b);

        cout << "Please select an option below\n\n";
        cout << "1:  Convert mm to cm\n";
        cout << "2:  Convert cm to mm\n";
        cout << "3:  Convert cm to Inches\n";
        cout << "4:  Convert Inches to cm\n";
        cout << "5:  Convert Meters to Miles\n";
        cout << "6:  Convert Miles to Meters\n";
        cout << "7:  Convert Meters to Yards\n";
        cout << "8:  Convert Yards to Meters\n";
        cout << "9:  Calculate Circumference (cm) of a Circle\n";
        cout << "10: Calculate Area (cm2) of a Circle\n";
        cout << "11: Calculate Area (cm2) and Perimeter (cm) of a Square/Rectangle\n";
        cout << "12: Main Menu\n";
        cin >> option;

        while ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5) && (option != 6) && (option != 7) && (option != 8) && (option != 9) && (option != 10) && (option != 11) && (option != 12))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }

        if (option == 1)
        {
                getNumber(a);
                mmTocm(a, b);
                cout << a << "mm/s = " << b <<"cm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 2)
        {
                getNumber(a);
                cmTomm(a, b);
                cout << a << " cm/s = " << b <<" mm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 3)
        {
                getNumber(a);
                cmToinches(a, b);
                cout << a << " cm/s = " << b <<" Inche/s\n\n";
                measurements( a, b, option);
        }
        if (option == 4)
        {
                getNumber(a);
                inchesTocm(a, b);
                cout << a << " Inche/s = " << b <<" cm/s\n\n";
                measurements( a, b, option);
        }
        if (option == 5)
        {
                getNumber(a);
                metersTomiles(a, b);
                cout << a << " Meter/s = " << b <<" Mile/s\n\n";
                measurements( a, b, option);
        }
        if (option == 6)
        {
                getNumber(a);
                milesTometers(a, b);
                cout << a << " Mile/s = " << b <<" Meters/s\n\n";
                measurements( a, b, option);
        }
        if (option == 7)
        {
                getNumber(a);
                metersToyards(a, b);
                cout << a << " Meter/s = " << b <<" Yard/s\n\n";
                measurements( a, b, option);
        }
        if (option == 8)
        {
                getNumber(a);
                yardsTometers(a, b);
                cout << a << " Yard/s = " << b <<" Meter/s\n\n";
                measurements( a, b, option);
        }
        if (option == 9)
        {
                circleCircumferece(a, b);
                cout << "Circumference (cm/s) of the Circle = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 10)
        {
                circleArea(a, b);
                cout << "Area (cm2) of the Circle = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 11)
        {
                areaPerimeterOfSquareRectange (a, b);
                cout << "Area (cm2) = " << a << " Perimeter (cm) = " << b << "\n\n";
                measurements( a, b, option);
        }
        if (option == 12) 
                main();
}

void mmTocm (double& a, double& b)
{
        b = (a / 10);
}
void cmTomm (double& a, double& b)
{
        b = (a * 10);
}
void cmToinches (double& a, double& b)
{
        b = (a / 2.50);
}
void inchesTocm (double& a, double& b)
{
        b = (a * 2.50);
}
void metersTomiles (double& a, double& b)
{
        b = (a / 0.000621371192);
}
void milesTometers (double& a, double& b)
{
        b = (a * 1609.344);
}
void metersToyards (double& a, double& b)
{
        b = (a * 1.0936133);
}
void yardsTometers (double& a, double& b)
{
        b = (a / 0.9144);
}
void circleCircumferece (double& a, double& b)
{
        double const PI = 3.14;
        cout << "Enter Diameter (cm/s)\n";
        cin >> a;
        b = (a * PI);
}
void circleArea (double& a, double& b)
{
        double const PI = 3.14;
        cout << "Enter Diameter (cm/s)\n";
        cin >> a;
        b = (a / 2); 
        b = (b * b * PI);       
}
void areaPerimeterOfSquareRectange (double& a, double& b)
{
        double temp(0);
        cout << "Enter Lenght (cm/s)\n";
        cin >> a;
        cout << "Enter Width (cm/s)\n";
        cin >> b;
        temp = a;
        a = (a * b);
        b = (temp * 2) + (b * 2);
}
void calc(double& a, double& b, int& option)
{
        double Addition(double& a, double& b);
        double Subtraction(double& a, double& b);
        double Multiplication(double& a, double& b);
        double Division(double& a, double& b);

        cout << "Please select an option below\n\n";
        cout << "1: Addition\n";
        cout << "2: Subtraction\n";
        cout << "3: Multiplication\n";
        cout << "4: Division\n";
        cout << "5: Main Menu\n";
        cin >> option;

        while ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }

         if (option == 1)
         {
                 getNumbers(a, b);
                 cout << a << " + " << b << " = " << Addition(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 2)
         {
                 getNumbers(a, b);
                 cout << a << " - " << b << " = " << Subtraction(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 3)
         {
                 getNumbers(a, b);
                 cout << a << " x " << b << " = " << Multiplication(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 4)
         {
                 getNumbers(a, b);
                 cout << a << " / " << b << " = " << Division(a, b) << "\n\n";
                 calc (a, b, option);
         }
         if (option == 5) 
                 main();
}

double Addition(double& a, double& b)
{
        return a + b;
}
double Subtraction(double& a, double& b)
{
        return a - b;
}
double Multiplication(double& a, double& b)
{
        return a * b;
}
double Division(double& a, double& b)
{
        return a / b;
}

void currency(double& a, double& b, int& option)
{
        void gbpToeuro(double& a, double& b);
        void euroTogbp(double& a, double& b);
        void gbpTousd(double& a, double& b);
        void usdTogbp(double& a, double& b);

        cout << "Select An Option Below\n\n";
        cout << "1: Convert GBP to EURO\n";
        cout << "2: Convert EURO to GBP\n";
        cout << "3: Convert GBP to USD\n";
        cout << "4: Convert USD to GBP\n";
        cout << "5: Main Menu\n";
        cin >> option;

        while ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5))
        {
                cout << "ERROR: Invalid Key - Try Again: ";
                cin >> option;
        }

        if (option == 1)
        {
                getNumber(a);
                gbpToeuro(a, b);
                cout << a << " GBP = " << b << " EURO\n\n";
                currency(a, b, option);
        }
        if (option == 2)
        {
                getNumber(a);
                euroTogbp(a, b);
                cout << a << " EURO = " << b << " GBP\n\n";
                currency(a, b, option);
        }
        if (option == 3)
        {
                getNumber(a);
                gbpTousd(a, b);
                cout << a << " GBP = " << b << " USD\n\n";
                currency(a, b, option);
        }
        if (option == 4)
        {
                getNumber(a);
                usdTogbp(a, b);
                cout << a << " USD = " << b << " GBP\n\n";
                currency(a, b, option);
        }
        if (option == 5) 
                main();
}


void gbpToeuro(double& a, double& b)
{
        b = (a * 1.21);
}
void euroTogbp(double& a, double& b)
{
        b = (a * 0.83);
}
void gbpTousd(double& a, double& b)
{
        b = (a * 1.67);
}
void usdTogbp(double& a, double& b)
{
        b = (a * 0.60);
}

Edited by BradleyW - 4/13/14 at 9:27am
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #5 of 10
I see what's going on. You should put all your cout statements within the loop as well. That's what I mentioned the first time. Here is how what the main function should be more like:
Code:
int main ()
{
        double a(0), b(0); int option(0);
        void getNumbers(double& a, double& b); 
        void getNumber(double& a); 
        void temps(double& a, double& b, int& option);
        void measurements(double& a, double& b, int& option);
        void calc(double& a, double& b, int& option);
        void currency(double& a, double& b, int& option);

        bool quit = false;

        while (!quit)
        {
            cout << "Main Menu\n\n";
            cout << "Please select an option below\n\n";
            cout << "1. Convert Temperatures\n";
            cout << "2. Convert Measurements\n";
            cout << "3. Basic Calculator\n";
            cout << "4. Currency Converter (Updated as of April 2014)\n"; 
            cout << "5. Quit\n";
            cin >> option;

            if ((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5))
            {
                cout << "ERROR: Invalid Key - Try Again: ";
            }

            if (option == 1)
                temps(a, b, option);
            if (option == 2)
                measurements(a, b, option);
            if (option == 3)
                calc(a, b, option);     
            if (option == 4)
                currency(a, b, option);
            if (option == 5) 
                quit = true;
        }
        system ("pause");

My C++ is a bit rusty and I haven't tested this code, but it should get you along the way to improving the command flow. As you can see, nearly all your program logic (except for the pause command) run inside the while loop, and when option == 5, quit is declared true which makes the program break out of the loop and end. After a function runs from one of the non-quit options, the screen prints the menu text again.

Putting code that can be executed many times is a common convention for programs that wait for a user's input and need to continue getting input indefinitely, especially in programs that need to run real time in every frame such as video games.

Aside from that, also look into switch-case statements, which are made for handling multiple input selection. They are better than using "if" statements here, in particular to replacing this
Code:
((option != 1) && (option != 2) && (option != 3) && (option != 4) && (option != 5))

to a default case.
post #6 of 10
Thread Starter 
+ 1, again.
Thank you very much for the help and advice.
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #7 of 10
Or you can do if ( (option >0) && (option < 6))

Not sure what would happen if someone typed in "Hello World" for option, probably would just exception out anyways.
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #8 of 10
Thread Starter 
Quote:
Originally Posted by Mrzev View Post

Or you can do if ( (option >0) && (option < 6))

Not sure what would happen if someone typed in "Hello World" for option, probably would just exception out anyways.

That's a much better way, agreed. As for someone entering hello world, or any other Char's / String, it just causes an out of control infinite loop.
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
post #9 of 10
Quote:
Originally Posted by BradleyW View Post

That's a much better way, agreed. As for someone entering hello world, or any other Char's / String, it just causes an out of control infinite loop.

And that's what try catch is for =). Well kinda... with great power comes great responsibility.
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #10 of 10
Thread Starter 
+1 for your troubles smile.gif
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
X79-GCN
(22 items)
 
  
CPUMotherboardGraphicsRAM
Intel 3930K 4.5GHz HT GIGABYTE GA-X79-UP4 AMD R9-290X GEil Evo Potenza DDR3 2400MHz CL10 (4x4GB) 
Hard DriveCoolingCoolingCooling
Samsung 840 Pro 120GB EK Supremacy (CPU) NF F12's P/P (360 Rad)  NF A14's (420 Rad)  
CoolingCoolingCoolingCooling
XSPC Chrome Compression Fittings EK RES X3 150 Primochill PremoFlex Advanced LRT Clear 1/2 ID EK-FC (R9 290X) 
CoolingCoolingCoolingOS
EK D5 Vario Top-X  Phobya G-Changer V2 360mm Phobya G-Changer V2 420mm Win 10 x64 Pro 
MonitorKeyboardPowerCase
BenQ XR3501 35" Curved Corsair Vengeance K90 Seasonic X-1250 Gold (v2) Corsair 900D 
MouseAudio
Logitech G400s Senn HD 598 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ Help. Trying to get my program to loop back to main menu on demand?