Whenever i input "sqrt", "divisors", "factorial", or "triangle" it asks me for two numbers! Any help would be greatly appreciated.
Here is the code.
//
// main.cpp
// Calculator
//
// Created by elinder17 on 4/30/15.
// Copyright (c) 2015 elinder17. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
int add(int x, int y);
int add(int x, int y)
{
int total;
total = x+y;
return total;
}
int subtract(int x, int y);
int subtract (int x, int y)
{
int total;
total = x-y;
return total;
}
int multiply(int x, int y);
int multiply(int x, int y)
{
int total;
total = x*y;
return total;
}
void divide(int x, int y);
void divide(int x, int y)
{
if (y == 0){
cout << "Error";}
else {
int total;
total = x/y;
cout << "The quotient is " << total;}
}
double sqrt_n(int n);
double sqrt_n(int n)
{
int total;
total = sqrt(n);
return total;
}
int modular(int x, int y);
int modular(int x, int y)
{
int total;
total = x%y;
return total;
}
int power (int x, int y);
int power(int x, int y)
{
int tot = 1;
for (int i = 0; i < y; i++)
{
tot = tot*x;
}
return tot;
}
int gcf(int x, int y);
int gcf(int x, int y)
{
if (y == 0)
{
return x;
}
else
{
return gcf (y, x%y);
}
}
int lcm(int x, int y);
int lcm(int x, int y)
{
int lcm, gc, n, b;
gc = gcf (x, y);
n = x / gc;
b = y / gc;
lcm = n * gc * b;
return lcm;
}
void absolute(int n);
void absolute(int n)
{
int total;
if (n >= 0)
{
total = n;
cout<< "The absolute value of " << n << " is " << total <<endl;
}
else if ( n < 0)
{
total = n + (-n *2);
cout << "The absolute value of " << " is " << total << endl;
}
}
double factorial(int a);
double factorial(int a)
{
int ans = 1;
for (int i = 1; i<= a; i++)
{
ans = i*ans;
}
return ans;
}
int triangle (int n);
int triangle (int n)
{
int ans = 1;
if (n ==1)
{
return 1;
}
else{
ans = n + triangle(n-1);
return ans;
}
}
void divisors(int n);
void divisors(int n)
{
int i;
for (i = 2; i <= sqrt(n); i++)
if (n % i == 0){
cout << i << ", ";
divisors(n/i);
return;
}
cout << n;
}
if ( operation == "+"){
cout << "The sum is " << add(x,y);}
else if ( operation == "-"){
cout << "The difference is " << subtract (x,y) << endl;}
else if ( operation == "*"){
cout << "The product is " << multiply (x,y) << endl;}
else if (operation == "/"){
divide(x,y);
cout << endl;}
else if (operation == "power"){
cout << "The product is " << power(x, y)<< endl;}
else if (operation == "modular"){
cout << "The remainder is " << modular(x,y) << endl;}
else if (operation == "gcf"){
cout << "The gcf is " << gcf(x,y) << endl;}
else if (operation == "lcm"){
cout << "The lcm is " << lcm(x,y)<<endl;}
else if (operation == "percent"){
cout << x << " out of " << y << " is " << percent(x,y) << "%"<< endl;}