invalid types ‘double[int]’ for array subscript

hi guys
i am developing a calculator through a certain tutorial and i came up with the following errors

roncriss@linux-box:~/Desktop/linux calculator> g++ main.cpp
In file included from main.cpp:4:
calculator.cpp: In member function ‘int Calc::Operation(std::string)’:
calculator.cpp:47: error: invalid types ‘double[int]’ for array subscript
calculator.cpp:49: error: invalid types ‘double[int]’ for array subscript
calculator.cpp:55: error: expected `}' before ‘else’
calculator.cpp:57: error: ‘else’ without a previous ‘if’
calculator.cpp:59: error: ‘else’ without a previous ‘if’
calculator.cpp:62: error: invalid types ‘double[int]’ for array subscript
calculator.cpp: At global scope:
calculator.cpp:65: error: expected unqualified-id before ‘else’
calculator.cpp:71: error: expected declaration before ‘}’ token
roncriss@linux-box:~/Desktop/linux calculator>


here are my source code:

calculator.cpp

class Calc
{
public:
Calc();
double Number; //Answer
int Operation(string); //work with client code
int stack_spot; //hold the spot in the stack
private:
double s2f(string); //converts from string to double
double stack; // stack that hold data
void Add();
void Sub();
void Mult();
void Div();
void Sqrt();
void Equal();
void Clear();
double SquareRoot(double);
double Abs(double);
};
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
Calc :: Calc()
{
stack_spot = 0;
Number = 0;
}
/***************************************************************************/
/***************************************************************************/
/***************************************************************************/
int Calc :: Operation (string n)
{
if( n == "sqrt")
{
cout<<stack_spot<<endl;
Sqrt();
stack_spot = 1;
cout<<stack_spot<<endl;
return -1;
}
else if (n == "Clear")
{
Clear();
return -1;
}
else if (stack_spot == 0)
{
stack[0] = s2f(n);
stack_spot++;
Number = stack[0];
}
else if (stack_spot == 1)
{
if (n == "+");
n == "1";
else if (n == "-");
n == "2";
else if (n == "*");
n == "3";
else if (n == "/");
n == "4";

stack[stack_spot] =s2f(n);
stack_spot++;
}
else if(stack_spot == 2)
{
stack[2] == s2f(n);
Equal ();
stack_spot = 1;
}
}
/*********************************************************** ****************/
/***************************************************************************/
/***************************************************************************/
void Calc :: Sqrt()
{


}
/*********************************************************** ****************/
/***************************************************************************/
/***************************************************************************/
void Calc :: Clear()
{

}
/*********************************************************** ****************/
/***************************************************************************/
/***************************************************************************/
void Calc :: s2f(string s)
{

}
/*********************************************************** ****************/
/***************************************************************************/
/***************************************************************************/
void Calc :: Equal()
{

}
/*********************************************************** ****************/
/***************************************************************************/

and main.cpp


#include<iostream>
#include <stdlib.h>
using namespace std;
#include "calculator.cpp"

int main()
{
system ("clear");
Calc a;
string s ="q";
while (s !="exit")
{
cout<<"MATH:>";
cin>> s;
a.Operation(s);
}
return 0;

}

is anyone can help me with this?
Last edited on
Please use code tags when posting code.

First, you are using the "stack" member as an array in Operation() but it is defined
as a double, not an array of doubles.

Second, you have stray semicolons at the end of some of your if() statements:

1
2
3
4
5
6
7
8
if (n == "+"); //<- remove semicolon
n == "1";
else if (n == "-");  //<- remove semicolon
n == "2";
else if (n == "*"); //<- remove semicolon
n == "3";
else if (n == "/"); //<- remove semicolon
n == "4";


Thanks man you have been very helpful!
i discovered other errors also through your help,now i can compile without problem.
Topic archived. No new replies allowed.