#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    Maryland
    Posts
    2
    Rep Power
    0

    Question question about switch statements


    hi group I need to ask a question, I just starting to write the code and got stuck on something. If I am using a switch statement as a menu in a program with functions do I still need to call the functions or is the call in the switch statement?? example below

    #include <iostream>
    #include <stdlib.h>
    #include <fstream>

    using namespace std;

    //function prototypes
    void menu();
    int addFractions(int addNu1, int addDeno1, int addNu2, int addDeno2);
    int subFractions(int subNu1, int subDeno1, int subNu2, int subDeno2);
    int mulFractions(int mulNu1, int mulDeno1, int mulNu2, int mulDeno2);
    int divFractions(int divNu1, int divDeno1, int divNu2, int divDeno2);
    void displayErrorMsg();

    ifstream inputsource;
    ofstream outputsource;

    int main ()
    {
    //declare variables
    int addNu1 = 0;
    int addDeno1 = 0;
    int addNu2 = 0;
    int addDeno2 = 0;
    int subNu1 = 0;
    int subDeno1 = 0;
    int subNu2 = 0;
    int subDeno2 = 0;
    int mulNu1 = 0;
    int mulDeno1 = 0;
    int mulNu2 = 0;
    int mulDeno2 = 0;
    int divNu1 = 0;
    int divDeno = 0;
    int divNu2 = 0;
    int divDeno = 0;
    int result_of_add = 0;
    int result_of_sub = 0;
    int result_of_mul = 0;
    int result_of_div = 0;


    //open files
    inputsource.open("A:fractiondata.txt"); //open input file
    outputsource.open("A:resultdata.txt"); //open output file

    //call functions

    while(menuChoice !='0')
    {
    switch(menuChoice)
    {
    case '1':
    addFractons(addNu1, addDeno1, addNu2, addDeno2);
    break;
    case '2':
    subFractions(subNu1, subDeno1, subNu2, subDeno2);
    break;
    case '3':
    mulFractions(mulNu1, mulDeno1, mulNu2, mulDeno2);
    break;
    case '4':
    divFractions(divNu1, divDeno1, divNu2, divDeno2);
    break;
    default:
    displayErrorMsg();
    }
    }
    menu();
    cin>>menuChoice;
    //input datat for adding the fractions
    inputfile>>addNu1>>addDeno1>>addNu2>>addDeno2;

    result_of_add = addFractions(addNu1, addDeno1, addNu2, addDeno2);
    result_of_sub = subFractions(subNu1, subDeno1, subNu2, subDeno2);
    result_of_mul = mulFractions(mulNu1, mulDeno1, mulNu2, mulDeno2);
    result_of_div = divFractions(divNu1, divDeno1, divNu2, divDeno2);

    //close files
    inputsource.close();
    outputsource.close();

    system("PAUSE");
    return 0;

    }

    //function header void menu
    void menu()
    {
    cout<<"\n1. To add a Fraction";
    cout<<"\n2. To subtract a Fraction";
    cout<<"\n3. To multiply a Fraction";
    cout<<"\n4. To divide a Fraction";
    cout<<"\n Enter 0 to quite";
    cout<<"\n Enter your choice>> ";
    }//end of function void menu
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    4
    Rep Power
    0
    hi...
    I've done similar thing in C programming and what i did was that i printed out the menu first... then in the switch statements i called the functions... however your functions should be defined before...
    hope this helps...
  4. #3
  5. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Please use "code" tags when posting code: http://forums.devshed.com/misc.php?action=bbcode&s=.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo