Thread: First (semi) usefull program. Big accomplishment for me.

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    Thumbs up First (semi) usefull program. Big accomplishment for me.

    Well a while ago i started learning C. Then i toook about a month off from learning anything new, or even using what i already had learnt. I came back the other day to get goin again with it and i couldnt remember how to do anything. Well i gave up on C and yesterday i started learning C++. I relearned what i new very quik and i made my first Calculator program just a little bit ago. Heres the code in all its glory.

    Code:
    #include <iostream>                                //Matt's New calculator program in C++ :)
    #include <stdlib.h>                                //It works too =D
     
     float add(float a, float b);
    
     float sub(float a, float b);  
    
     float mult(float a, float b);
    
     float div(float a, float b);
    
      float a, b;
    
    int main(int argc, char *argv[])
    {  int choose;
       int l;
    for(int l=0; l<10000; a++)
     {
       cout<<"\n                                   WELCOME";
       cout<<"\n\n\n1.          Addition";
       cout<<"\n\n2.          Subtraction";
       cout<<"\n\n3.          Multiplication";
       cout<<"\n\n4.          Division\n\n";
       
       cout<<"Pick the number of what you want to do, then press enter\n\n";
       
       cin>>choose;
       switch (choose)
        {
         case 1:
          { 
           add(a, b);
           break;
          }
         
         case 2:
          {
           sub(a, b);
           break;
          }
         
         case 3:
          {
           mult(a, b);
           break;
          }
        
          case 4:
          { 
           div(a, b);
           break;
          }
          
          default:
           {
            cout<<"Not recognized as a choice, program will quit\n";
           }
         }
      system("PAUSE");
     }	
      return 0;
    }
    
    float add(float a, float b)
     {
      cout<<"You have chosen to add\n";
      cout<<"Input 2 numbers you wish to add\n";
      cin>>a>>b;
      cout<<a<<" + "<<b<<" = "<< a+b;
     }
    
    float sub(float a, float b)
     { 
      cout<<"You have chosen to subtract\n";
      cout<<"Input 2 numbers you wish to subtract.\n(The first number will be subtracted by the second)\n";
      cin>>a>>b;
      cout<<a<<" - "<<b<<" = "<< a-b;
     }
      
    float mult(float a, float b)
     {
      cout<<"You have chosen to multiply\n";
      cout<<"Input 2 numbers that you wish to multiply\n";
      cin>>a>>b;
      cout<<a<<" x "<<b<<" = "<< a*b;
     }
     
    float div(float a, float b)
     {
      cout<<"You have chosen to divide\n";
      cout<<"Input 2 numbers that you wish to divide. \n(The first number will be divided by the second)\n";
      cin>>a>>b;
      cout<<a<<" / "<<b<<" = "<< a/b;
     }
    This is big for me because i did it without any big errors that i got stuck on, or any help. Let me know if im doing something wrong that could be done alot easier some other way. Thanks.
    ~matt~

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Lightbulb Function pointer

    Code:
    #include <iostream> 
    #include <stdlib.h>    
    float add();
    float sub();  
    float mult();
    float div();
    float a, b;
    
    int main(int argc, char *argv[])
    {  
       int choose;
       int l;
       float (*function[4])()= { add, sub, mult, div };
       do{
          cout<<"\nWELCOME";
          cout<<"\n\n\n1.          Addition";
          cout<<"\n\n2.          Subtraction";
          cout<<"\n\n3.          Multiplication";
          cout<<"\n\n4.          Division\n\n";
       
          cout<<"Pick the number of what you want to do, then press enter\n\n";
       
          cin>>choose;
          if( choose != 0)
             function[ choose - 1 ]();
    
      }while(choose != 0);	
      return 0;
    }
    
    float add() {
       cout<<"You have chosen to add\n";
       cout<<"Input 2 numbers you wish to add\n";
       cin>>a>>b;
       cout<<a<<" + "<<b<<" = "<< a+b;
    }
    
    float sub() { 
       cout<<"You have chosen to subtract\n";
       cout<<"Input 2 numbers you wish to subtract.\n(The first number will be subtracted by the second)\n";
       cin>>a>>b;
       cout<<a<<" - "<<b<<" = "<< a-b;
    }
      
    float mult() {
       cout<<"You have chosen to multiply\n";
       cout<<"Input 2 numbers that you wish to multiply\n";
       cin>>a>>b;
       cout<<a<<" x "<<b<<" = "<< a*b;
    }
     
    float div() {
       cout<<"You have chosen to divide\n";
       cout<<"Input 2 numbers that you wish to divide. \n(The first number will be divided by the second)\n";
       cin>>a>>b;
       cout<<a<<" / "<<b<<" = "<< a/b;
    }
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. program size too big?
    By apsync in forum Windows Programming
    Replies: 2
    Last Post: 12-31-2004, 02:58 PM
  3. My First BIG program done!!! Check it out!!
    By biosninja in forum C++ Programming
    Replies: 8
    Last Post: 09-04-2002, 03:33 PM
  4. BIG problem-O with a C program
    By JohnMayer in forum C Programming
    Replies: 13
    Last Post: 07-20-2002, 03:41 PM
  5. big trouble in little program
    By jonesy in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 02:04 PM