Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Happy Numbers

Name: Tynach 2009-09-03 19:34

Hey, I just found this board on 4chan (didn't know about it before), and I wanted to know what you guys thought of my program that checks to see if a number is happy. It was originally written in Python, but as a means to learn C, I re-wrote it in C a while back. Recently, I decided I wanted to find out if I really understood pointers as well as I thought, so I re-wrote the C version from scratch, though implemented some of the same code (though heavily modified to work with my new format) to use pointers so it uses less RAM.

Here's the source:

#include <stdio.h>

void check(char *);
char checkh(long long int, long long int *);
void usragain(char *, char *);
void getnum(long long int *);
void flushin();

int main()
{
    char run = 1;
    char state = 0;
    printf("Happy Number Checker v 2.0\n\n");
    while(run == 1) {
        switch (state) {
        case 0:
            check(&state);
            break;
        case 1:
            usragain(&run, &state);
            break;
        }
    }
    return 0;
}

void check(char *ret)
{
    long long int orig;
    getnum(&orig);
    *ret = checkh(orig, &orig);
    if (*ret == 0) {
        printf("%lld is not happy. Try again.\n\n", orig);
    } else if (*ret == 1) {
        printf("%lld is happy!\n\n", orig);
    }
}

char checkh(long long int guess, long long int *orig)
{
    long long int tmp = guess;
    long long int sum;
    char digit;
    char loop = 1;
   
    while (loop == 1) {
        sum = 0;
        while (tmp != 0) {
            digit = tmp % 10;
            sum += digit * digit;
            tmp /= 10;
        }
        if (sum == 1) {
            loop = 0;
            return 1;http://www.qso.com/carlautta/colin/Beta/happy.c
        } else if (sum == 42 || sum == *orig) {
            loop = 0;
            return 0;
        } else {
            loop = 1;
            tmp = sum;
        }
    }
}

void usragain(char *yn, char *state)
{
    *yn = 2;
    printf("Would you like to check another number (y/n)? ");
    while (*yn == 2) {
        flushin();
        scanf("%c", yn);
        printf("\n");
        if (*yn == 'y') {
            *yn = 1;
            *state = 0;
        } else if (*yn == 'n') {
            *yn = 0;
        } else {
            printf("Invalid input, type y or n.\n");
            *yn = 2;
        }
    }
}

void getnum(long long int *guess)
{
    int test = 0;
    while (test == 0) {
        printf("Please insert a number: ");
        test = scanf("%lld", guess);
        if (test == 0) {
            flushin();
            printf("\n\nInvalid input. Try again.\n\n");
        }
    }
}

void flushin()
{
    int ch = 0;
    while ((ch = getc(stdin)) != EOF && ch != '\n') {
        continue;
    }
}

I'm a newb to text boards here on 4chan, so I may be missing a way to insert code properly (in some forums it's and). Please tell me if I fail at this and should become an hero.

Name: UMH memesmith !gNlkr4vCuc 2009-09-05 19:35

OpenCOBOL passes many of the tests included in the NIST sponsored COBOL 85 test suite. While it passes over 9000 of the tests, OpenCOBOL does not claim conformance to any level of COBOL Standard.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List