Fladio Armandika
Fladio Armandika

Reputation: 1

Input characters with getche and store it to the array

i want to input character and store it to the array StringTemp[5][50]. it will stop input character if i press enter button. but it's not working

char StringTemp[5][50];
char c;
int i,o;
i = 1;
o = 1;

c = getche();
while (c != EOF && c != '\n') {
    if (c == ' ') {
        i++;
        o = 1;
    }
    else {
        StringTemp[i][o] = c;
        o++;
    }
    c = getche();
}

any suggestion?

Upvotes: 0

Views: 623

Answers (1)

Sumit Gemini
Sumit Gemini

Reputation: 1836

  1. There are variation depending on keyboard and stdin, but getche() gets the key without echoing. When the user types Enter, the un C-ified char may be '\n' or '\r'. When this is printed you get the corresponding line-feed or carriage return. Since Enter maps to '\r' on your keyboard, when fetched via getche(), test for that and print '\n'. When fetched via getchar(), C translates the Enter to '\n'.

    int ch = getche();

    if (ch == '\r') c = '\n';

    printf("%c", ch);

  2. Use int c so code can check for EOF.

Upvotes: 1

Related Questions