Thread: My calc program?

  1. #1

    Angry My calc program?

    I kept getting errors and warnings on this code, but before I post the errors, I am sure you can pick them out by yourselves with this being such a low level code.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int add()
    {
    	unsigned long int a;
    	unsigned long int b;
    	cout << "\nFirst number: ";
    	cin.get >> a;
    	cout << "\nSecond number: ";
    	cin.get >> b;
    	cout << "\nAnswer: ";
    	cout << a + b;
    	cin.get;
    	menu();
    	return 0;
    }
    
    int subtract()
    {
    	unsigned long int a;
    	unsigned long int b;
    	cout << "\nFirst number: ";
    	cin.get >> a;
    	cout << "\nSecond number: ";
    	cin.get >> b;
    	cout << "\nAnswer: ";
    	cout << a - b;
    	cin.get;
    	menu();
    	return 0;
    }
    
    int multiply()
    {
    	unsigned long int a;
    	unsigned long int b;
    	cout << "\nFirst number: ";
    	cin.get >> a;
    	cout << "\nSecond number: ";
    	cin.get >> b;
    	cout << "\nAnswer: ";
    	cout << a * b;
    	cin.get;
    	menu();
    	return 0;
    }
    
    int devide()
    {
    	unsigned long int a;
    	unsigned long int b;
    	cout << "\nFirst number: ";
    	cin.get >> a;
    	cout << "\nSecond number: ";
    	cin.get >> b;
    	cout << "\nAnswer: ";
    	cout << a / b;
    	cin.get;
    	menu();
    	return 0;
    }
    		
    int menu()
    {
    	int choice;
    	cout << "1. Add\n";
    	cout << "2. Subtract\n";
    	cout << "3. Multipply\n";
    	cout << "4. Devide\n";
    	cout << "\n";
    	cout << "Choice: ";
    	cin.get >> choice;
    	if (choice='1') {
    		add();
    	}
    	else if (choice='2') {
    		subtract();
    	}
    	else if (choice='3') {
    		multiply();
    	}
    	else if (choice='4') {
    		devide();
    	}
    	else {
    		cout << "Invalid option.";
    		menu();
    	}
    	return 0;
    }
    
    int main()
    {
    	menu();
    	return 0;
    }

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369

    Re: My calc program?

    Code:
         cin.get >> a;        // I dunno if this would be a problem, but  try a = cin.get();
    
         else if (choice='2') // Use == in conditional statements, = assigns a VALUE to the variable
    EDIT: vVv beat me
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Originally posted by vVv
    Your usage of cin.get( ) is wrong. cin.get( ) is a /function/ and it returns the character read. For example

    >cin.get >> a;

    becomes a = cin.get( ), or simply cin >> a;. Also note that cin overloads the types you supply, so you should make your variables ``char'' instead of ``unsigned int'' if you want to input ascii chars.

    >if (choice='1') {

    This shouldn't give you an error, but make it ==.

    >but before I post the errors, I am sure you can pick them out by
    >yourselves with this being such a low level code.

    Ignorance. Annoying. Post your errors if you still get any.
    Yea I just noticed the if statement was messed up and fixed that, I am going to go and compile to see what I get.

  4. #4
    I did what you said, I am not getting any warnings now, but I am getting two errors-

    C:\Windows\Desktop\Programming\calc\clac.cpp(16) : error C2065: 'menu' : undeclared identifier
    C:\Windows\Desktop\Programming\calc\clac.cpp(66) : error C2373: 'menu' : redefinition; different type modifiers

  5. #5
    Thanks, it is finally working

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    I donīt see the purpose of returning 0 in add-, subtract-, multiply-, devide- and menufunctions. Change it to void insteed and remove unnessesary return 0 statements.

    Do NOT change the code in main to void main and exclude return 0 or else be prepared to face the wrath of Salem.
    Last edited by ripper079; 12-26-2002 at 04:06 PM.

  7. #7
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    2 things mentioned by others.. are okey.. one problem was with cin.get() and other was useage of = instead of == in conditional statement.. but there is one more thing important is that choice is an integer variable then why are you comparing using ' as
    if (choice == '1')
    this is not goood but it should be as
    if (choice == 1)

    One more important thing is about menu() function.. when menu is displayed..by correcting your code.. there is no way to get out the menu() I mean to close the program.. so in the menu there must be a choice by which user can terminate the program...
    I have edited the program..and code is

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    int menu();//You did not declared menu() before using it..
    int add()
    {
    	unsigned long int a;
    	unsigned long int b;
    	cout << "\nFirst number: ";
    	cin >> a;
    	cout << "\nSecond number: ";
    	cin >> b;
    	cout << "\nAnswer: ";
    	cout << a + b;
    	menu();
    	return 0;
    }
    
    int subtract()
    {
    	unsigned long int a;
    	unsigned long int b;
    	cout << "\nFirst number: ";
    	cin >> a;
    	cout << "\nSecond number: ";
    	cin >> b;
    	cout << "\nAnswer: ";
    	cout << a - b;
    	menu();
    	return 0;
    }
    
    int multiply()
    {
    	unsigned long int a;
    	unsigned long int b;
    	cout << "\nFirst number: ";
    	cin >> a;
    	cout << "\nSecond number: ";
    	cin >> b;
    	cout << "\nAnswer: ";
    	cout << a * b;
    	menu();
    	return 0;
    }
    
    int devide()
    {
    	unsigned long int a;
    	unsigned long int b;
    	cout << "\nFirst number: ";
    	cin >> a;
    	cout << "\nSecond number: ";
    	cin >> b;
    	cout << "\nAnswer: ";
    	cout << a / b;
    	menu();
    	return 0;
    }
    		
    int menu()
    {
    	int choice;
    	cout << "1. Add\n";
    	cout << "2. Subtract\n";
    	cout << "3. Multipply\n";
    	cout << "4. Devide\n";
        cout << "5. Exit\n";
    	cout << "\n";
    	cout << "Choice: ";
    	cin >> choice;
    	if (choice == 1 ) {
    		add();
    	}
    	else if (choice == 2) {
    		subtract();
    	}
    	else if (choice == 3) {
    		multiply();
    	}
        else if (choice == 4) {
    		devide();
    	}
        else if (choice == 5) {
    		exit(0);
    	}
      	else {
    		cout << "Invalid option.";
    		menu();
    	}
    	return 0;
    }
    
    int main()
    {
    	menu();
    	return 0;
    }
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

  8. #8

    Accually...

    I accually remade it. I was bored and then I thought about remaking it. So here is the new code.

    Code:
    #include <iostream>
    	
    using namespace std;
    	
    int main()
    {
    	char option;
    	float a, b;
    	cout << "1. Add" << endl;
    	cout << "2. Subtract" << endl;
    	cout << "3. Multiply" << endl;
    	cout << "4. Devide" << endl << endl; // the extra "<< endl" so it will leave a space between the menu and the "Choice" line 
    	cout << "Choice: ";
    	cin >> option;
    	
    	if (option=='1')
    	{
    		cout << "\nWhat is the first number?: ";
    		cin >> a;
    		cout << "\nWhat is the second number?: ";
    		cin >> b;
    		cout << "\nAnswer: ";
    		cout << a+b;
    	}
    	else if (option=='2')
    	{
    		cout << "\nWhat is the first number?: ";
    		cin >> a;
    		cout << "\nWhat is the second number?: ";
    		cin >> b;
    		cout << "\nAnswer: ";
    		cout <<	a-b;
    	}
    	else if (option=='3')
    	{
    		cout << "\nWhat is the first number?: ";
    		cin >> a;
    		cout << "\nWhat is the second number?: ";
    		cin >> b;
    		cout << "\nAnswer: ";
    		cout << a*b;
    	}
    	else if (option=='4')
    	{
    		cout << "\nWhat is the first number?: ";
    		cin >> a;
    		cout << "\nWhat is the second number?: ";
    		cin >> b;
    		cout << "\nAnswer: ";
    		cout << a/b;
    	}
    	else
    	{
    		cout << "\nThat is an invalid option.";
    	}
    	return 0;
    }

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Accually...

    Originally posted by Munkey01
    I accually remade it. I was bored and then I thought about remaking it. So here is the new code.
    Whoa, what the heck happened to your modularity?
    Functions are good.
    Modularity is good.
    Functions are good.
    Modularity is good.
    Functions are good.
    In short, don't get rid of your functions.

    Another suggestion: Give the user an option to use the calculator again if he wants to. Hint: fuctions will make doing this a whole lot easier.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  10. #10
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    Phew, it's good to know that I'm not the worst programmer on the CBoard
    - Dean

  11. #11
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    LordVirus, that was harsh

    But you were thinking healthy when you tried to implement functions, Monkey. It's the more logical path
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  12. #12
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Originally posted by LordVirusXXP
    Phew, it's good to know that I'm not the worst programmer on the CBoard
    And probably not the best!!!

    Come on LordVirusXXP, that was rude and inpolite (like my post).

  13. #13
    Registered User LordVirusXXP's Avatar
    Join Date
    Dec 2002
    Posts
    86
    Yeah, I'm definately not the best. But on a helpful note, your if statements are messed up. I certainly hope that helps. (TeeHee!Z)
    - Dean

  14. #14
    Shadow12345
    Guest
    I don't know how far you are planning on going with this calculator idea, but if you want to make it object oriented you could make a calculator class, and use operator+, operator-, operator/, and operator*.

    Code:
    ...
    int a, b, answer;
    cout << "enter two numbers to add" << endl;
    cin >> a >> b;
    answer = mycalc.operator+(a,b);
    something along those lines

  15. #15
    Originally posted by LordVirusXXP
    Phew, it's good to know that I'm not the worst programmer on the CBoard
    Well yes I am not very good, yet. But you got to take in the fact I just started a few days before Christmas.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM