为什么我不能得到字符串输入?

Why can't I get string input?

我试图模拟堆栈概念,这是我的代码,到处都是错误,来自

gcc 没有通知我任何可用的错误消息,所以我根本不知道去哪里。在这里迫切需要帮助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    const int towerHeight = 32;
    int tower[towerHeight];
    int towerIndicator = 0;
    /*
    printf("%i개의 정수를 담을 수 있는 스택을 만들었습니다.\n", towerHeight);
    printf("- '+'를 붙여서 정수를 담습니다.\n");
    printf("- '-'를 입력해 정수를 빼냅니다.\n");
    printf("- '?'를 입력해 스택을 확인합니다.\n");
    printf("- '0'를 입력해 작업을 종료합니다.\n");
    printf("명령을 입력해주세요.\n================================\n");
    */
    char* command;
    char* kindOfCommand[1];
    char* actualCommand;
    while(1) {
        printf("> ");
        scanf("%s", command);
        printf("%s", command);
        strncpy(*kindOfCommand, command, 1); kindOfCommand[1] = '[=10=]';puts("#");
        strncpy(actualCommand, command+1, strlen(command)-1);puts("$");

        switch(**kindOfCommand) {
                int i;
            case '+':
                if(towerIndicator<towerHeight) {
                    tower[towerIndicator] = atoi(actualCommand);
                    towerIndicator++;
                    printf("현재 %i개의 값이 있습니다.\n", towerIndicator);
                } else printf("더 이상 넣을 곳이 없습니다.\n");
                break;
            case '-':
                if(towerIndicator>0) {
                    towerIndicator--;
                    printf("%i\n", tower[towerIndicator]);
                    printf("현재 %i개의 값이 있습니다.\n", towerIndicator);
                } else printf("더 이상 빼낼 값이 없습니다.\n");
                break;
            case '?':
            default:
                printf("[");
                for(i=0; i<towerIndicator; i++) {
                    if(i==towerIndicator) printf("[%i]", tower[i]);
                    else printf("%i", tower[i]);
                    if(i!=towerIndicator-1) printf(" ");
                }
                printf("]\n");
                break;
        }

        if(**kindOfCommand=='0') break;
    }

}

此处需要进行相当多的修改

松散修复可能需要更多修复

//    char* command;    // <-- initialize this, failure in scanf other wise
      char command[120] ; 

假设您正在寻找单个字符,请不要使代码复杂化

//    char* kindOfCommand[1];  pointer not required
          char kindOfCommand;

因为你在某处使用 strncpy

//    char* actualCommand; // <-- initialize this
    char actualCommand[126];

kindOfCommand 代码更改

//        strncpy(kindOfCommand, command, 1);
          kindOfCommand = *command;// since you are taking single character
          puts("#");

开关上还有一些

switch( kindOfCommand ) {

同时打破

if( kindOfCommand == '0' ) break;

结束前还有return

return 0;

我应用了 kkk 的答案中的更改,现在获取输入效果很好。

char command[11];
char kindOfCommand;
char actualCommand[10];
while(1) {
    printf("> ");
    scanf("%s", command);
    kindOfCommand = *command;
    memset(actualCommand,0,sizeof(actualCommand));
    strncpy(actualCommand, command+1, strlen(command)-1);

    switch(kindOfCommand) { ... }
    ...
    if(kindOfCommand=='0') break;
}
return 0;

}

我需要解决输入问题。这是因为当 actualCommandcommand 收到一个新的字符串并且它比之前收到的字符串更短时,字符串的最后几个字符仍然留在 actualCommand 中。所以我放了一个 memset 来在每次 while 循环循环时重置变量。它不是一个指针,所以 sizeof() 可以完成这项工作。否则我应该使用 strlen() 来告诉 memset 长度。