. /../Need help with a weird bug in.../ 1
????
written by Malkom on Apr 30, 2011 15:36
I have a weird bug in my C++ program, and I need help with it. I'm not sure if this is the right place to post this, so if a mod wants to move this please feel free.

Here's the program:
// A utility to clean errant characters from ASCII text files. Originally intended to be used for
// cleaning up transcripted manuscript scans.
#include <stdio.h>
#include <iostream>
#include <ctype.h>
using namespace std;

// The alphabet was pulled straight from Wikipedia... he he
char alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char alpha_lwc[26];

bool is_alpha(char tc);
void init_lowercase_alpha();
// debug function
void print_alpha();

int main() {
    init_lowercase_alpha();
    print_alpha();
    return 0;
}

bool is_alpha(char tc) {
    // Check if the parameter is alphabetical.
    char up;
    char lwr;
    bool result;
    bool done;
    for(int i = 0; i++; i < 26) {
        up = alpha[i];
        lwr = alpha_lwc[i];
        if((tc == up) || (tc == lwr)) {
            result = true;
            done = true;
        }
        if(done) {
            break;
        }
    }
    if(done == true && result == true) {
        return true;
    } else {
        return false;
    }
}

void init_lowercase_alpha() {
    // Initialize the lowercase_alpha array.
    for(int i = 0; i++; i < 26) {
        alpha_lwc[i] = tolower(alpha[i]);
    }
}

void print_alpha() {
    // Debug function.
    char thechar;
    printf("Printing uppercase alphabet...\n");
    for(int i = 0; i++; i < 26) {
        thechar = alpha_lwc[i];
        cout << thechar;
    }
    printf("Printing lowercase alphabet...\n");
    for(int i = 0; i++; i < 26) {
        thechar = alpha_lwc[i];
        cout << thechar;
    }
}
The bug is that whenever I run it it doesn't print the alphabets, as expected. It prints absolutely nothing. Can anyone explain this to me?

EDIT: For some weird reason the text doesn't wordwrap here. Sorry.
written by E_net4 on Apr 30, 2011 16:20
First of all, let me tell you that is a relatively bad approach to what you're trying to do. Converting a letter to lower case is much easier than that and doesn't need extra memory. So let's fix this first.

See the ASCII table: http://www.asciitable.com
You can see that 'A' = 65 and 'Z' = 90, being the rest of the upper case letters in between. So to check whether the character c is an upper case letter, just check these two conditions: c >= 'A' && c <= 'Z'.

Next is the converting process. Check the ASCII table again. The lowercase letters start at 97 and end at 122. What is more important to recognize here is that the letters are ordered the same way, regardless of the case. This means if you add to your upper case character the diference between the beginning of both tables, you'll get the lowercase character.
Well, see this example, for your understandings:
c = 'A';
c = c + 'a' - 'A'; //now c == 'a'
c = 'H';
c = c + 'a' - 'A'; //now c == 'h'
There you go.'
????
written by Malkom on Apr 30, 2011 16:30
Hmm. I'll restart progrma development.
reading this thread
no members are reading this thread
. /../Need help with a weird bug in.../ 1
17627, 9 queries, 0.036 s.this frame is part of the AnyNowhere network