Home > Mobile >  C Can't print certain words from Char array
C Can't print certain words from Char array

Time:08-19

Not sure how to phrase the question, but I'm making a program for an assignment, which we're not allowed to use pre-existing libraries besides input/output. We also can only use primitive data-types. I have to read a text file with words, remove all punctuation from the word, and then store those words in a 2D array of characters.

This problem seems to be that when a word starts with a non-alphabetic character, the whole word doesn't output when using cout << stack[top] but when I output each individual character with cout << stack[top][i], it produces the expected output.

'stack' is a 2D array which contains characters to make up words.

'top' is a variable to represent the length of stack

Code:

#include <iostream>
#include <fstream>

using namespace std;

// Function Prototypes
void push(char word[]);
char formatCharacter(char letter);
bool isAlphabet(char letter);
char toLowercase(char letter);

// Global Variables
const int STACK_SIZE = 50000;
const int WORD_SIZE = 30;
char stack[STACK_SIZE][WORD_SIZE];
int top = 0;
int words = 0;
int wordCount[STACK_SIZE];    

int main(){
    // Local Variables
    char filename[20];
    ifstream fin;
    char word[WORD_SIZE];

    // Get file input
    cerr << "Please enter the name of the input file: ";
    cin >> filename;

    // Open file
    fin.open(filename);
    // Print error if file doesn't open, then quit the program.
    if (!fin) {
        cerr << "Error opening file " << filename << ". Program will exit." << endl;
        return 0; 
    }

    // Read the file into the stack
    while (fin >> word) {
        push(word);
    }

    // Close file
    fin.close();
}

void push(char word[]){
    if (top == STACK_SIZE) return;

    int i = 0;
    int j = 0;

    do {
        if (isAlphabet(word[i])){
            word[i] = formatCharacter(word[i]);
            stack[top][i] = word[i];
            cout << stack[top][i]; // Output fine
            j  ;
        }
        i  ;
    } while (word[i]);

    wordCount[words] = j;
    //cout << stack[top] << ": " << wordCount[words] << endl; // Output incorrect
    cout << endl;
    top  ;
    words  ;
    return;
}

bool isAlphabet(char letter){
    if ((letter < 'A' || letter > 'Z') && (letter < 'a' || letter > 'z')){
        return false;
    }
    else{
        return true;
    }
}

char formatCharacter(char letter){
    if ((letter < 'A' || letter > 'Z') && (letter < 'a' || letter > 'z')){
        letter = '\0';
    }
    else{
        if (letter >= 'A' && letter <= 'Z'){
            letter = toLowercase(letter);
        }
    }
    return letter;
}

char toLowercase(char letter){
    letter = letter   32;
    return letter;
}

isAlphabet() just checks if it's an alphabetic character

formatCharacter() removes any punctuation by replacing the character with '\0', and also changes uppercase to lowercase.

Input:

Jabberwocky

'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.

Output when using cout << stack[top][i]:

jabberwocky
twas
brillig
and
the
slithy
toves
did
gyre
and
gimble
in
the
wabe
all
mimsy
were
the
borogoves
and
the
mome
raths
outgrabe

Output when using cout << stack[top]:

jabberwocky: 11
: 4
brillig: 7
and: 3
the: 3
slithy: 6
toves: 5
did: 3
gyre: 4
and: 3
gimble: 6
in: 2
the: 3
wabe: 4
all: 3
mimsy: 5
were: 4
the: 3
borogoves: 9
and: 3
the: 3
mome: 4
raths: 5
outgrabe: 8

Notice the word 'twas' is missing. I'd rather not loop through each character of each word to get the output I need. I'd appreciate any advice, thanks!

CodePudding user response:

The simplest fix is to change:

stack[top][i] = word[i];

To:

stack[top][j] = word[i];
           ^ j not i here

This will ensure that the 'Twas ends up as twas and not \0twas.

Also, formatCharacter() should call isAlphabet() rather than repeat the condition.

  • Related