Thread: The new FAQ

  1. #1
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    The new FAQ

    I've been working on a new format FAQ, which you can view here.

    I've used most of the content from Lightatdawn's FAQ, taken some stuff from the FAQ board, and written some of my own. I'll add more in due course. If there's anything specific you'd like to see in there, let me know. If you want to write anything yourself, feel free to do so, ask me for details on how to submit it.

    All comments/suggestions/corrections are welcome, either reply to this thread or PM me.

    Enjoy.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Nice job, hopefully it sees more use than the old one. I don't like the logo though :P

    edit: i think the vc++ series should be in the compiler/ide section, i was suprised that it is not.
    Last edited by RoD; 02-18-2003 at 08:24 AM.

  3. #3
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    geez Hammer... you're all involved in the community and making it better now... what gives?

    seriously... looks good. Keep up the superb work, my friend.

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    This got bumped down b4 it got seen, maybe sticky it? well....bumpety...

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>hopefully it sees more use than the old one
    I can but hope that newbies read it

    >>I don't like the logo though
    Yah well... it ain't that bad. I couldn't do too much in a 100x100 image. Still, this is about the FAQ itself, not the logo

    >>involved in the community, what gives
    Well, yonks ago I said I'd do this, so I thought I'd better keep my promise.

    [edit]Stickied for a day or two
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Looks good...For beginners that is, I now have to click 2 links to
    get to my answer!

    Yeah, I know, I'm a lazy bump

  7. #7
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Woohoo, my submission is up! Looks like we'll have to stop our late-night submission sodomizations, l@d

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Do you want me to do a C++ Style converting int to string?

    BTW, it looks nice.

    Code:
    #include <sstream> //this header automatically includes iostream and is needed for ostringstream
    #include <string> 
    using namespace std;
    
    string IntToString(int num); //this will be our converter function
    
    int main(void)
    {     int number=5;
          string result=IntToString(number);
          cout<<result<<endl;
          cin.get();
          return 0;
    }
    
    string IntToString(int num)
    {     ostringstream myStream; //creates an ostringstream object
          myStream<<num<<flush; 
          /*outputs the number into the string stream and then flushes
          the buffer (makes sure the output is put into the stream)*/
          return myStream.str(); //returns the string form of the stringstream object
    }
    Last edited by golfinguy4; 02-18-2003 at 10:18 PM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Do you want me to do a C++ Style converting int to string?
    Sure Help is welcome.

    If anyone wants to submit anything for this, here's a few guidelines:

    The code must be:
    - valid, ie it compiles and does what its supposed to
    - standard. Don't use things like getch() when there is no need
    - not over complicated for target audience.
    - documented. Try to put some decription text with your code. This is an FAQ afterall, not a snippets library.

    As for the formatting, just submit it to me in text, I'll sort out the HTML to go round it.

    [edit]
    Thanks golfinguy4, I see you've posted some code. I'll sort that out later. I'm going to be off in a minute
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Use this if you'd like...
    If you want, i'll write some more tommorow.
    Why does my program enter an infinite loop if the user inputs invalid data?
    Suppose you prompt the user to enter an integer value, and they enter something like "foo" or 'q'. Your istream-derived object (usually cin) attempts to extract an integral value from the input buffer, fails, and all subsequent input attempts fail.

    To remedy this, you must check your object for a fail state after extraction, flush the input buffer, and attempt another extraction.
    Code:
    using std::cout;
    using std::cin;
    
    // ...
    
    int input = 0;
    while (cout << "Enter a number: " && !(cin >> input))
    {
       cout << "\nInvalid input\n";
       cin.clear();
       cin.ignore(std::numeric_limits<int>::max());
    }
    Output
    Enter a number: Hello
    Invalid input
    Enter a number: d
    Invalid input
    Enter a number: 2

    Note: If you're unfamiliar with the funky std::numeric_limits<int>::max() statement, it's equivalent to the INT_MAX macro in <climits> or <limits.h>. Basically, it descripts the largest value an integer can hold. Include <limits> to use the std::numeric_limits template.

    std::endl vs. '\n'
    It's simple, std::endl sends a '\n' and flushes the output buffer. '\n' does not flush the output buffer.

    How can I get input without having the user hit <enter>?
    Using standard C/C++, you cannot. However, if you're lucky your compiler may have the non-standard conio.h header (which includes getch()), if you're on *nix (UNIX, Linux, etc.) you can try the ncurses library, else try the different API input routines (such as those included in the Win32 API)
    Last edited by Eibro; 02-18-2003 at 08:49 PM.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Thanks Eibro. I'll have a proper look at this tonight and put it up.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Hidoi Ryuujin
    Join Date
    Nov 2002
    Posts
    220
    I'm having trouble figuring out the section on unbuffered input. May be I just need to learn a little more before I try, but it seems so much more useful than cin.

    It might prove more effective to simplify the code (maybe even as far as something like saying to press enter and then spitting out whether they did or not, in this case). Complexity leads to confusion.
    Last edited by deathstryke; 02-21-2003 at 09:06 AM.
    One death is a tragedy, one million... a statistic.
    -Josef Stalin

    In case I forget, I use Bloodshed Dev C++ v.4

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @deathstryke: Are you meaning this entry? I'll write something easier to understand if so.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I'm going to write some more once I get some time.
    A fix to "Why does my program enter an infinite loop..."; below the code where I mention the std::numeric_limits<int> template, '<int>' is being treated as an HTML tag and it's not showing up.

    Edit:
    How can I convert a char/string to upper or lower case?
    For single characters converting to upper/lower case is simple, use the functions toupper() and tolower() included in ctype.h or cctype.
    For example:
    Code:
    #include <cctype>
    #include <iostream>
    
    int main(int argc, char** argv)
    {
       using std::cout;
       using std::toupper;
       using std::tolower;
    
       char letter = 'a';
       cout << "Before toupper: " << letter << endl;
       letter = toupper(letter);
       cout << "After toupper: " << letter << endl;
       letter = tolower(letter);
       cout << "After tolower: " << letter << endl;
    
       return 0;   
    }
    /*
      Output:
      Before toupper: a
      After toupper: A
      After tolower: a
    */
    For strings, it's a little more compilcated.
    First, it depends on what type of 'string' you're using. If it's traditional c-style strings (character arrays) then you'll need to iterate through the array and call tolower or toupper for each element.
    Code:
    #include <cctype>
    #include <iostream>
    
    int main(int argc, char** argv)
    {
       using std::cout;
       using std::endl;
    
       char hello[] = "Hello World";
       cout << "Before conversion: " << hello << endl;
    
       for (char* iter = hello; *iter != '\0'; ++iter)
       {
           *iter = std::tolower(*iter);
           ++iter;
       }
    
       cout << "After conversion: " << hello << endl;
    
       return 0;   
    }
    /*
       Output:
       Before conversion: Hello World
       After conversion: hello world
    */
    For C++-style strings of the std::string type, things are essentially the same. You need to iterate through each character and call tolower or toupper. Luckily, the STL includes an algorithm which does most of the work for us.
    Code:
    #include <algorithm>
    #include <string>
    #include <iostream>
    #include <cctype>
    
    int main(int argc, char** argv)
    {
       using std::string;
       using std::cout;
       using std::endl;
    
       string hello = "Hello World";
       cout << "Before conversion: " << hello << endl;
       std::transform(hello.begin(), hello.end(), hello.begin(), std::tolower);
    
       return 0;
    }
    /*
       Output:
       Same as above example
    */
    Edit again: I'm unsure whether tolower and toupper are in namespace std. VC6 doesn't seem to think so, but that compiler is far from standards compliant.
    Last edited by Eibro; 02-21-2003 at 11:02 PM.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    For the todo list:

    A sample directory walker:

    Code:
    /* 
    Note: your compiler may require preceding underscores for some of the functions/constants/data structures used here. 
    ie: _findfirst or _A_SUBDIR, etc...
    */
    
    #include <dirent.h>
    #include <string.h>
    #include <iostream>
    
    static char CURRENT_DIRECTORY[1024];
    
    void
     Search(const char * folder, const char * substring)
    {
    DIR * directory = opendir(folder);
    
    if(directory == NULL) return;
    
    chdir(folder);
    
    finddata_t * file = &directory->dd_dta;
    
    long index = findfirst("*", file);
    
       do
     {
      
          if(strstr(file->name, substring))
        {
            if(file->attrib & A_SUBDIR)
             cout << " Folder:" << endl;
            else
             cout << " File:" << endl;
            if(file->attrib & A_ARCH)
             cout << "(Archive)" << endl;
            if(file->attrib & A_SYSTEM)
             cout << "(System)" << endl;
            if(file->attrib & A_HIDDEN)
             cout << "(Hidden)" << endl;
            if(file->attrib & A_RDONLY)
             cout << "(Read Only)" << endl;
    
          getcwd(CURRENT_DIRECTORY, 1024);
    
          cout << CURRENT_DIRECTORY << "\\" << file->name << endl;
        }
    
          if(file->attrib & A_SUBDIR
             && strcmp(file->name, ".") != 0
             && strcmp(file->name, "..") != 0)
        {
          Search(file->name, substring);
          chdir("..");
        }
    
     } while(findnext(index, file) == 0);
    
     findclose(index);
    
     closedir(directory);
    
     return;
    }
    
    
    
    
    
    int main()
    {
      Search("c:\\", "config.sys");
      cout << endl << "Finished Searching.";
      cin.get();
    }


    A simple busy wait function:



    Code:
    #include <time.h>
    
    void wait(unsigned milliseconds)
    {
      unsigned timeout = clock() + milliseconds;
      while( clock() < timeout ) continue;
    }


    Two handy functions for newbies:



    Code:
    void say(const char * str, ...)
    {
     char quote[strlen(str)+1024];
     va_list v;
     va_start(v, str);
     vsprintf(quote, str, v);
     int length = strlen(quote);
        if(length && length < 80)
      {
        if(length%2)
         ++length;
        for(int i = 0; i < 40-length/2; ++i)
         cout << " ";
        cout << quote << endl;
      }
     cin.get();
    }
    
    
    void affirm(bool condition)
    {
     if(condition)
      say("Yes.");
     else
      say("No.");
    }
    
    
    
    int main()
    {
      int x = 4096;
      affirm(x > 4095);
    }
    Last edited by Sebastiani; 02-22-2003 at 08:43 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM
  3. FAQ - it is not a true list of FAQ
    By alpha561 in forum C Programming
    Replies: 1
    Last Post: 05-25-2002, 06:40 PM