Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with this code? 2

Status
Not open for further replies.

mover50

Programmer
Aug 3, 2004
77
US
Getting this error...

[mover50@localhost C++]$ g++ prog3-06.cpp -o prog3-06
prog3-06.cpp:7: error: syntax error before `^' token
prog3-06.cpp: In function `int main()':
prog3-06.cpp:20: error: syntax error before `^' token
prog3-06.cpp: At global scope:
prog3-06.cpp:25: error: syntax error before `^' token

with this code..

/* Program #3-06 Demonstrate the xor() function
*/

#include <iostream>
using namespace std;

bool xor(bool a, bool b);

int main()
{
bool p, q;

cout << "Enter P (0 or 1): ";
cin >> p;
cout << "Enter Q (0 or 1): ";
cin >> q;

cout << "P and Q: " << (p && q) << "\n";
cout << "P or Q: " << (p || q) << "\n";
cout << "P xor Q: " << xor(p, q) << "\n";

return 0;
}

bool xor(bool a, bool b)
{
return (a || b) && !(a && b);
}

Where is the error?

Thanks,

Kent
 
xor is a keyword that means the same as the ^ operator. Use a different name for your function, or just use the built-in xor or ^ operator.
 
xor is a reversed name, change the name of that function.
 
Thanks,

Was not aware that there was an xor operator. My book does not mention it copyright date of 2003.

Kent
 
I just verified it in my book, copyright 1997, before I posted. Of course, my book is Stroustrup's The C++ Programming Language, so I guess if anybody should have that info in their book it's the language creator. ;-)
 
I have the C++ from the ground up by Osborne and on page 51 it says 'although C++ does not contain a built-in xor operator,it is easy to construct one'.
Then on page 198 it shows the XOR ^ operator. It's there. Hate it when it confuses me like that.

Thanks,

Kent

Kent (the worrier)
 
Well, remember that there are both logical and bitwise AND/OR/XOR operations.

& and | are the bitwise AND and OR operators; there is a bitwise XOR operator (^).

&& and || are the logical AND and OR operators; there really isn't a logical XOR operator. You need to write a function for it... you just can't call it "xor" (just like you can't call it "for" (well, not exactly, but...)).


If I'm not mistaken, the xor keyword was added because on some (foreign, for example) keyboards, the carat (^) key does not exist. An alternative using only English alphabetic characters was provided for convenience.

My reference guides say that you have to [tt]#include <isi646.h>[/tt] to get the xor keyword, implying that it contains a line [tt]#define xor ^[/tt]. That would explain the error, since your code would be saying things like:
Code:
bool ^(bool a, bool b);
after preprocessing.

At least, it would explain it if you were actually including that header. I'd have to guess that <iostream> is including it indirectly, for whatever reason.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top