Useful Header(maybe) C++ menu

User Tag List

Results 1 to 4 of 4
  1. #1
    Fireking300's Avatar Active Member
    Reputation
    27
    Join Date
    Jun 2007
    Posts
    92
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Useful Header(maybe) C++

    Here is a header that I have created. Any more ideas would be great or optimizing the current code would be better.

    Code:
    #ifdef WIN32
    	#include <windows.h>
    #endif
    #include <iostream>
    #include <ctime>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    void dans_sleep(double seconds)
    {
    	
    	#ifdef WIN32
    		sleep(seconds * 1000);
    	#else
    		usleep(seconds * 1000000);
    	#endif
    }
    
    void dans_clear(double seconds)
    {
    	#ifdef WIN32
    		dans_sleep(seconds);
    		system("cls");
    	#else
    		dans_sleep(seconds);
    		system("clear");
    	#endif
    }
    
    void dans_clear()
    {
    	#ifdef WIN32
    		system("cls");
    	#else
    		system("clear");
    	#endif
    }
    
    void dans_pause()
    {
    	cout << "Please hit enter to continue...";
    	cin.get();
    }
    
    
    void dans_swap(int &a, int &b)
    {
    	int temp = a;
    	a = b;
    	b = temp;
    }
    
    void dans_swap(char &a, char &b)
    {
    	char temp = a;
    	a = b;
    	b = temp;
    }
    
    void dans_swap(double &a, double &b)
    {
    	double temp = a;
    	a = b;
    	b = temp;
    }
    
    void dans_swap(float &a, float &b)
    {
    	float temp = a;
    	a = b;
    	b = temp;
    }
    
    void dans_ascii(char *world, int width, int height)
    { 
        for(int a = 0; a < height; a++)
        {
            for(int b = 0; b < width; b++)
            {
                cout << *(world + (a * width) + b);
            }
            cout << endl;
        }
    }
    
    void dans_gotoxy(int x, int y) 
    {
    	#ifdef WIN32
    		COORD coord;
    		coord.X = x;
    		coord.Y = y;
    		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    	#else
    		char essq[100];
    		char xstr[100];
    		char ystr[100];	
    		sprintf(xstr, "%d", x);
    		sprintf(ystr, "%d", y);
    		essq[0] = '\0';
    		strcat(essq, "\033[");
    		strcat(essq, ystr);
    		strcat(essq, "d");
    		strcat(essq, "\033[");
    		strcat(essq, xstr);
    		strcat(essq, "G");
    		printf("%s", essq);
    	#endif
    }
    Last edited by Fireking300; 03-23-2010 at 06:05 AM.

    Useful Header(maybe) C++
  2. #2
    flo8464's Avatar Active Member
    Reputation
    30
    Join Date
    Apr 2009
    Posts
    434
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. Don't use sytem("cls"). It has an incredible overhead. Use your dans_gotoxy and simply overwrite the console buffer with spaces.

    2. Your swap functions could be generic by using templates.

    This is a generic one, works with every type:

    Code:
    template<typename T>
    void dans_swap(T& a, T& b)
    {
    	T temp = a;
    	a = b;
    	b = temp;
    }
    3. Headers of the C-Library don't have the suffix .h anymore, instead they have the prefix c. For example <stdio.h> becomes <cstdio>

    4. This leads to the next point ... don't use C-Libraries, if there are C++ solutions
    Last edited by flo8464; 03-22-2010 at 07:36 PM.
    Hey, it compiles! Ship it!

  3. #3
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1358
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/6
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Fireking300 View Post
    Here is a header that I have created. Any more ideas would be great or optimizing the current code would be better.

    Code:
    #ifdef WIN32
    	#include <windows.h>
    #endif
    #include <iostream>
    #include <ctime>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    void dans_sleep(double seconds)
    {
    	
    	#ifdef WIN32
    		sleep(seconds * 1000);
    	#else
    		usleep(seconds * 1000000);
    	#endif
    }
    
    void dans_clear(double seconds)
    {
    	#ifdef WIN32
    		dans_sleep(seconds);
    		system("cls");
    	#else
    		dans_sleep(seconds);
    		system("clear");
    	#endif
    }
    
    void dans_clear()
    {
    	#ifdef WIN32
    		system("cls");
    	#else
    		system("clear");
    	#endif
    }
    
    void dans_pause()
    {
    	cout << "Please hit enter to continue...";
    	cin.get();
    }
    
    
    void dans_swap(int &a, int &b)
    {
    	int temp = a;
    	a = b;
    	b = temp;
    }
    
    void dans_swap(char &a, char &b)
    {
    	char temp = a;
    	a = b;
    	b = temp;
    }
    
    void dans_swap(double &a, double &b)
    {
    	double temp = a;
    	a = b;
    	b = temp;
    }
    
    void dans_swap(float &a, float &b)
    {
    	float temp = a;
    	a = b;
    	b = temp;
    }
    
    void dans_ascii(char *world, int width, int height)
    { 
        for(int a = 0; a < height; a++)
        {
            for(int b = 0; b < width; b++)
            {
                cout << *(world + (a * width) + b);
            }
            cout << endl;
        }
    }
    
    void dans_gotoxy(int x, int y) 
    {
    	#ifdef WIN32
    		COORD coord;
    		coord.X = x;
    		coord.Y = y;
    		SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    	#else
    		char essq[100];
    		char xstr[100];
    		char ystr[100];	
    		sprintf(xstr, "%d", x);
    		sprintf(ystr, "%d", y);
    		essq[0] = '\0';
    		strcat(essq, "\033[");
    		strcat(essq, ystr);
    		strcat(essq, "d");
    		strcat(essq, "\033[");
    		strcat(essq, xstr);
    		strcat(essq, "G");
    		printf("%s", essq);
    	#endif
    }
    If that's a header file there are quite a few problems.

    1. You're defining functions in your header that have external linkage. This will cause multiple-definition linker errors. Mark them as inline if you really want to define them in a header.
    2. Never never never never never NEVER put a using declaration in a header file. That causes pollution in every file that includes your header.
    3. Your swap functions are useless.
    4. Your pause function will not work if the stream is in an invalid state. Clear and sync the stream before calling istream::get.
    5. Your sleep function won't compile because you haven't included the appropriate header for non-windows platforms. At any rate, that's a really disgusting solution because you assume that the function 'usleep' is available for all non-windows platforms, which afaik is wrong. If you want cross-platform threading just use Boost.Thread (that will also make it easier to transition to C++0x's threading library as Boost.Thread was used as a 'base').
    6. Your 'clear' function suffers from the same cross-platform problem (not to mention that using 'system' to implement it is about as yuck as you can get).
    7. dans_ascii is a very unsafe function. STL strings are there for a reason. Use them.
    8. I don't even want to know what gotoxyz does.


    Originally Posted by flo8464 View Post
    1. Don't use sytem("cls"). It has an incredible overhead. Use your dans_gotoxy and simply overwrite the console buffer with spaces.

    2. Your swap functions could be generic by using templates.

    This is a generic one, works with every type:

    Code:
    template<typename T>
    void dans_swap(T& a, T& b)
    {
    	T temp = a;
    	a = b;
    	b = temp;
    }
    3. Headers of the C-Library don't have the suffix .h anymore, instead they have the prefix c. For example <stdio.h> becomes <cstdio>

    4. This leads to the next point ... don't use C-Libraries, if there are C++ solutions

    Errr, speaking of there being C++ solutions:
    swap

    There's no need to re-implement swap at all unless you're going to be doing work that is specific to a given type. The STL provides a generic implementation for a reason.

    Also, your implementation is "wrong" anyway because it uses the assignment operator rather than the constructor to create the temporary object. That's unnecessary overhead as it results in one constructor call and then one assignment operator call, rather than just a constructor call.
    Last edited by Cypher; 03-25-2010 at 11:35 AM.

  4. #4
    Fireking300's Avatar Active Member
    Reputation
    27
    Join Date
    Jun 2007
    Posts
    92
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the suggestions I will work right away to rectify these issues.

Similar Threads

  1. [VIDEO] Vlog - How I personally use M2Mod4 - Maybe helpful for others
    By theduncehat in forum WoW Advanced Model Edits
    Replies: 0
    Last Post: 04-14-2015, 10:08 PM
  2. Replies: 1
    Last Post: 09-22-2011, 12:18 AM
  3. maybe useful Gundrak bug
    By Xonic in forum World of Warcraft Exploits
    Replies: 4
    Last Post: 12-05-2008, 07:42 PM
  4. [Maybe useless] Longer overpower use
    By Sylvan in forum World of Warcraft Exploits
    Replies: 5
    Last Post: 11-05-2008, 01:59 PM
All times are GMT -5. The time now is 07:24 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search