Reputation: 485
So, I wrote the following code:
void main(void) {
int charNums[ALPHABET], i = 1;
char word[MAX];
while(i) {
initialize(charNums, word);
getString(word);
setLetters(charNums, word);
getString(word);
checkLetters(charNums, word);
if(isZero(charNums))
printf("Anagram\n");
else
printf("Not anagram\n");
}
}
The while loop is infinite, which it is supposed to be. My professor said something about using CTRL-C to exit the infinite while loop, but that doesn't work with what I've coded. Am I missing something simple or what? Can someone please point me in the right direction? Thank you! (Note: this is only a portion of the code.)
Upvotes: 6
Views: 16617
Reputation: 19874
while(1)
{
}
This is a infinite loop there should be some condition in the while loop to break out of it.
ctrl+c
will terminate you program. So instead of ctrl+c there should be some condition within the loop to break
out of it.
If ctrl+c
should be used to break out of the loop then you need to use a signal handler
to handle your ctrl+c
signal
Upvotes: 1
Reputation: 59701
This should work for you:
(Here i'm adding a signal handler which checks if you press ctrl + c and if so it's stops the loop)
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
volatile sig_atomic_t stop;
void inthand(int signum) {
stop = 1;
}
int main(int argc, char **argv) {
signal(SIGINT, inthand);
while (!stop)
printf("loop\n");
printf("exiting safely\n");
system("pause");
return 0;
}
So i think your program should look something like this:
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
volatile sig_atomic_t stop;
void inthand(int signum) {
stop = 1;
}
int main(int argc, char **argv) {
signal(SIGINT, inthand);
int charNums[ALPHABET], i = 1;
char word[MAX];
while(!stop) {
initialize(charNums, word);
getString(word);
setLetters(charNums, word);
getString(word);
checkLetters(charNums, word);
if(isZero(charNums))
printf("Anagram\n");
else
printf("Not anagram\n");
}
printf("exiting safely\n");
system("pause");
return 0;
}
Upvotes: 16