Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite while loop and control-c

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.)

like image 873
user3727648 Avatar asked Nov 17 '14 04:11

user3727648


People also ask

Can a while loop be infinite?

In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. It is an infinite loop which will run till a break statement is issued explicitly.

Why is my while loop infinite?

Basically, the infinite loop happens when the condition in the while loop always evaluates to true. This can happen when the variables within the loop aren't updated correctly, or aren't updated at all.

How do you break a while loop in CTRL C?

To end a while loop prematurely in Python, press CTRL-C while your program is stuck in the loop. This will raise a KeyboardInterrupt error that terminates the whole program. To avoid termination, enclose the while loop in a try/except block and catch the KeyboardInterrupt .


2 Answers

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;

}
like image 86
Rizier123 Avatar answered Sep 24 '22 23:09

Rizier123


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

like image 26
Gopi Avatar answered Sep 24 '22 23:09

Gopi