Thread: Im new an trying to make a calculator

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Question Im new an trying to make a calculator

    Hi everyone. I'm new here and I'm also new to programming. I figured that making a calculator might be a good start. I know how about some commands but not all the ones I will need. I use Borland C++ 5.0.2

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    So what's your question?

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    32

    Cool Reply

    Im new 2.... plus im only 12 so its hard to uunderstand all of this stuff..... i have made a calulator before here is my code.

    Code:
    #include <iostream.h>
    
    int main()
    {
         int a, b;
         cout<<"Entering text (if any)\n";
         cout<<"enter a number\n";
         cin>> a;
         cout<<"enter a number to add to it\n";
         cin>> b;
         cout<<"the sum of the number is " << a+b << endl;
         return a+b;
    }

  4. #4
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    You might want to hold off on a calculator program untill your at least intermiddient(sp?), but it's just MHO.

  5. #5
    Unregistered
    Guest
    Helbovine, dont return a+b from main. There is no need to, the return statement is used to pass a value back to the OS, sometimes it'll look like
    Code:
    return EXIT_SUCCESS;
    but all this represents is an int value

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    32

    k

    thx im new and i have to get a better book but thx for advise

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    32
    1 question.. lets say i said this

    Code:
    #include <iostream.h>
    
    int add()
    {
       int a, b;
       cout<<"text";
       cin>> a;
       cout<<"text";
       cin>> b;
       cout<<"The sum is " << a+b << endl;
       //now here in the add function would i put
       return a+b; //or return EXIT_SUCCESS
    }
    int main()
    {
      add
      /*and here would it be return EXIT_SUCCESS or*/ return 0;
    }
    thnk u

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream.h>
    
    void add()
    {
       int a, b;
       cout<<"enter first number";
       cin>> a;
       cout<<"enter second number";
       cin>> b;
       cout<<"The sum is " << a+b << endl;
    }
    
    int main()
    {
      add();
      return 0;
    }

  9. #9
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    the return a+b isnt needed.

    You could use it if you want to do something like this:

    int c = add();

    c will then be equal to the return value of add()

    in case your function doesn't need to return anything, do like elad did it and use void.
    That means that the function doesn't return anything

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    32

    grrr help

    illegal else without matching if

    Code:
    #include <iostream.h>
    
    int add()
    {
       int a, b;
       cout<<"First Number\n";
       cin>> a;
       cout<<"Second Number\n";
       cin>> b;
       cout<<"The sum is " << a+b << endl;
       return a+b;
    }
    
    int subtract()
    { 
    	int c, d;
    	cout<<"First Number\n";
    	cin>> c;
    	cout<<"Second Number\n";
    	cin>> d;
    	cout<<"The Difference Is " << c-d << endl;
    	return c-d;
    }
    
    int main()
    {
    	char sign;
    	cout<<"Enter Mathamatical Symbol ( + - * / )\n";
    	cin>> sign;
    	if(sign == '+');
    	{
    		add();
    		return 0;
    	}
    	else if(sign == '-');
        {
    		subtract();
    		return 0;
    	}
        return 0;
    }

  11. #11
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    if(sign == '+');
    it should be:

    if(sign == 'x') { //no ; after the if statement
    //blablabla
    } else if(...) {//no ; here either
    //blabla
    }

  12. #12
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    just use a simple switch statment

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    void main()
    {
    double A, B, ans; char sign;
    
    clrscr();
    cout << "Enter first number: ";
    cin >> A;
    cout << endl;
    
    cout << "Enter second number: ";
    cin >> B;
    cout << endl;
    
    cout << "enter operation (+,-,*,/): ";
    cin >>  sign;
    cout << endl;
    
    switch (sign)
    {
       case '+':
             ans = A + B; break;
       case '-':
             ans = A - B; break;
       case '*':
             ans = A * B; break;
       case '/':
             ans = A / B; break;
       default:
             cout << "Invalid operation";
    }
    
    cout << "The answer is: " << ans;
    getch();
    }
    if you use a 'switch' then it cuts down on the number of functions.
    Last edited by xlnk; 03-30-2002 at 01:36 PM.
    the best things in life are simple.

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    32

    calc finished

    well i finished it before i even saw ur switch statment there here it is

    Code:
    #include <iostream.h>
    
    int add()
    {
       long int a, b;
       cout<<"First Number\n";
       cin>> a;
       cout<<"Second Number\n";
       cin>> b;
       cout<<"The sum is " << a+b << endl;
       return a+b;
    }
    
    int subtract()
    { 
    	long int c, d;
    	cout<<"First Number\n";
    	cin>> c;
    	cout<<"Second Number\n";
    	cin>> d;
    	cout<<"The Difference Is " << c-d << endl;
    	return c-d;
    }
    
    int multiply()
    {
    	long int e, f;
    	cout<<"First Number\n";
    	cin>> e;
    	cout<<"Second Number\n";
    	cin>> f;
    	cout<<"The Product Is " << e*f << endl;
    	return e*f;
    }
    
    int divide()
    {
    	long int g, h;
    	cout<<"First Number\n";
    	cin>> g;
    	cout<<"Second Number\n";
    	cin>> h;
    	cout<<"The Answer Is " << g/h << endl;
    	return g/h;
    }
    
    int main()
    {
    	char sign;
    	cout<<"Enter Mathamatical Symbol ( + - * / )\n";
    	cin>> sign;
    	if(sign == '+')
    	{
    		add();
    		return 0;
    	}
        else if(sign == '-')
        {
    		subtract();
    		return 0;
    	}
    	else if(sign == '*')
    	{
    		multiply();
    		return 0;
    	}
    	else if(sign == '/')
    	{
    		divide();
    		return 0;
    	}
    	else if(sign == 'a' || 'b' || 'c' || 'd' || 'e' || 'f' || 'g' || 'h' || 'i' || 'j' || 'k' || 'l' || 'm' || 'n' || 'o' || 'p' || 'q' || 'r' || 's' || 't' || 'u' || 'v' || 'w' || 'x' || 'y' || 'z')
    	{ 
    		cout<<"Not A Legall Statement Try Again\n";
    	}
        return 0;
    }
    but lets say if i wanted i wanted to make it say........
    add, subtract, multiply, or divide again? y,n
    how would i get it to loop back up and start main all over again

  14. #14
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    int main()
    {
    char sign;
    char redo;
    int loopQuit = 0;
    while(loopQuit == 0) {
    cout<<"Enter Mathamatical Symbol ( + - * / )\n";
    cin>> sign;
    if(sign == '+')
    {
    add();
    return 0;
    }
    else if(sign == '-')
    {
    subtract();
    return 0;
    }
    else if(sign == '*')
    {
    multiply();
    return 0;
    }
    else if(sign == '/')
    {
    divide();
    return 0;
    }
    else
    {
    cout<<"Not A Legal Statement Try Again\n";
    }
    cout << "Redo this?(y or n) ";
    cin >> redo;
    if(strcmp(redo,"n")==0 || strcmp(redo,"N")==0) {
    loopQuit = 1;
    }
    }
    return 0;
    }


    also, notice the else instead of the else if(blalblablabla)

  15. #15
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <bool.h>
    
    void calc()
    {
         double A, B; double ans; char sign;
    
         clrscr();
         cout << "Enter first number: ";
         cin >> A;
         cout << endl;
    
         cout << "Enter second number: ";
         cin >> B;
         cout << endl;
    
         cout << "enter operation (+,-,*,/): ";
         cin >>  sign;
         cout << endl;
    
         switch (sign)
         {
              case '+':
                   ans = A + B; break;
              case '-':
                   ans = A - B; break;
              case '*':
                   ans = A * B; break;
              case '/':
                   ans = A / B; break;
              default:
                   cout << "Invalid operation";
         }
    
         cout << "The answer is: " << ans;
         getch();
    }
    
    bool NotFinished()
    {
         char Choice;
         bool Temp;
    
         cout << endl;
         cout << "Would you like to repeat [Y/N]: ";
         cin >> Choice;
    
         switch(Choice)
         {
              case 'Y':
                   Temp = true; break;
              case 'N': 
                   Temp = false; break;
              default :
                   cout << "You must enter Y or N" << endl;
         }
         return Temp;
    }
    
    
    
    void main()
    {
         do
         {
              calc();
         }while(NotFinished());
    }
    Last edited by xlnk; 03-30-2002 at 02:51 PM.
    the best things in life are simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 08:23 AM
  2. Basic Calculator
    By Surge in forum C Programming
    Replies: 18
    Last Post: 12-02-2006, 10:20 PM
  3. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  4. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  5. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 02:16 PM