Я пытаюсь получить userInput из одной функции, чем использовать ее в другой.Когда я проверяю, работает ли char в функции DetermineWhatCommand , при первом вводе символов я получаю неправильный вывод, но после этого следующая введенная строка отображается правильно.
#include<stdio.h>
#include<string.h>
#define MAX 100
char * GetUserInput(){
char userInput[MAX];
fgets(userInput, sizeof (userInput), stdin);
userInput[strcspn(userInput, "\n")] = '\0';
return userInput;
}
void DetermineWhatCommand(char *userInput){
printf(userInput);
}
int main() {
char * userInput;
userInput = new char[MAX];
char exitTest[] = "exit";
while(strcmp(exitTest, userInput) != 0){
userInput = GetUserInput();
DetermineWhatCommand(userInput);
}
return 0;
}
Выход:
Hello //First string entered
@ //What the output in the function looks like
Hello //Second string entered
Hello //What the output in the function looks like