Whats wrong with my code?

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;
}

double percent (double x, double y);
double percent (double x, double y)
{
double total, t;
t = x/y;
total = t*100;
return (double)total;
}


int main(int argc, const char * argv[])

{

double n, x, y;
string operation;

while (true)
{
cout << "What operation would you like to run? (+, -, *, /, sqrt, modular, power, gcf, lcm, absolute, factorial, triangle, divisors, percent) (c to exit)" << endl;
cin >> operation;


if (operation == "c"){
break;
}
else if (operation == "+" || "-" || "*" || "/" || "modular" || "power" || "gcf" || "lcm" || "percent")
{
cout << "Insert first number" << endl;
cin >> x;
cout << "Insert second number" << endl;
cin >> y;

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;}

}

else if (operation == "sqrt" || "factorial" || "triangle" || "divisors"){

cout << "Insert number" << endl;
cin >> n;

if (operation == "sqrt"){
cout << "The square root is " << sqrt_n(n)<<endl; }
else if (operation == "absolute") {
absolute(n);
cout << endl;}
else if (operation == "factorial"){
cout << "The factorial is " << factorial (n)<<endl;}
else if (operation == "triangle"){
cout << "The triangle is " << triangle(n)<<endl;}
else if (operation == "divisors"){
divisors(n);
cout << endl;}

}

}
return 0;
}
closed account (2LzbRXSz)
Your if statements don't really specify what the condition needs to be. The compiler isn't going to just assume, it'll look at that and say "or what?"

This can be fixed by writing your if statements like this:
else if (operation == "+" || operation == "-" || operation == "*" || operation == "/" || operation == "modular" || operation == "power" || operation == "gcf" || operation == "lcm" || operation == "percent")

else if (operation == "sqrt" || operation == "factorial" || operation == "triangle" || operation == "divisors")
Topic archived. No new replies allowed.