Why does this create an infinite loop

0 Ash Marz · July 1, 2015
    int number;
    int sum;
    char i;
    printf("Enter a number: ");
    scanf("%d", &number);
    while (i != 'q')
        printf("Enter a number: ");
        scanf("%d", &number);
    sum = number + number;
    printf("The sum is %d", sum);

Post a Reply


Oldest  Newest  Rating
0 Gitesh Khanna · August 23, 2015
You are never making the Variable i equal to q, so your condition 
(i != 'q')

is always standing to be true and hence an infinite loop is the resultant.
0 Rich Sharma · August 21, 2015
yeah i is not equal to q in this case i think what you were trying to do was trying to make the user input a character, or a number and got those mixed up. i is usually used to represent the word index, used in for loops etc.
+2 Bucky Roberts · July 1, 2015
It loops as long as
(i != 'q')

but you are never setting the value of i. You are just having the user input a number.
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator