Compile Error!

Post here if you don't find any other place for your post. But please, stay on-topic: algorithms, programming or something related to this web site and its services.

Moderator: Board moderators

Post Reply
freaky
New poster
Posts: 6
Joined: Sat Sep 14, 2002 11:27 pm
Location: Taipei

Compile Error!

Post by freaky »

:(
The following code gets tons of compiling errors from the online judge.
Why does the library remain non-standard for this long?
[cpp]
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;

bool judge(const string &s) {
string temp(s);
string::iterator newEnd = remove(temp.begin(), temp.end(), '-');
temp.erase(newEnd, temp.end());

if (temp.size() != 10)
return false;
else {
const char* number = temp.c_str();
int sum = 0;
size_t index = 0;
for (; index != (temp.size()-1); ++index) {
if (isdigit(number[index]) != 0) {
sum += (10 - index) * (number[index] - '0');
continue;
}
else
return false;
}
sum += (number[index] == 'X') ? 10 : (number[index] - '0');

if ((sum % 11) == 0)
return true;
else
return false;
}
}

int main() {

string input;
string::iterator newEnd;

while (getline(cin, input)) {
newEnd = remove(input.begin(), input.end(), ' ');
input.erase(newEnd, input.end());

if (judge(input) == true)
cout << input << " is correct." << endl;
else
cout << input << " is incorrect." << endl;

}

return 0;
}
[/cpp]

Here are the compiler error messages (the real list is more than ten times longer):

01088009_24.c:3: syntax error before `a'
In file included from /usr/include/_G_config.h:44,
from /usr/include/libio.h:32,
from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/streambuf.h:36,
from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/iostream.h:31,
from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3/iostream:6,
from 01088009_24.c:12:
/usr/include/gconv.h:72: type specifier omitted for parameter
/usr/include/gconv.h:72: parse error before `*'
....
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

It compiles when I submit it. Certainly your mailer is adding some text, try with:
/* @begin_of_source_code */
Code
/* @end_of_source_code */
freaky
New poster
Posts: 6
Joined: Sat Sep 14, 2002 11:27 pm
Location: Taipei

Post by freaky »

Adrian Kuegel wrote:It compiles when I submit it. Certainly your mailer is adding some text, try with:
/* @begin_of_source_code */
Code
/* @end_of_source_code */
Thanks, but here's another compile error, this time it's definitely the library's fault:

[cpp]
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
string temp;
string denominator("1000000000");
unsigned long int p1 = 0;
unsigned long int p2 = 0;
unsigned long int q1 = 0;
unsigned long int q2 = 0;
unsigned long int ptemp = 0;
unsigned long int qtemp = 0;
unsigned long int a, b;
int j;
unsigned int N = 0;
unsigned int k = 0;
while(cin >> j) {
if (j != -1)
cin >> temp;
else
break;
++N;
k = static_cast<unsigned int>(temp.size()-2-j);
istringstream(string(temp.substr(2))) >> p1;
istringstream(string(((j != 0)&&(k != 0)) ? temp.substr(2, k) : "0")) >> p2;
ptemp = p1 - p2;
istringstream(string(denominator.substr(0, j+k+1))) >> q1;
istringstream(string((j != 0) ? denominator.substr(0, k+1) : "0")) >> q2;
qtemp = q1 - q2;

if (ptemp == 0)
cout << "Case " << N << ": " << 0 << "/" << 1 << endl;
else if (ptemp == qtemp)
cout << "Case " << N << ": " << 1 << "/" << 1 << endl;
else {
unsigned long int temp = qtemp;
a = ptemp;
while (temp != 0) {
b = temp;
temp = a % temp;
a = b;
}

cout << "Case " << N << ": " << ptemp/a << "/" << qtemp/a << endl;
}
}

return 0;
}
[/cpp]

What's wrong with my usage of istringstream?

Here are the compiler error messages:

01088993_24.c: In function `int main()':
01088993_24.c:29: parse error before `>'
01088993_24.c:30: redeclaration of `class istringstream string'
01088993_24.c:29: `class istringstream string' previously declared here
01088993_24.c:30: parse error before `>'
01088993_24.c:32: redeclaration of `class istringstream string'
01088993_24.c:30: `class istringstream string' previously declared here
01088993_24.c:32: parse error before `>'
01088993_24.c:33: redeclaration of `class istringstream string'
01088993_24.c:32: `class istringstream string' previously declared here
01088993_24.c:33: parse error before `>'
Adrian Kuegel
Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany

Post by Adrian Kuegel »

try it like this:
[cpp]istringstream istr1(string(temp.substr(2)));
istr1 >> p1;
istringstream istr2(string(((j != 0)&&(k != 0)) ? temp.substr(2, k) : "0"));
istr2 >> p2;
ptemp = p1 - p2;
istringstream istr3(string(denominator.substr(0, j+k+1)));
istr3 >> q1;
istringstream istr4(string((j != 0) ? denominator.substr(0, k+1) : "0"));
istr4 >> q2; [/cpp]
freaky
New poster
Posts: 6
Joined: Sat Sep 14, 2002 11:27 pm
Location: Taipei

Post by freaky »

Adrian Kuegel wrote:try it like this:
[cpp]istringstream istr1(string(temp.substr(2)));
istr1 >> p1;
istringstream istr2(string(((j != 0)&&(k != 0)) ? temp.substr(2, k) : "0"));
istr2 >> p2;
ptemp = p1 - p2;
istringstream istr3(string(denominator.substr(0, j+k+1)));
istr3 >> q1;
istringstream istr4(string((j != 0) ? denominator.substr(0, k+1) : "0"));
istr4 >> q2; [/cpp]
ok, I know this works, but why does the previous code not compile?
I write pure standard C++ codes, and they compile fine in Comeau C++ and VC (yes, even that notoriously non-compliant VC, actually their library (from dinkumware) is quite conformance).
I'm somewhat frustrated by this issue and thus quit submitting for two years. Hope things could get better in the near future.
By the way, do I miss something that I get PE? Thank you for your help again, Andrian!
Post Reply

Return to “Other words”