Reputation: 1
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
Reputation: 1836
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);
Use int c so code can check for EOF.
Upvotes: 1