Reputation: 25
So i am trying to get this program to simply exit but i can seem to get it to work.
i tried a lot of things. one thing i found odd as well is if i made x = 0 in the while loop it would immediately exit the program so i had to make it x !=0. i dont understand what that is so if you can answer that as well that would be nice.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char a[256];
int x = 0;
while (x != 0) {
int y = strcmp(fgets(a, 256, stdin), "exit");
if (y==0) {
exit(0);
}
else {
printf("Line read:%s\n",a);
}
}
return 0;
}
so it should output right now something like this(i am also running this in a linux terminal):
//input://
hello
//output://
Line read: hello
//(program stays open for more inputs)//
//input//
exit
//(the program now closes in the terminal and you return to directory)//
Upvotes: 0
Views: 60
Reputation: 2933
The keyboard input contains a new line at the end. Change "exit"
to "exit\n"
in call to strcmp
.
Upvotes: 1