|
|
#1 |
|
Prospect
Join Date: Jan 2002
Posts: 16
|
To those who code in C++...
Does anyone have an explanation for this...?
compile this simple code under both Devtools g++/c++ 3.1 and c++ 2.95. Code:
#include <iostream>
using std::cout;
using std::cin;
int main()
{
char a[8];
cin.getline(a, 8);
cout << a;
return 0;
}
|
|
|
|
|
|
#2 |
|
Prospect
Join Date: Jul 2002
Location: Durham, England
Posts: 2
|
I get no different behaviour whatsoever using c++3 (gcc 3.1) or c++ (gcc 2.95.2) to compile this -- using the April (beta) version of the devtools. Need to hit return once after my input to get it echoed and the program quit.
|
|
|
|
|
|
#3 |
|
Prospect
Join Date: Jan 2002
Posts: 16
|
Oh, well I'm using 3.1 from cvs (1200 20020420). Didn't specify any unusual options during the install process though. I guess everything will be ok when jaguar comes out. Thanks.
|
|
|
|
|
|
#4 |
|
Moderator
Join Date: Jan 2002
Location: Singapore
Posts: 4,237
|
Another issue with the gcc3 compiler is an incompatibility for C++ ABIs between gcc2 and gcc3. In practice, this means that C++ programs compiled with gcc3 cannot link to libraries compiled with gcc2.
Cheers... |
|
|
|
|
|
#5 |
|
Moderator
Join Date: Jan 2002
Location: Singapore
Posts: 4,237
|
http://developer.apple.com/techpubs/...otes/GCC3.html
says: • Recompile all your C++ code, including libraries and frameworks. GCC 3 has a new application binary interface (ABI) for C++, including changes to name mangling, exception handling, and class layout and alignment. You do not need to recompile C or Objective-C code. The version of gcc3 that's tagged gcc-1151 will be released with Jaguar. Cheers... |
|
|
|
|
|
#6 |
|
Prospect
Join Date: Jan 2002
Posts: 16
|
Recently got jaguar and I noticed that I am still having this problem. I fresh installed the os and software (wiped drive) w/ the retail dev tools + the august update (1161), so it's not the 'old baggage' that's causing the problem. Using GCC2 works as expected, but GCC3 is screwing it up.
Try this... both getlines requires 2 inputs and the second getline gets the 2nd input of the first getline. even with cin.sync() or cin.clear(), the second getline seems to keep getting input from the buffer. Code:
#include <iostream>
using namespace std;
int main()
{
char a[8];
cout << "A1: ";
cin.getline(a, 8);
cout << "a is " << a << endl;
cout << "A2: ";
cin.getline(a, 8);
cout << "a is " << a << endl;
return 0;
}
Code:
A1: TEST1 TEST2 a is TEST1 A2: TEST3 a is TEST2 [EDIT] Just tried it out on sun solaris machines running gcc 3.0.3, and it works fine there. Wondering why i am getting this problem, as I am using the retail tools. Last edited by c15zyx; 09-16-2002 at 10:04 AM. |
|
|
|
|
|
#7 |
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 31,939
|
known bug in gcc 3.1
This is a known bug in gcc 3.1 (it got fixed in gcc 3.1.1).
For more details, go to the gcc bug tracking page: http://gcc.gnu.org/cgi-bin/gnatsweb.pl and View bug # 6648 Unfortunately, the latest Developer Tools use gcc 3.1, so it seems we are stuck with this bug until the next update. But here's a replacement function that I wrote: Code:
// myGetline:
// The gcc 3.1 version of the 'getline' function has a bug - it requires
// two delimiters (e.g. newlines) before it accepts the input
// The 'myGetline' function provides a replacement for 'getline'
// Sample use: char name[100]; myGetline(cin, name, 100);
istream& myGetline(istream& in, char* buffer, streamsize n, char delim = '\n')
{
if (in.peek() == delim)
{
// the gcc 3.1 version of the 'get' function seems to have a bug
// that causes it to fail if there are no characters before the
// delimiter (e.g. an empty line)
buffer[0] = '\0';
}
else
{
in.get(buffer, n, delim);
}
if (in.good() && strlen(buffer) < n - 1)
{
// eat the delimiter
in.ignore(1);
}
return in;
}
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|