Calculator fail.

#include <iostream>
#include <cmath>
using namespace std;

int main(){

cout << "Press 1 for addition.\n" << "Press 2 for subtraction.\n" << "Press 3 for multiplication.\n" << "Press 4 for divison.\n";

int array[50];

cin >> array[1]; cout << endl;


if(array[1]=1)
{
cout << "Pick 2 numbers to add";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]+array[2];
cout << endl;
cout << array[4];
cout << endl;
}


if(array[1]=2)
{
cout << "Pick 2 numbers to subtract";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]-array[2];
cout << endl;
cout << array[4];
cout << endl;
}

if(array[1]=3)
{
cout << "Pick 2 numbers to multiply";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[3]*array[2];
cout << endl;
cout << array[4];
cout << endl;
}

if(array[1]=4)
{
cout << "Pick 2 numbers to subtract";
cout << endl;
cin >> array[2];
cout << endl;
cin >> array[3];
cout << endl;
array[4]=array[2]/array[3];
cout << endl;
cout << array[4];
cout << endl;
}

else
{
cout << "I'm sorry. That is invalid";
return main();
}



return 0;
}


Can someone tell me why the else function won't work?
When I did 5 it did the add function.
= is an assignment operator.
== is a test operator.
More exactely, in the if statement I think that you wanted to compare the second array-component with the integer value - 1. So what are you doing is wrong because if(array[1]=1) tests if that array-component can assign the integer 1 which is always true. I think you wanted to write if(array[1]==1) which tests if that component of array has memorized the value 1. As ciphermagi says, == is a test operator.
Your program seems to be a bit buggy. you need to nest your if statements. as of now you choose option one but it runs through the other options as well.

if(statement)
{
text
else if(statement)
{
else if(statement)
text
else
text
}
}
then the propper amount of closing brackets.
Last edited on
else is not a function
main is a function
if, else-if, else are functions
while, do-while, for are loops
if, else-if, else are functions

(emphasis mine)

if, else if, and else are not functions. They're basic control structures like loops.
sorry typo thats what i meant
Topic archived. No new replies allowed.