Thread: Help with while loop

  1. #1
    Registered User
    Join Date
    Sep 2020
    Posts
    2

    Help with while loop

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 5
    
    
    int main()
    {
        int counter = 0, num = 0;
        int memory[N];
        while(scanf("%d", &num) != EOF){
            if (counter == N){
                for(int i = 0; i< N-1; i++){
                memory[i] = memory[i+1];
            }
            memory[N-1] = num;
            continue;
            }
            memory[counter] = num;
            counter++;
    
    
        }
            for(int i = 0;i < counter; i++){
                printf("%d ", memory[i]);
            }
    
    
        return 0;
    }

    My while loop wouldn't terminate when I enter -1 as value, it just keeps registering more and more numbers into the array even the -1 and never reaches the second part of the code(the printf).

    the only solution I could think of is adding another if statement but I feel like it's supposed to be working without it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define N 5
    
    
    int main()
    {
        int counter = 0, num = 0;
        int memory[N];
        while(scanf("%d", &num) != EOF){
            if(num == EOF){
                break;
            }
            if (counter == N){
                for(int i = 0; i< N-1; i++){
                memory[i] = memory[i+1];
            }
            memory[N-1] = num;
            continue;
            }
            memory[counter] = num;
            counter++;
    
    
        }
            for(int i = 0;i < counter; i++){
                printf("%d ", memory[i]);
            }
    
    
        return 0;
    }

    If it's not supposed to be working without the second if statement may I recieve a small explanation to why it is like this?

    Thanks alot in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In your original code, I would change this:
    Code:
    while(scanf("%d", &num) != EOF)
    to this:
    Code:
    while(scanf("%d", &num) == 1 && num != -1)
    That is, you keep looping as long as you read and assign 1 value in the scanf, i.e., to num, and num is not equal to -1. Previously, your loop didn't terminate when you entered -1 because you didn't check for it.

    In your revised code, you did check for it, but by incorrectly comparing it to EOF. EOF could be -1, but that isn't guaranteed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    2
    After reading your comment I came to a conclusion that my assumption about EOF was wrong so I went to re-read information about EOF and noticed that I need to enter ctrl+z instead of -1.
    With that my initial code is working as expected!

    Thanks alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 4
    Last Post: 11-05-2015, 03:29 PM
  3. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  4. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  5. Replies: 23
    Last Post: 04-05-2011, 03:40 PM

Tags for this Thread