Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 38

Thread: C Problems

  1. #11
    Join Date
    Mar 2004
    Posts
    123
    Rep Power
    0

    Default Re: C Problems

    Quote Originally Posted by Skillachi
    can you tell me the ascii equivalent codes for like... all the keys on the keyboard or gimme a link to them... (i mus get at least a 95% now... mwahahahaha)

    And my final question, when i use the ascii code does case still matter... as in lets say the ascii code for the letter "g" is 20... if the person say enters G would the ascii be 21? or would it still be read as 20?
    Links to ASCII values:
    www.lookuptables.com
    www.newebgroup.com/rod/newillusions/ascii.htm
    There is no wrong or right, only consequencies to action

    "It is the duty of man to make his knowledge so complete in life, so as to make it impossible for any other to take advantage of him" - Marcus Garvey

  2. #12
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Re: C Problems

    thats easy write this code in borland and run it.

    Code:
    int main() {
    
        char c;
        while(1) {
            c = getch();
            printf("%d\n", c);
        }
    }
    when u run it just hit any key on the keyboard.. it will print the ascii number. the function keys like F1 and so forth sends two asccii numbers but they all start with 0.
    note: the program will loop forever until you terminate it.. just close the console window or tyr pressing ctrl + c.

  3. #13
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Re: C Problems

    Quote Originally Posted by Skillachi
    And my final question, when i use the ascii code does case still matter... as in lets say the ascii code for the letter "g" is 20... if the person say enters G would the ascii be 21? or would it still be read as 20?
    yea.. upper case letters have different ascii numbers from their lowersace equivalent. you could look at the link tjrak posted or you could hold down shift key will you hit the keys with the code i posted

  4. #14
    Join Date
    Nov 2002
    Posts
    2,231
    Rep Power
    0

    Default Re: C Problems

    Man... that code there stink... i will now begin to pwn all in C
    Laptop: HP DV6700t - Core 2 Duo T9300 2.5Ghz, 3GB RAM, Nvidia 8400m GS, 250GB HDD. Ubuntu 12.04 and Windows 7
    Phone: Samsung Galaxy Nexus

  5. #15
    Join Date
    Aug 2004
    Posts
    2,789
    Rep Power
    0

    Default Re: C Problems

    Quote Originally Posted by leoandru
    if u using borland (which u are) you can change the console background using textbackground function in the conio.h library textcolor to change the text color. for example to change the background to blue and textcolor to yellow you could do something like this:

    textbackground(BLUE);
    textcolor(YELLOW);

    note: if you change the text color using the textcolor function you will have to use cprintf function instead of printf function to get the text printed in color.
    What do u use to change the colour of the font in C++?
    The views expressed in the above post are not neccesarily the views of icuucme.

  6. #16
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Re: C Problems

    Quote Originally Posted by icuucme
    What do u use to change the colour of the font in C++?
    uhmm, what you must know is that these functions used in both c and C++ are not part of ANSI/ISO standard. must of these are funtions are supplied with the compiler. The function i posted (about changing the textcolor) before only works with the borland compiler and not on the windows c compiler. The code im about to post definately works on the windows compiler because I tested it b4 i posted but im not sure if it will work on the borland compiler. If the borlands windows.h library is compatible with the windows version then it should work. here goes.

    Code:
    #include <windows.h>
    #include <iostream>
    
    using std::cout;
    
    
        class Color
        {
            friend std::ostream& operator<<(std::ostream& stream,const Color& c);
    
    	public:        
    	Color(unsigned short c): color(c) {}
    	unsigned short color;
        };
    
        std::ostream& operator<<(std::ostream& stream,const Color& c)
        {
            stream.flush();
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c.color);
            stream.flush();
            return stream;
        }	
        
        const Color RED     = FOREGROUND_RED|FOREGROUND_INTENSITY;
        const Color BLUE    = FOREGROUND_BLUE|FOREGROUND_INTENSITY;  
    
        int main() {
    	cout << RED << "This is red." << BLUE << "\nThis is blue.\n";
    	getchar();
    	return 0;
        }
    this is a fairly simply program. I used a Color class to store the color, as you can see the colors are made up of a mixture the colors: FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_BLUE and FOREGROUND_INTENSITY. The
    << stream operator was overloaded to set the console text color before we do any printing. Basically that set of function calls will have to be called everytime you need to change the color.

    Let me know if it works in borland if not tell me the errors you got.

    Oh i guess you figured that the.
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),c.color); is all you need to change the textcolor

    update:
    The STD_OUTPUT_HANDLE has no spaces i tried editing the post several times but the spaces remained.
    Last edited by leoandru; Mar 25, 2005 at 01:26 AM.

  7. #17
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default Re: C Problems

    It works for me
    MS VC 6++
    WinXP

  8. #18
    Join Date
    Aug 2004
    Posts
    2,789
    Rep Power
    0

    Default Re: C Problems

    Quote Originally Posted by leoandru
    Code:
    #include <windows.h>
    #include <iostream>
    
    using std::cout;
    
    
        class Color
        {
            friend std::ostream& operator<<(std::ostream& stream,const Color& c);
    
    	public:        
    	Color(unsigned short c): color(c) {}
    	unsigned short color;
        };
    
        std::ostream& operator<<(std::ostream& stream,const Color& c)
        {
            stream.flush();
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c.color);
            stream.flush();
            return stream;
        }	
        
        const Color RED     = FOREGROUND_RED|FOREGROUND_INTENSITY;
        const Color BLUE    = FOREGROUND_BLUE|FOREGROUND_INTENSITY;  
    
        int main() {
    	cout << RED << "This is red." << BLUE << "\nThis is blue.\n";
    	getchar();
    	return 0;
        }
    The code does not work in Borland for me
    I'm using Borland c++ Version 5.02
    The views expressed in the above post are not neccesarily the views of icuucme.

  9. #19
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default Re: C Problems

    could you post a copy of the compile error?

  10. #20
    Join Date
    Aug 2004
    Posts
    2,789
    Rep Power
    0

    Default Re: C Problems

    First I got these errors:
    Info :Compiling C:\DOCUMENTS AND SETTINGS\myusername\...\BIN\noname01.cpp
    Error: noname01.cpp(4,5):Qualifier 'std' is not a class or namespace name
    Error: noname01.cpp(4,7):Identifier expected
    Error: noname01.cpp(9,19):Qualifier 'std' is not a class or namespace name
    Error: noname01.cpp(9,21): Declaration terminated incorrectly
    Error: noname01.cpp(16,8):Qualifier 'std' is not a class or namespace name
    Error: noname01.cpp(16,10): Declaration terminated incorrectly
    Error: noname01.cpp(28,13):Undefined symbol 'RED'
    Error: noname01.cpp(29,10):Call to undefined function 'getchar'

    After i modified the code to this:
    Code:
    #include <windows.h>
    #include <iostream.h>
    
    
    
    
        class Color
        {
            friend ostream& operator<<(ostream& stream,const Color& c);
    
    	public:
    	Color(unsigned short c): color(c) {}
    	unsigned short color;
        };
    
        ostream& operator<<(ostream& stream,const Color& c)
        {
            stream.flush();
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c.color);
            stream.flush();
            return stream;
        }
    
        const Color RED     = FOREGROUND_RED|FOREGROUND_INTENSITY;
        const Color BLUE    = FOREGROUND_BLUE|FOREGROUND_INTENSITY;
    
        int main() {
    	cout << RED << "This is red." << BLUE << "\nThis is blue.\n";
    	getchar();
    	return 0;
        }
    I got the error:

    Info :Compiling C:\DOCUMENTS AND SETTINGS\myusername\...\BIN\noname01.cpp
    Error: noname01.cpp(29,10):Call to undefined function 'getchar'

    NB: I'm new to c++
    Last edited by icuucme; Mar 27, 2005 at 11:15 AM.
    The views expressed in the above post are not neccesarily the views of icuucme.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •