6 Replies - 611 Views - Last Post: 30 June 2011 - 07:06 PM Rate Topic: -----

#1 IsoChronous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-June 11

Trying to make a simple calculator

Posted 30 June 2011 - 05:52 AM

Im just starting C++ and Im trying to make a simple calculator program (no GUI yet, just command line). My question has three parts: first, I cannot seems to get it to do anything other than add two numbers. it skips right over the "subtract", "multiply", and "divide" statements. I am unable to see what i did wrong. second question: how to use the "float" variable? I read that using "float" will allow for decimals (important in a calculator). Last question: how do i allow for an infinite amount of numbers to be added, divided, etc? i mean if i wanted to multiply 17 different numbers, how would that look in code? heres my code so far.
#include <iostream>
#include <string>
using namespace std;
int mult (int x, int y);
int main()
{
    string answer;
    int x;
    int y;
    cout << "Give me two numbers: ";
    cin >> x >> y;
    cin.ignore();
    cout << "Do you want to add, multiply, subtract, or divide?\n";
    cin >> answer;
    cin.ignore();
    if ((answer == "Add"))
    {
        cout << "These two numbers equal "<< x+y <<".\n";
    }
    int mult (int x, int y);
    {
        return x+y;
    }
    if ((answer == "Multiply"))
    {
        cout << "These two numbers equal "<< x*y <<".\n";
    }
    int mult (int x, int y);
    {
        return x*y;
    }
    if ((answer == "Subtract"))
    {
        cout << "These two numbers equal "<< x-y <<".\n";
    }
    int mult (int x, int y);
    {
        return x-y;
    }
    if ((answer == "Divide"))
    {
        cout << "These two numbers equal "<< x/y <<".\n";        
    }
    int mult (int x, int y);
    {
        return x/y;
    }
}

thanks in advance, guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to make a simple calculator

#2 stayscrisp  Icon User is online

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,195
  • Joined: 14-February 08

Re: Trying to make a simple calculator

Posted 30 June 2011 - 06:27 AM

You keep returning the answer to main, everytime you call mult it returns the answer to main.

Why do you have the same mult function defined over and over? You don't even need those functions, you are already doing the arithmetic when you output the answer.
Was This Post Helpful? 0
  • +
  • -

#3 IsoChronous  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-June 11

Re: Trying to make a simple calculator

Posted 30 June 2011 - 07:03 AM

Well like I said, I'm still learning. So, removing those "int mult" lines should fix the issue?
Was This Post Helpful? 0
  • +
  • -

#4 brandon_v  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 29-May 08

Re: Trying to make a simple calculator

Posted 30 June 2011 - 07:22 AM

Yea, remove those "int mult" lines, including the {.blah.blah.blah.} from there.
What you're doing is defining the mult function several times, and inside another function, both of which programming do nots.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is online

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,195
  • Joined: 14-February 08

Re: Trying to make a simple calculator

Posted 30 June 2011 - 08:13 AM

I would also recommend that you get the user to choose a number rather than doing a string comparison, just say choose 1 for addition, 2 for multiplying etc..
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,556
  • Joined: 09-June 09

Re: Trying to make a simple calculator

Posted 30 June 2011 - 10:16 AM

An example of using a switch, as stayCrisp suggested

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int choice, x, y;
    cout << "Give me two numbers: ";
    cin >> x >> y;
    cout << "Do you want to (1) add, (2) multiply, (3) subtract, or (4) divide?\n";
    cin >> choice;
	
	switch(choice) {
		case 1 : 
		//code to add numbers
		break;
		case 2:
		//code to multiply numbers
		break;
		case 3:
		//code to subract numbers
		break;
		case 4:
		//code to divide numbers
		break;
		default:
		//code to handle if user entered a number that is not 1-4
	}
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#7 dragon40226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 29-June 11

Re: Trying to make a simple calculator

Posted 30 June 2011 - 07:06 PM

I wrote a simple calc like this a couple days ago here is my code:
#include <iostream>
#include <cstdlib>

using namespace std;

int main () {
    char choice;
    double num;
    double num2;

    for (;;)/>{
    do {
    cout << "What function would you like to do?\n";
    cout << "There are 4 functions.\n";
    cout << "1 - For Additon.\n";
    cout << "2 - For Subtraction.\n";
    cout << "3 - For Division.\n";
    cout << "4 - For Multiplication.\n";
    cout << "Made By Andrew Corp :D/> \n";
    cout << "Enter which one you would like to do here: \n";
    cin >> choice;
    } while ( choice < '1' || choice > '4' && choice != 'q');
    if (choice == 'q') break;
    switch (choice) {

    case '1':
    cout << "Enter the first number to add: ";
    cin >> num;
    cout << "Enter the second number to add: ";
    cin >> num2;
    cout << "The sum of the two is: " << num + num2 << endl;
    cout << " ";
    cout << " ";
    break;

    case '2':
    cout << "Enter the first number to subtract: ";
    cin >> num;
    cout << "Enter the second number to subtract: ";
    cin >> num2;
    cout << "The difference of the two is: " << num - num2 << endl;
    cout << " ";
    cout << " ";
    break;

    case '3':
    cout << "Enter the dividend: ";
    cin >> num;
    cout << "Enter the divisor: ";
    cin >> num2;
    cout << "The quotient is: " << num / num2 << endl;
    cout << " ";
    cout << " ";
    break;

    case '4':
    cout << "Enter the first number to multiply: ";
    cin >> num;
    cout << "Enter the second number to multiply: ";
    cin >> num2;
    cout << "The product is: " << num * num2 << endl;
    cout << " ";
    cout << " ";
    break;

    default:
    cout<<"That is not an option, Nice Try Though!";
}
    }
        return 0;
        }



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1