Thread: Gas Gadge

  1. #1
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12

    Gas Gadge

    Hey everyone, i'm creating a program that requires the gas gadge to be updated everytime the speed changes. At the moment i'm manually entering the numbers, but later I will have the numbers being entered via txt file. Anyway, i'm having troubles getting the gas gadge to work, here is the code so far:


    Code:
    /*Variables*/
    //  FILE *numList;      //list of integers, manually enter numbers for now
      int i;                        //for loop variable;
      int temp;                //temp number
      float num;               //speed in %
      
    //  numList = fopen("c:\\numList.txt","r");
    
      /*do loop*/
     // do{
      
        /*Prompt to enter number*/
        printf("Please enter the integer: ");
        scanf("%d", &temp);
        
        num = temp /64 * 100;
        printf("%.2f\n\n", num);
        
        num = 50;
        printf("\t|");
        for(i=0;i>=num;i++)
        {
          printf("-");
        }
        printf("|");
      
    
     // }while(num != -1);    /*exit do on '-1'*/
      
        getch();
    The main problem is that the inputed number isn't being converted into a percentage, and I have no idea why. I know this might seem like a n00b question, but I can't see the problem.

    I want the gadge to loop something like the:
    |------------------------ |


    or something like that, the dashes will represent the speed. Also, If anyone has any other suggestions about how the gadge show look, I'm all ears.

    Thanks;
    S0n1C!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://dictionary.reference.com/browse/gauge

    Quote Originally Posted by S0n1C
    The main problem is that the inputed number isn't being converted into a percentage
    If you want to do floating point math, don't do integer math.
    Code:
    num = temp /64 * 100;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Yes, to get floating point number, put the temp variable into float data type.

    And then, I think you should change the second expression within the for loop. I think it should be i <= 100, not i >= 100. Isn't it?

    Then, one thing... why do you put 'num = 50;' over there?? I don't get it...

    Anyway, keep trying!

  4. #4
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12
    Hey, i have 'num = 50' just for testing, because i couldn't get the percentage part to work. I just wanted to see if the gadge would work in the first place, ingnore that line of code, sorry forgot to document it out.

    S0n1C!

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This is going a little further than I should, but there's really not that much here besides what I've highlighted.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    /**
     * Display a progress bar.
     * @param  value   the current value
     * @param  maxval  the maximum value
     */
    void gauge(int value, int maxval)
    {
       static const int scale = 50; /* adjust per your preference */
       double progress = (double)value / maxval;
       int i, current = progress * scale;
       /*
        * Print bar |-----+-------|
        */
       putchar('|');
       for ( i = 1; i < current; ++i )
       {
          putchar('-');
       }
       putchar('+');
       for ( ; i < scale - 1; ++i )
       {
          putchar('-');
       }
       putchar('|');
       /*
        * Print percentage.
        */
       printf(" %g%%", progress * 100.0);
    }
    
    int main()
    {
       int i, top = 25;
       for ( i = 0; i < top; ++i )
       {
          time_t now = time(NULL);
          putchar('\r'); /* may work, may not */
          gauge(i, top);
          while ( time(NULL) == now ); /* ugly delay */
       }
       return 0;
    }
    And 0% and 100% don't work the way you might expect. (I gotta leave something.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12
    Works great, thanks. I like the gauge, i'm now in the process of changing it. Making it look a little more graphical.

    Thanks Again;
    S0n1C!
    Last edited by S0n1C; 10-25-2006 at 08:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extern variables in GAS?
    By Kernel Sanders in forum C Programming
    Replies: 6
    Last Post: 10-20-2008, 10:10 PM
  2. Gas books
    By lruc in forum Tech Board
    Replies: 5
    Last Post: 10-01-2008, 12:06 AM
  3. Don't Pump Gas on May 15th
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 05-04-2007, 04:28 PM
  4. Gas dropping?
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 06-10-2004, 06:25 AM