Shell in C, Stage 3 Unable to git commit

I wrote a program that loops indefinitely and calls the exit() function if exit 0 was occurred. I got a weird error message when I tried to git commit even though the program worked fine when I ran the your_program bash script:
remote: [tester::#PN5] Expected prompt ("$ ") but received “”
remote: [tester::#PN5] Assertion failed.
remote: [tester::#PN5] Test failed
The code I used was:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
char input [100];
while(1){
fflush(stdout);

  printf("$ ");
  
  fgets(input, 100, stdin);
  input[strlen(input) - 1] = '\0';

  if (!strcmp(input, "exit 0"))
    exit(0);
  int i = 0;
  printf("%s: command not found\n", input);
  fprintf(stderr, "%d", i);
}

}

Hi @WINDSORKAKAMA, what error messages are you seeing? Could you share a screenshot to help me understand the issue better?

@WINDSORKAKAMA There’re two problematic lines:

Here’re the steps to fix the code:

  1. Replace fflush(stdout); with setbuf(stdout, NULL);
  2. Delete fprintf(stderr, "%d", i);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  setbuf(stdout, NULL);
  char input [100];

  while (1) {
    printf("$ ");
    
    fgets(input, 100, stdin);
    input[strlen(input) - 1] = '\0';

    if (!strcmp(input, "exit 0")) exit(0);

    printf("%s: command not found\n", input);
  }
}

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.