scanf in a nested loop

mojeaux
mojeaux used Ask the Experts™
on
Hi -
I'm working on my program this morning and I'm trying to determine why my first scanf statement works but the second one does not.  It compiles normally. It runs fine.   It reads the first resp1 and evaluates it correctly.  However if i provide an invalid response it will go thru the second loop but not wait for a second response and so each time i get an invalid response on the second loop.   What am I missing here?

for(count=0;count<100;count++)
      {
      i = rand();
        if ((i > 0) && (i <= 999))
        {
        randmArray [count] = i;
        sigChk(&i);
        printf ("Your array element is %d.  Your random number is %d\n", count, i);
        }
        else
        count--;
   }
  printf ("You now have 100 random numbers in your array!\n\n");  
 
  printf ("Would you like to display your array in table format?\n");
  printf ("Please enter 'Y' for Yes and 'N' for No.\n\n");
  scanf("%c", &resp1);
      if (resp1 == 'Y')
      {
            tableFmt(randmArray, count);
          system("pause");
      }
      else if (resp1 == 'N')
      {
            printf("Sorry you do not wish to continue.  Program will end now.  Thank you.\n\n");
            system("pause");
            return (i);
      }
      else
      {
            printf ("Please enter a valid response!!\n\n");
            printf ("Try again.  Would you like to display your array in table format?\n");
            scanf("%c", &resp2);
                  if (resp2 == 'Y')
                  {
                        tableFmt(randmArray, count);
                        system("pause");
                  }
                  else if (resp2 == 'N')
                  {
                        system("pause");
                        return (i);
                  }
                  else
                  {
                  printf("Sorry that was an invalid response.  Program will end now.  Thank you.\n\n");
                  system("pause");
                  return(i);      
                  }
      }
 
  return(i);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer (Avast)
Commented:
It's been a long long time since I used scanf but if my (quite old) memory serves me, the problem is that because your first call to scanf was unable to extract anything from the file stream that data is still in the stream. The second scanf is then attempting to read the same data and fails because, it too, is unable to process the invalid stream content. Generally, you are better off reading a string (%s) and then parsing this string. There are very few input values that will not be read as a string so you should find you have an easier time of it.
mojeauxBusiness User

Author

Commented:
Thank you.   I will try reading the string.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial