c - 为什么我无法获取字符串输入?

标签 c arrays while-loop char scanf

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

  • 第一个 scanf
  • 到任何引用 char* 变量的地方,
  • 最终堆栈指针(我将其命名为 towerIndicator)根本不会改变。
  • 然后,每个键入的输入都会以某种方式被搞砸:如果我键入“+314”将 314 添加到堆栈中,如果上面的所有问题在编译时都以某种方式阻止,它最终会添加 3144。

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] = '\0';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

switch( kindOfCommand ) {

并且在打破时

if( kindOfCommand == '0' ) break;

结束前也返回

return 0;

关于c - 为什么我无法获取字符串输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32882650/

相关文章:

python - 在python中对数组列表进行分类

java - 需要帮助在 java 中循环我的 try 和 catch 语句

javascript - 如何根据对象数组中的值过滤键

c++ - 多核多处理器环境中的主机和插槽有什么区别?

c - 如何仅为一个随机数生成器生成不同的随机数并保持其他随机数生成器相同

c - C语言的FIFO读取和删除

SQL JSON_VALUE/JSON_QUERY 从数组并转置为行

java - "do while"出现问题

c - 为什么“while(!feof(file))”总是错误的?

c - MPI协议(protocol)中的MPI_Comm如何实现?