Thread: Should this work?

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    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
    Last edited by Lynux-Penguin; 08-04-2003 at 12:05 AM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    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.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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...
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM