Home > front end >  can't figure out how to do Conversions
can't figure out how to do Conversions

Time:05-27

This is my first post so please be kind. This code is a countdown with seconds minutes, and hours and it works fine if the number stated is between 0 and 60 but if it is over 60 it breaks and I can't figure it out. How to do it? I tried doing it in Case 1 under the first loop but it just breaks my code. Any help would be appreciated

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

/* run this program using the console pauser or add your own getch,
 * system("pause") or input loop */
void clock(int n) {
    int i;
    int b;
    int c;
    int C;
    int x;
    int s;
    printf("which countdown do you want\n");
    printf("1.seconds\n");
    printf("2.Minutes\n");
    printf("3.Hours\n");
    scanf("%d", &C);

    b = 0;

    switch (C) {
        case 1:
            scanf("%d", &n);

            for (i = n; i >= 0; i--) {
                if (n > 60) {
                    s = n / 60;
                    s   b;
                }

                printf("d", c);
                printf(": d", b);
                printf(" : d\r", i);
                Sleep(1000);

                break;
            }

        case 2:
            scanf("%d", &n);
            n = n - 1;
            for (b = n; b >= 0; b--) {
                for (i = 59; i >= 0; i--) {
                    printf("d", c);
                    printf(": d", b);
                    printf(" : d\r", i);
                    Sleep(1000);
                }
            }
            break;

        case 3:
            scanf("%d", &n);
            n = n - 1;
            for (c = n; c >= 0; b--) {
                for (b = 59; b >= 0; b--) {
                    for (i = 59; i >= 0; i--) {
                        printf("d", c);
                        printf(": d", b);
                        printf(" : d\r", i);
                        Sleep(1000);
                    }
                }
            }
    }
}

int main(int argc, char *argv[]) {
    int n;
    clock(n);
    return 0;
}

CodePudding user response:

there is a lot wrong here

        for (i = n; i >= 0; i--) {
            if (n > 60) { <<<=== n never changes so why is this in the loop
                s = n / 60; <<< you never do anything with s
                s   b; <<< this does nothing at all
            }

            printf("d", c); <<<<=== c is never given a value
            printf(": d", b); <<< nor is b
            printf(" : d\r", i);
            Sleep(1000);

            break; <<<< == you only loop once
        }

you probably mean

        for (i = n; i >= 0; i--) {
             int hrs = i / 3600;
             int rem = i % 3600;
             int mins = rem /60;
             int secs = rem `;

            printf("d", hrs);
            printf(": d", mins);
            printf(" : d\r", secs);
            Sleep(1000);

        
        }

slightly wasteful in that it recalculates everything every second, but is at least simple

CodePudding user response:

There are multiple problems:

  • you do not initialize variable b in cases 1 and 2
  • you have a break in the body for case 1, causing the count down to stop after 1 second.
  • you pass n uninitialized to clock() which ignores its argument anyway.
  • clock() is a standard library function.
  • since output is buffered by default, you may need to manually flush the output with fflush(stdout); to ensure proper display very second.
  • the loops and conversions are too complicated. You should just compute the number seconds, iterate on that and display the hours, minutes and seconds left with divisions and modulo operations.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

void countdown(void) {
    int C = 1;
    int n = 0;

    printf("which countdown do you want?\n");
    printf("1: seconds\n");
    printf("2: Minutes\n");
    printf("3: Hours\n");

    scanf("%d", &C);

    switch (C) {
      case 1:  // seconds
        scanf("%d", &n);
        break;
      case 2:  // minutes
        scanf("%d", &n);
        n = n * 60;
        break;
      case 3:  // hours
        scanf("%d", &n);
        n = n * 3600;
        break;
      default:
        return;
    }
    for (;;) {
        // display the countdown:  hours  :  minutes  :   seconds
        printf("\rd:d:d", n / 3600, n / 60 % 60, n % 60);
        fflush(stdout);
        if (n-- <= 0)
            break;
        Sleep(1000);
    }
    printf("LIFT OFF!\n");  // https://www.fai.org/news/3-2-1-0…-lift-launch-apollo-11
}

int main() {
    countdown();
    return 0;
}
  •  Tags:  
  • c
  • Related