简体   繁体   中英

Basic c program doesn't continue after the final scanf in the function, scanf just keeps accepting input endlessly

Here is relevant code:

I am somewhat new to the world of c so please help me out The problem is at the bottom of the code: the divider = getchar() this bit of code gets the program stuck and it never progresses after that it. Its just an infinite loop of allowing input. What is wrong. Please be specific as I am not entirely familiar with complex concepts (though I am eager to solve this issue no matter how complex the solution may be)

void read_in(int n, int m)
{

    int a = 0, b = 0, i = 0, x;

    while (a != n && b != m)
    {
        if (b == 0 && i == 0)
        {
            printf("\nEnter row %d:  ", a);

         }

         scanf("%d", &array[a][b]);

         diglength[a][b] = floor (log10 (abs (array[a][b]))) + 1;

         if (b == m - 1)
         {
             a++;            
             b = -1;
         }
         if (b == -1 && i != 0 && a != n)
         {
             printf("\nEnter row %d:  ", a);
         }

         i++;
         b++;
    }

    printf("\nEnter the number of new lines in between rows: ");
    scanf("%d", &newlines);
    printf("\nEnter the span of each array element: ");

    scanf("%d", &span);

    check_span(n, m);
    getchar();
    printf("\nEnter the type of justification (l - left   r - right): ");

    just = getchar();

    while (just != 'r' && just != 'l')
    {
        printf("\nInvalid argument");
        printf("\nEnter the type of justification (l - left   r - right): ");
        scanf(" %c", &just);
    }

    printf("\nEnter the character to be placed in between elements: ");
    getchar();
    divider = getchar();
//THE PROGRAM NEVER GETS PAST HERE IT GETS STUCK ON THE ABOVE CHARACTER ASSIGNMENT...WHY

    printf("it got here "); printf("%c", divider);    
}

I used Xcode to test this, and because you didn't provide all of you code I had to skip a lot of it out and tested the following:

#include <stdio.h>

void read_in(int n, int m)
{ 
    char just = getchar();
    char divider;

    while (just != 'r' && just != 'l')
    {
        printf("\nInvalid argument");
        printf("\nEnter the type of justification (l - left   r - right): ");
        scanf(" %c", &just);
    }

    printf("\nEnter the character to be placed in between elements: ");
    getchar();
    divider = getchar();


    printf("it got here "); printf("%c", divider);    
}

int main(int argc, const char * argv[]) {

    read_in(8, 9);
    return 0;
}

And got the following output:

Invalid argument
Enter the type of justification (l - left   r - right): l

Enter the character to be placed in between elements: k
it got here k

I suspect that there might be something wrong outside of this function or piece of code you posted.

In other news - scanf() , I find, can be confusing. I choose to keep the formatting in mine as follows: scanf("%c", &foo) then use an purge(stdin) to start the input buffer from 'fresh'.

printf("Enter the character to be placed in between elements: ");

fpurge(stdin);
scanf("%c", &divider);

printf("it got here ");
printf("%c", divider);

Using the above code I still managed to get to the final line and, personally, I think there's less room for error in the scanf() .

Hope that helps you fix the problem.

Did you initialise the variable divider somewhere? Try to replace the last two lines with:

char divider;
divider = getchar();

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM