Need Help on program guys

This is a discussion on Need Help on program guys within the C Programming forums, part of the General Programming Boards category; Hey guys, Having some trouble with my program, basically wanting it to be a "digital units converter" so that you ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    1

    Need Help on program guys

    Hey guys,

    Having some trouble with my program, basically wanting it to be a "digital units converter" so that you input say 1024 kilobytes and it outputs 1 megabyte.


    The program seems to be broken in that the dummy variable and the input string dont seem to work inthe loops below, am i doing something wrong?

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
        double output;
        double dummy1;
        char bits[19], bytes[19];
        int i, input;
    puts("You may choose from:\nBits     \tBytes    \tKilobytes\nMegabytes\tGigabytes\tTerabytes\nPetabytes\tExabytes\tZettabytes\nYottabytes\n\n");
    puts("What  do you have?\n");
    scanf("%d %s", &input, bits);
    puts("What is the unit you wish to convert to?\n");
    scanf("%s", bytes);
    
      for(i = 0; bits[ i ]; i++)             //lower cases the strings
        bits[i] = tolower(bits[ i ]);
      for(i = 0; bytes[ i ]; i++)
        bytes[i] = tolower(bytes[ i ]);
    printf("\n\n\n\n\n%s   ::: %d\t%s\n", bits, input, output);
    snooze(bits, input, dummy1);
    snoozes(bytes, dummy1, output);
    
    
    printf("\n\nYou have %d %s\n", output, bytes);
    
    
    
    return 0;
    }
    
    
    int snooze(xx, zz, oo)
    {
    
        if(strchr(xx, 'bits')) {oo=zz;}
        if(strchr(xx, 'bytes')){oo=zz*8;}
        if(strchr(xx, 'kilobytes')) {oo=zz*8*1024;}
        if(strchr(xx, 'megabytes')) {oo=zz*8*1024*1024;}
        if(strchr(xx, 'gigabytes')) {oo=zz*8*1024*1024*1024;}
        if(strchr(xx, 'terabytes')) {oo=zz*8*1024*1024*1024*1024;}
        if(strchr(xx, 'petabytes')) {oo=zz*8*1024*1024*1024*1024*1024;}
        if(strchr(xx, 'exabytes')) {oo=zz*1024*1024*1024*1024*1024*1024;}
        if(strchr(xx, 'zettabytes')) {oo=zz*8*1024*1024*1024*1024*1024*1024*1024;}
        if(strchr(xx, 'yottabytes')) {oo=zz*8*1024*1024*1024*1024*1024*1024*1024*1024;}
        printf("\n\n%s\t%d\t%d\n", xx, oo, zz);
    
    }
    
    int snoozes(xx, zz, oo)
    {
    
    
             if(strchr(xx, 'bits')) {oo=zz;}
        if(strchr(xx, 'bytes')){oo=zz/8;}
        if(strchr(xx, 'kilobytes')) {oo=zz/8/1024;}
        if(strchr(xx, 'megabytes')) {oo=zz/8/1024/1024;}
        if(strchr(xx, 'gigabytes')) {oo=zz/8/1024/1024/1024;}
        if(strchr(xx, 'terabytes')) {oo=zz/8/1024/1024/1024/1024;}
        if(strchr(xx, 'petabytes')) {oo=zz/8/1024/1024/1024/1024/1024;}
        if(strchr(xx, 'exabytes')) {oo=zz/8/1024/1024/1024/1024/1024/1024;}
        if(strchr(xx, 'zettabytes')) {oo=zz/8/1024/1024/1024/1024/1024/1024/1024;}
        if(strchr(xx, 'yottabytes')) {oo=zz/8/1024/1024/1024/1024/1024/1024/1024/1024;}
        printf("\n\n%s\t%d\t%d\n", xx, oo, zz);
    
    
    }

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You do know that x / y1 / y2 / y3 ... / yn is the same as x / (y^n)?

    You should also read the documentation for strchr().
    Last edited by msh; 09-08-2010 at 04:27 AM.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    “dont seem to work” is not a good description of a problem. You should explain what you expect to happen followed by what actually happens; include all the information you can think of that might be useful to somebody who is taking the time to look at your code. For example, your compiler has to be issuing diagnostics because of this code. You should mention that and show what the diagnostics are.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It might also be a good idea to use descriptive variable and function names...

    "CountWords" tells us a lot more than "Snorg."

    Comments are a good idea too...

    It may not mean much to you right now but if you do get busy writing code (in any language) you will likely need to come back to it some time after writing it... and you will need to be able to read it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to execute other program within c program?
    By KoYoungSuk in forum C Programming
    Replies: 7
    Last Post: 06-07-2010, 05:08 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21