0

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);    
}
miltonb
  • 6,905
  • 8
  • 45
  • 55
  • 2
    You could have removed a lot of empty lines to make your code more readable. – Fiddling Bits Oct 29 '15 at 20:16
  • just guessing, there is a spacein the last scanf... I would advise narrowing this down, make a simpler example that has the same issue and keep stripping away code. That way, people on SO can be more helpful. – Jaap Oct 29 '15 at 20:17
  • The way code formatting works: first you need a blank line, then every line of code must begin with 4 space characters. Additional space characters should be used for indenting. – user3386109 Oct 29 '15 at 20:18
  • Yes i have tried this. And sorry for the messy code was in a hurry ill try to fix it up a bit. – Chad Nash Oct 29 '15 at 20:51
  • @Chad does it remain blocked if you type some characters, and then press `Enter`? – A.S.H Oct 29 '15 at 20:55
  • yes it does. The code just keeps receiving input for divider = getchar(). I have also tried using scanf for this. When i say its constantly receiving input i mean anything before divider = getchar() is printed but nothing after it runs. It just keeps allowing me to type and press enter indefinitely – Chad Nash Oct 29 '15 at 21:02
  • Obviously you have *undefined behavior*. Most likely reason is that you have exceeded the bounds of your array. You should probably check for that once again. Sorry I could not review the code to verify it, it looks a little bit complicated. – A.S.H Oct 29 '15 at 21:08
  • ok thank you ill look into that – Chad Nash Oct 29 '15 at 21:18
  • Still can't find anything wrong with my arrays bounds and such. But again i'm not entirely familiar with all the c's susceptibility to undefined behavior. Btw divider is just a single char, not an array of chars. – Chad Nash Oct 29 '15 at 21:25
  • Also note that if I add another getchar() or scanf() after the divider = getchar() then the divider = getchar() works but then the program does the same thing on the next scanf or getchar() and doesn't continue. – Chad Nash Oct 29 '15 at 21:37
  • how did you know that it passes it and gets stuck at the next getchar? Are you stepping into with a debugger I guess? – A.S.H Oct 29 '15 at 22:04
  • just tests. That is if put print statements directly before it and they ran, directly after and they didn't run, I put another scanf() function after everything and then the printf() after divider = getchar() ran however then it got stuck on the new scanf and nothing ran after the new one. I've tried flushing the input and output and that didnt work. For some reason the final input function is just getting stuck. If you have a better explanation please tell. – Chad Nash Oct 29 '15 at 22:28
  • I am sorry @Chad, I really fail to see the reason of this strange behavior. – A.S.H Oct 29 '15 at 23:15
  • Please post a [MCVE](http://stackoverflow.com) along with the actual input you are providing. Otherwise people can only guess. – M.M Oct 30 '15 at 01:14
  • You should `fflush(stdout);` at the end, or `printf("\n");` - output may be line-buffered so perhaps your code reached that line but you don't realize it – M.M Oct 30 '15 at 01:15
  • This is one of the All-Time most frequently asked questions on StackOverflow. Did you ever consider looking that the top 2-3 **Related:** topics conveniently listed for you on the right side of the page? e.g. [**problem with flushing input stream C**](http://stackoverflow.com/questions/1384073/problem-with-flushing-input-stream-c?rq=1) or [**Using scanf to accept user input**](http://stackoverflow.com/questions/2144459/using-scanf-to-accept-user-input?rq=1) – David C. Rankin Oct 30 '15 at 14:16

2 Answers2

1

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.

geeks_kick
  • 151
  • 7
0

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

char divider;
divider = getchar();

Hope this helps!

ricpacca
  • 800
  • 6
  • 9