Thread: conio.h

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    1

    conio.h

    I'm teaching C programming for the first time and the code is not working. In conio.h there is a function TEXTCOLOR which changes textcolor of output. But there are errors. Could you tell me where is the mistake.

    insert
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    textcolor(RED);
    printf("Programming in C");
    
    return 0;
    }
    There are errors on the online C compiler(www.onlinegdb.com). Why? Could you tell me because I have to teach it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,749
    > I'm teaching C programming for the first time and the code is not working.
    Well the big problem is that your idea of C programming is 30 years out of date.

    You need to learn how to write programs which don't involve using conio.h.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    443
    Quote Originally Posted by Salem View Post
    > I'm teaching C programming for the first time and the code is not working.
    Well the big problem is that your idea of C programming is 30 years out of date.

    You need to learn how to write programs which don't involve using conio.h.
    hhhhh , by the way, isn't using library conio.h for defining is old? recently we aren't using that library at all

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    567
    Conio.h is a non standard header file providing functions that are for Windows compilers only and should never be used even for Windows programming. Stick to C99 and C11 C Standard function and features only!

    You could use the ncurses library if you need that functionality on Windows or Linux.

  5. #5
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    71
    Quote Originally Posted by silvery View Post
    I'm teaching C programming for the first time and the code is not working. In conio.h there is a function TEXTCOLOR which changes textcolor of output. But there are errors. Could you tell me where is the mistake.

    There are errors on the online C compiler(www.onlinegdb.com). Why? Could you tell me because I have to teach it.
    @silvery - take the "MingW" for Windows (it's the gcc for Win, a free compiler).
    MinGW-w64 - for 32 and 64 bit Windows - Browse /Toolchains targetting Win32/Personal Builds/mingw-builds at SourceForge.net


    If you want a little bit color look the program code below, but it is for Windows only - for Linux is there something similar . . .

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    
    int main(void)
    {
        int fgcolor;
      float netto, brutto;
    
    
        printf("\nNettopreis = ");
        scanf("%6f", &netto);
        printf("\nMehrwertsteuer: + 19% MWst");
        
        //New color - lightgreen
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fgcolor = 10);
        printf("\n\nBruttopreis: %6.2f Euro\n", (brutto = (netto * 1.19)));
        
        //Set return the color to standard - lightgrey
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fgcolor = 7);
        
        return(0);
    Example:
    conio.h-color_2018-12-07_221239-jpg

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    541
    I get different results, depending on whether I use x86_64-pc-cygwin-gcc or i686-w64-mingw32-gcc. With the x86_64 version, the "Mehrwertsteuer" line is also colored green. This is probably due to a slight difference in stdout buffering between the stdio library used by each version. That can be fixed by putting fflush(stdout); before each call to SetConsoleTextAttribute. Or end each line with '\n' rather than printing it at the beginning of each line. It's more standard practice to end a line with '\n' (and stdio buffers a line until '\n').

    Also, your printf format is wrong. There is no "%M" format specifier; you need to use two percent signs to output a percent sign.

  7. #7
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    71
    Quote Originally Posted by christop View Post
    I . . . That can be fixed by putting fflush(stdout);
    . . .
    Also, your printf format is wrong. There is no "%M" format specifier; you need to use two percent signs to output a percent sign.
    First: That's absolute bad, bad, bad, bad, . . .


    And "%", ok, a better question is: Why shows "printf("\nMehrwertsteuer: + 19%% MWst");" the same output?

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    541
    Quote Originally Posted by Kernelpanic View Post
    And "%", ok, a better question is: Why shows "printf("\nMehrwertsteuer: + 19%% MWst");" the same output?
    Because printf is allowed to print the percent sign when it sees an unknown specifier. But (I haven't looked at the C standards) it might be implementation-defined, meaning you shouldn't rely on it.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,578
    Quote Originally Posted by Kernelpanic View Post
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    
    int main(void)
    {
        int fgcolor;
      float netto, brutto;
    
    
        printf("\nNettopreis = ");
        scanf("%6f", &netto);
        printf("\nMehrwertsteuer: + 19% MWst");
        
        //New color - lightgreen
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fgcolor = 10);
        printf("\n\nBruttopreis: %6.2f Euro\n", (brutto = (netto * 1.19)));
        
        //Set return the color to standard - lightgrey
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), fgcolor = 7);
        
        return(0);
    Why do you do this?...
    Devoted my life to programming...

  10. #10
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    71
    Quote Originally Posted by GReaper View Post
    Why do you do this?...
    Strange question. Why not? I do not like dogmatism, and only who are curious learn something new.


    Over 20 years ago, I completed a course in economics with programming in Cobol; Micro Focus Cobol Prof 3.0 / 3.1.
    One day I asked the teacher can one recursively programming in Cobol and he answered no, that will not work in Cobol.


    After school, I dealt with this question in the evening, everyone in the class had a copy of Cobol. And I actually succeeded in imitating a recursive function in Cobol.
    Of course, nobody would use it in practice, because it is too inconvient, but that's not what it was about, but to show that it works recursively in Cobol too. I showed it because I was curious. Curiouser than the teacher.
    The next day I showed him the solution . . . he was not amused.


    Is the question answered? Only those who are curious learn, all others stay where they stand.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,578
    So you're doing it because you can... fair enough. Do you do other similarly useless things as well? I'm not judging, just curious.
    Devoted my life to programming...

  12. #12
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    567
    Quote Originally Posted by christop View Post
    Because printf is allowed to print the percent sign when it sees an unknown specifier. But (I haven't looked at the C standards) it might be implementation-defined, meaning you shouldn't rely on it.
    Nothing to do with an "unknown specifier".

    %% is well defined in the C99 Standard in section 7.19.6.1, on page 280. (This specifies the format conversions for both printf() and fprintf())
    % A % character is written. No argument is converted. The complete
    conversion specification shall be %%.
    and
    If a conversion specification is invalid, the behavior is undefined.
    "But (I haven't looked at the C standards) it might be implementation-defined,"

    In this case it is not, but other conversion specifiers, that are not defined by the Standard, if any, may be implementation defined,

    You can, and should rely on the "%%" conversion specifier to print a percent sign.

  13. #13
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    71
    Quote Originally Posted by GReaper View Post
    So you're doing it because you can... fair enough. Do you do other similarly useless things as well? I'm not judging, just curious.
    First, why is that an useless thing (color on the console)? It is nice, and I have learn how I can do it.

    Second, of course, programming in C again. It's for me variety, distraction, fun, and it clears my mind - it reset my brain. Is that not also an useless thing?

    Third, of course. If I on vacation there is no castle, no tower on the ride I do not climb up on. I am an absolute castle-fan and so on. Or if I do a bycicle tour - Are that not also useless things only?

    Fourth . . . and so on. The life is full of "useless things"! But without these useless things how would be the life? How is Your life?

    This is a look from the castle "Muehlburg" to the castle "Burg Gleichen", Thuringia
    Google Maps

    Is someone planning a holiday there? Ask me for "useless" infos.
    (Sorry for this OT.)

    conio.h-burggleichen-jpg

  14. #14
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    541
    Quote Originally Posted by rstanley View Post
    Nothing to do with an "unknown specifier".

    %% is well defined in the C99 Standard in section 7.19.6.1, on page 280. (This specifies the format conversions for both printf() and fprintf())

    "But (I haven't looked at the C standards) it might be implementation-defined,"

    In this case it is not, but other conversion specifiers, that are not defined by the Standard, if any, may be implementation defined,

    You can, and should rely on the "%%" conversion specifier to print a percent sign.
    Yes, you're right. I misread the question. I thought he was asking about the original format string with a single percent sign (I didn't look at the string closely enough). Two percent signs is, of course, defined to print a percent sign on output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conio.h and dev-c++.. How?
    By wasos in forum C Programming
    Replies: 6
    Last Post: 01-23-2007, 05:57 PM
  2. Using conio.h in C++
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 12-02-2005, 02:22 PM
  3. Conio.h
    By ZakkWylde969 in forum Linux Programming
    Replies: 4
    Last Post: 05-12-2004, 03:41 PM
  4. DevC++ accepts neither "conio.h" nor "conio.c"
    By Nutshell in forum C Programming
    Replies: 9
    Last Post: 01-18-2003, 04:35 AM
  5. I want my conio.h!
    By Hannwaas in forum Linux Programming
    Replies: 3
    Last Post: 05-24-2002, 03:05 PM

Tags for this Thread