-
Should this work?
I got this code from MSDN for a FAQ for Visual C++ 4.2
I saw this code, ran my eyes over it and I am wondering if the code is legal, according to the standard.
Code:
//piglatin.cpp
#include <string>
#include <iostream>
//convert a string to piglatin
string piglatin(const string& s)
{
string s1 ;
string s2(" .,;:?") ; //word separators
//word boundary markers
size_t start, end, next, p0 ;
int done = 0 ;
start = end = next = 0 ;
while (!done)
{
// Find start of word.
start = s.find_first_not_of(s2, next) ;
// Find end of word.
// Check for end of string.
p0 = s.find_first_of(s2, start) ;
end = (p0 >= s.length()) ? s.length() : p0 - 1 ;
// Copy all the word separators.
s1 = s1 + s.substr(next, start - next) ;
// Convert word to piglatin.
s1 = s1 + s.substr(start + 1, end - start) + s[start] + "ay" ;
next = end + 1;
// Check for end of string.
if( next >= s.length())
done = 1 ;
}
return s1 ;
}
void main()
{
string s("she sells sea shells by the sea shore") ;
cout << "s = " << s << endl ;
cout << "\npiglatin(s) = " << piglatin(s) << "\n"<< endl;
}
you know, even in the recent VC++ tutorial, they still used void main()...
no wonder windows has so many problems.
note: haven't tried compiling yet but I know it will compile...
Shouldn't it say
Code:
using namespace std;
under the includes...
also, i really think void should be changed to int (on main) and a return 0; added to the bottom. OR AT LEAST
__asm mov eax, 0x0
right?
-LC
-
I take that back, their own tutorial won't even compile:
Code:
--------------------Configuration: haha - Win32 Debug--------------------
Compiling...
haha.cpp
d:\program files\microsoft visual studio\myprojects\haha\haha.cpp(5) : error C2146: syntax error : missing ';' before identifier 'piglatin'
d:\program files\microsoft visual studio\myprojects\haha\haha.cpp(5) : error C2501: 'string' : missing storage-class or type specifiers
d:\program files\microsoft visual studio\myprojects\haha\haha.cpp(5) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
haha.exe - 3 error(s), 0 warning(s)
LOL
-LC
-
With the addition of using namespace std, changing main to int, and adding a return value of 0 it compiles fine for me on gcc.
-
yeah thats what I thought...
but I can't believe this actually worked on the older compiler for Microsoft. The newer tutorial hasn't changed much either though...