Im trying to get userInput from one function than use it in another. When I test to see if the char is working in the DetermineWhatCommand function the first time I type characters I get the wrong output but after that the next entered string appears correctly.
#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;
}
Output:
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