Thread: any suggestions on how to make this program smaller?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    2

    Question any suggestions on how to make this program smaller?

    can any help me out? i'm fairly new at this c++ programming thing and i was wondering if there was anyway to make this program a little smaller or less repetitive where the result for the calculations are concerned, in the switch statement.

    thanks


    /* This program performs calculator functions.*/

    #include <iostream.h>
    #include <conio.h>
    //constants for the menu
    #define ONE 1
    #define TWO 2
    #define THREE 3
    #define FOUR 4
    #define FIVE 5
    #define SIX 6
    #define SEVEN 7
    #define EIGHT 8
    #define NINE 9
    #define TEN 10
    #define ELEVEN 11
    #define TWELVE 12


    void main() { //beginnning of program

    //initialize variables
    int choice=0;
    int num1=0;
    int num2=0;

    while(choice!=TWELVE) { //beginning of while loop
    clrscr();
    cout<<endl;
    cout<<" My Calculator - Menu"<<endl;
    cout<<endl;
    cout<<endl;
    cout<<"\t[" <<ONE<< "] - Add"<<endl;
    cout<<"\t[" <<TWO<< "] - Subtract"<<endl;
    cout<<"\t[" <<THREE<< "] - Multiply"<<endl;
    cout<<"\t[" <<FOUR<< "] - Divide"<<endl;
    cout<<"\t[" <<FIVE<< "] - Remainder"<<endl;
    cout<<"\t[" <<SIX<< "] - Less than"<<endl;
    cout<<"\t[" <<SEVEN<< "] - Less than or equal to"<<endl;
    cout<<"\t[" <<EIGHT<< "] - Greater than"<<endl;
    cout<<"\t[" <<NINE<< "] - Greater than or equal to"<<endl;
    cout<<"\t[" <<TEN<< "] - Equal to"<<endl;
    cout<<"\t[" <<ELEVEN<<"] - Not equal to"<<endl;
    cout<<"\t[" <<TWELVE<<"] - Quit"<<endl;
    cout<<endl;
    //prompts the user to input an operation and 2 numbers.
    cout<<"Enter an operation: ";
    cin>>choice;
    /*this if statement tests the choice entered and terminates
    the program if the selection 12 is entered.*/
    if (choice==TWELVE) { //beginning of if statement
    cout<<"Goodbye!"<<endl;
    getch();
    return;
    } //end if
    cout<<"Enter first number: ";
    cin>>num1;
    cout<<"Enter second number: ";
    cin>>num2;
    switch (choice) { //beginning of switch statement
    case 1: {
    cout<<"Result is: "<<num1+num2;
    }
    break;
    case 2: {
    cout<<"Result is: "<<num1-num2;
    }
    break;
    case 3: {
    cout<<"Result is: "<<num1*num2;
    }
    break;
    case 4: {
    cout<<"Result is: "<<num1/num2;
    }
    break;
    case 5: {
    cout<<"Result is: "<<num1%num2;
    }
    break;
    case 6: {
    if(num1<num2) {
    cout<<"Result is: True";
    }
    else {
    cout<<"Result is: False";
    } //end if
    }
    break;
    case 7: {
    if(num1<=num2) {
    cout<<"Result is: True";
    }
    else {
    cout<<"Result is: False";
    } //end if
    }
    break;
    case 8: {
    if(num1>num2) {
    cout<<"Result is: True";
    }
    else {
    cout<<"Result is: False";
    } //end if
    }
    break;
    case 9: {
    if(num1>=num2) {
    cout<<"Result is: True";
    }
    else {
    cout<<"Result is: False";
    } //end if
    }
    break;
    case 10:{
    if(num1==num2) {
    cout<<"Result is: True";
    }
    else {
    cout<<"Result is: False";
    } //end if
    }
    break;
    case 11:{
    if(num1!=num2) {
    cout<<"Result is: True";
    }
    else {
    cout<<"Result is: False";
    } //end if
    }
    break;
    /* default: {
    cout<<"Invalid Operation!"<<endl;
    } */

    } //end of switch statement
    getch();
    } //end of while loop
    } //end of program

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Use code tags. (See top of this forum)

    Don't use ONE, TWO, etc...
    Just deal with a plus sign, minus sign, etc...

    void main() is incorrect. Use int main() instead

    You don't need { and } after case

    Start out by printing "Result is" and print the rest out depending on the operation.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Since the while loop is going to always run while choice isn't 12, eliminate that unnecessary "if choice == TWELVE" and just put the cout and the getch() after the while loop.

    And, as said, int main (void), don't use goofy constants, eliminated unneccessary braces

    Also endl flushes the stream (I think), and you can avoid more cout pushing etc by just adding a \n to the end of the choice strings

    "Result is" is printed a lot unneccessarily, just print it once
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
     cout<<"\t[" <<ONE<< "] - Add"<<endl;
    cout<<"\t[" <<TWO<< "] - Subtract"<<endl;
    cout<<"\t[" <<THREE<< "] - Multiply"<<endl;
    cout<<"\t[" <<FOUR<< "] - Divide"<<endl;
    cout<<"\t[" <<FIVE<< "] - Remainder"<<endl;
    cout<<"\t[" <<SIX<< "] - Less than"<<endl;
    cout<<"\t[" <<SEVEN<< "] - Less than or equal to"<<endl;
    cout<<"\t[" <<EIGHT<< "] - Greater than"<<endl;
    cout<<"\t[" <<NINE<< "] - Greater than or equal to"<<endl;
    cout<<"\t[" <<TEN<< "] - Equal to"<<endl;
    cout<<"\t[" <<ELEVEN<<"] - Not equal to"<<endl;
    cout<<"\t[" <<TWELVE<<"] - Quit"<<endl;
    You could do something like this:
    Code:
    const std::string Element[] = { "Add", "Subtract", "Multiply", "Divide", "Remainder", "Whatever" };
    
    const int NUM_ELEMENTS = 6;
    for (int i = 0; i < NUM_ELEMENTS; ++i)
        cout << "\t[" << i+1 << "] - " << Element[i] << endl;

  5. #5
    Registered User devil@work's Avatar
    Join Date
    Mar 2003
    Posts
    33
    first of all get rid of all the define lines they are not needed.
    you dont need if (choice==TWELVE) statement it is going to terminate anyway.then no need for braces in case statements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. How do you make a program to interface with another?
    By Finchie_88 in forum C++ Programming
    Replies: 6
    Last Post: 08-24-2007, 03:48 AM
  3. Make window in VB but make program in C/C++?
    By Boomba in forum Windows Programming
    Replies: 1
    Last Post: 06-23-2004, 12:29 AM
  4. make a program stay....
    By Nirav in forum C Programming
    Replies: 6
    Last Post: 10-12-2003, 07:01 PM
  5. Programmer Cannot Make Any Program!
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2002, 12:48 PM