Thread: Background Help

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    10

    Background Help

    I would like to add a background colour on my programme

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    int menu();
    int distance();
    int sw;
    int main()
    {
    menu();
    float value;
    value=distance();
    printf("%f", value);
    return 0;
    }
    int menu()
    {
    system("cls");
    printf("                            Please choose a category below\n");
    printf("                            1.Weight\n");
    printf("                            2.Measurement\n");
    printf("                            3.Distance\n");
    printf("                            4.Temperature \n ");
    sw=getchar();
    sw=sw-'0';
    switch(sw)
    {
    case 1:
    weight();
    break;
    case 2:
    measurement();
    break;
    case 3:
    distance();
    break;
    case 4:
    temperature();
    break;
    case 5:
    main();
    break;
    default:
    menu();
    break;
    }
    }
    int weight()
    {
    system("cls");
    printf("                            Weight\n");
    printf("                            1.Ounce to Pounds\n");
    printf("                            2.Pounds to Ounce\n");
    printf("                            3.Pounds to Kilograms\n");
    printf("                            4.Kilograms to Pounds\n");
    printf("                            5.Back to the Main menu\n");
    sw=getchar();
    sw=sw-'0';
    switch(sw)
    {
    case 1:
    OtoP();
    break;
    case 2:
    PtoO();
    break;
    case 3:
    PtoK();
    break;
    case 4:
    KtoP();
    break;
    case 5:
    main();
    break;
    default:
    weight();
    break;
    }
    }
    int OtoP()
    {
    float oz,lb;
    printf("\n\n");
    printf("Enter Ounce: ");
    scanf("%f",&oz);
    lb=oz/16;
    printf("\n\n");
    printf("%f ounce = %f pounds",oz,lb);
    getch();
    return weight();
    }
    int PtoO()
    {
    float oz,lb;
    printf("\n\n");
    printf("Enter Pounds: ");
    scanf("%f",&oz);
    lb=oz*16;
    printf("\n\n");
    printf("%f Pounds = %f Ounce",oz,lb);
    getch();
    return weight();
    }
    int PtoK()
    {
    float kg,lb;
    printf("\n\n");
    printf("Enter Pounds: ");
    scanf("%f",&kg);
    lb=kg*0.453592;
    printf("\n\n");
    printf("%f Pounds = %f Kilograms",kg,lb);
    getch();
    return weight();
    }
    int KtoP()
    {
    float kg,lb;
    printf("\n\n");
    printf("Enter Kilograms: ");
    scanf("%f",&kg);
    lb=kg/0.453592;
    printf("\n\n");
    printf("%f Kilograms = %f Pounds",kg,lb);
    getch();
    return weight();
    }
    int measurement()
    {
    system("cls");
    printf("                                Measurement\n");
    printf("                                1.Centimetres to Inches\n");
    printf("                                2.Inches to Centimetres\n");
    printf("                                3.Inches to Feet\n");
    printf("                                4.Feet to Inches\n");
    printf("                                5.Back to the Main menu\n");
    sw=getchar();
    sw=sw-'0';
    switch(sw)
    {
    case 1:
    CtoI();
    break;
    case 2:
    ItoC();
    break;
    case 3:
    ItoF();
    break;
    case 4:
    FtoI();
    break;
    case 5:
    main();
    break;
    default:
    measurement();
    break;
    }
    }
    int CtoI()
    {
    float in,cm;
    printf("\n\n");
    printf("Enter Centimetres: ");
    scanf("%f",&cm);
    in=cm*0.3937;
    printf("\n\n");
    printf("%f Centimetres = %f Inches",cm,in);
    getch();
    return measurement();
    }
    int ItoC()
    {
    float in,cm;
    printf("\n\n");
    printf("Enter Inches: ");
    scanf("%f",&cm);
    in=cm/0.3937;
    printf("\n\n");
    printf("%f Inches = %f Centimetres",cm,in);
    getch();
    return measurement();
    }
    int ItoF()
    {
    float in,ft;
    printf("\n\n");
    printf("Enter Inches: ");
    scanf("%f",&ft);
    in=ft*0.083;
    printf("\n\n");
    printf("%f Inches = %f Foot",ft,in);
    getch();
    return measurement();
    }
    int FtoI()
    {
    float in,ft;
    printf("\n\n");
    printf("Enter Foot: ");
    scanf("%f",&ft);
    in=ft/0.083;
    printf("\n\n");
    printf("%f Foot = %f Inches",ft,in);
    getch();
    return measurement();
    }
    int distance()
    {
    system("cls");
    printf("                            Distance\n");
    printf("                            1.Miles to kilometres\n");
    printf("                            2.Kilomeres to miles\n");
    printf("                            3.Back to the Main menu\n");
    sw=getch();
    sw=sw-'0';
    switch(sw)
    {
    case 1:
    MtoK();
    break;
    case 2:
    KtoM();
    break;
    case 3:
    main();
    break;
    default:
    distance();
    break;
    }
    }
    int MtoK()
    {
    float k,m;
    printf("\n\n");
    printf("Enter miles: ");
    scanf("%f",&m);
    k=m*1.609344;
    printf("\n\n");
    printf("%f miles = %f kilometers",m,k);
    getch();
    return distance();
    }
    int KtoM()
    {
    float k,m;
    printf("\n\n");
    printf("Enter kilometres: ");
    scanf("%f",&m);
    k=m/1.609344;
    printf("\n\n");
    printf("%f kilometres = %f miles",m,k);
    getch();
    return distance();
    }
    int temperature()
    {
    system("cls");
    printf("                               Temperature\n");
    printf("                               1.Fahrenheiht to Centigrade\n");
    printf("                               2.Centigrade to Fahrenheiht\n");
    printf("                               3.Kelvin to Fahrenheit\n");
    printf("                               4.Kelvin to Centigrade\n");
    printf("                               5.Back to Main Menu\n");
    sw=getch();
    sw=sw-'0';
    switch(sw)
    {
    case 1:
    FtoC();
    break;
    case 2:
    CtoF();
    break; 
    case 3:
    KtoF();
    break;
    case 4:
    KtoC();
    break;
    case 5:
    main();
    break;
    default:
    temperature();
    break;
    }
    }
    int FtoC()
    {
    float c,f;
    printf("\n\n");
    printf("Enter Fahrenheiht: ");
    scanf("%f",&f);
    c=(f-32)*5/9;
    printf("\n\n");
    printf("%f Fahrenheiht = %f Centigrade",f,c);
    getch();
    return temperature();
    }
    int CtoF()
    {
    float f,c;
    printf("\n\n");
    printf("Enter Centigrade: ");
    scanf("%f",&c);
    f=c*33.8;
    printf("\n\n");
    printf("%f Centigrade = %f Fahrenheiht",c,f);
    getch();
    return temperature();
    }
    int KtoF()
    {
    float f,k;
    printf("\n\n");
    printf("Enter Kelvin: ");
    scanf("%f",&k);
    f=((k-273.15)*1.8)+32;
    printf("\n\n");
    printf("%f Kelvin = %f Fahrenheit",k,f);
    getch();
    return temperature();
    }
    int KtoC()
    {
    float c,k;
    printf("\n\n");
    printf("Enter Kelvin: ");
    scanf("%f",&k);
    c=k-272.15;
    printf("\n\n");
    printf("%f Kelvin = %f Centigrade",k,c);
    getch();
    return temperature();
    }
    I want to have a blue background with white text
    I know it proberly simple to do if some one can help me it would be a huge help

  2. #2
    Registered User
    Join Date
    Nov 2012
    Location
    Some rock floating in space...
    Posts
    32

    Arrow

    You could try the ncurses library. It's been ported to windows. I don't program under windows and haven't for quite some time really. If you're using Linux or another flavor of Unix with ANSI terminal emulation, then you should check out my post under "Dividing the Terminal" in the Linux section of the forums. I posted some simple ANSI macros which should work on any platform with ANSI terminal emulation. Windows does not use ANSI terminal emulation though, so they won't work under windows

    ncurses is probably your best bet if you want to write portable code though that will run just as happily on Windows or Linux or whatever since it's been ported to every platform with a console/terminal environment

    However, there are a set of console functions in the Platform SDK (or whatever they're calling it these days ) that handle things like setting colors on the screen. Below is a site that discusses changing foreground and background colors using the Platform SDK

    Setting Windows Console Text Colors in C - Jim Lawless' Blog

    Hope this helps! And no! To answer your question! There really is no "standard way" of setting text attributes on a given console/terminal... There are ANSI standards, Microsoft standards and common libraries found on various Operating Systems etc. But no single standard way...


    **EDIT**
    Accidentally posted a C# link before discussing the Microsoft functions you're looking for.. Replaced it with a C link
    Last edited by twiki; 11-17-2012 at 06:35 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    SourceForge.net: Indentation - cpwiki
    Before you do anything, you need to make your code readable.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bmp background in C++
    By Ervilha in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2011, 08:22 PM
  2. Background Image
    By NoFearXD in forum Windows Programming
    Replies: 4
    Last Post: 05-16-2007, 05:25 PM
  3. Background
    By hdragon in forum Game Programming
    Replies: 11
    Last Post: 04-01-2006, 12:14 AM
  4. Run in background
    By smog890 in forum C Programming
    Replies: 4
    Last Post: 06-09-2002, 02:22 PM
  5. Background
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2002, 09:43 PM