As a first year c++ coder, i was intreaged by the language as soon as i sat down in the classroom. However, on a school assighnment,I have come upon an error that is unfamiliar to me. The assighnment was to simplifly a program that was far too complex. The end result was a simple four function calculator. I typed up the code and when i compiled it i came across an error called a parse error, one that we hadnt reviewed in the classroom. My code was as follows:

#include <iostream.h>
#include <stdlib.h>

 main()
{
 float b, a;
 float c;
 char x[2];

 cout<< "What two numbers would you like to use the operation on?";
 cin>>a>>b;
 cout<< "What is the operation? ( [+] or [-] or [*] or [/] )";
 cin.get(x, 2);
 cin.ignore(80, '\n');
 c=a[x]b;
 cout << "Your answer is " <<c<<'\n' ;
 system("PAUSE");
 return 0;
}

<< moderator edit: added code tags: [code][/code] >>

The errors showed up as follows:

(line 17) parse error before `='

after hours of trying to correct the error, i typed up an alternate method and turned it in for the grade. But this error still confuses me.
Please Help!!!

Do you intend to multiply a[x] and b?

c=a[x]b;

[Dani - again I'd really love to have fixed-point fonts back. And font sizes for this portion.]

I intended to use x as char variable, to enable the user to input the operation to be used in the equation
that was the purpose of this:
cout<< "What is the operation? ( [+] or [-] or[*] or [/] )";
cin.get(x, 2);
cin.ignore(80, '\n');

then to use the operation given on a and b
a[x]b

Ex: User puts "-" in as input, then a-b=c. or if "/", then a/b=c

Ex: User puts "-" in as input, then a-b=c. or if "/", then a/b=c

That's what I thought; you can't do that. Source code has its meaning at compile time. At run time, text is not source code -- it's just text. If you want to interpret text into an operation, you'd need to write code to do that. Something like:

switch ( op )
{
   case '+': c = a + b; break;
   case '-': c = a - b; break;
   case '*': c = a * b; break;
   case '/': c = a / b; break; /* should check that b != 0 */
   default: break;
}

That's what I thought; you can't do that. Source code has its meaning at compile time. At run time, text is not source code -- it's just text. If you want to interpret text into an operation, you'd need to write code to do that. Something like:

switch ( op )
{
   case '+': c = a + b; break;
   case '-': c = a - b; break;
   case '*': c = a * b; break;
   case '/': c = a / b; break; /* should check that b != 0 */
   default: break;
}

ok, that makes more sense now.
Thank you

This article has been dead for over six months. Start a new discussion instead.