0

How can I use a single while loop to restrict entry of negative integers to the code. I have previously tried with two while loops each for the two scanf() and works cool. But I am unable to figure our how to combine them into one while loop. I have removed the two while loops I have originally used. I am completely new to this and I need some ideas.

#include <stdio.h>

int main()

{   
    int num, uplimit, a;

    printf("Which multiplication table do you want? ");

    scanf("%d",&num);

    printf("Please enter the upper limit: ");

    scanf("%d",&uplimit);


    printf("\nMultiplication Table for %d\n", num);

    printf("===========================\n\n");


for(a=1;a<=uplimit;++a)

{

    printf( "%d X %d = %d  %s\n", a, num, num*a, (num*a % 2 == 0) ? "*" : " " );

}

{

    printf("\n* indicates even multiple of %d", num);

}

return 0;
}
3

2 Answers 2

3

Simply replace your scanfs by this:

while(scanf("%d", &num) != 1 || num < 0)
    while ((c = getchar()) != '\n' && c != EOF);
5
  • I was going to answer this question. Thought better. You need to eat the crap, i.e. if it not a number the eat the next character. BTW -1 is harsh and I will rectify that
    – Ed Heal
    Jul 26, 2014 at 14:57
  • Thanks, yeah I did not understand where it came from :) Jul 26, 2014 at 14:58
  • when you enter a char rather than a integer it will not be accepted by scanf and thus remains in buffer and next time in loop again scanf will see that char. So you need to clear it somehow
    – mSatyam
    Jul 26, 2014 at 15:04
  • @mSatyam Thank you for pointing that out, indeed this is very important and I answered a little too fast here. I have now edited my answer. Jul 26, 2014 at 15:07
  • yeah sometime i too miss it.no issues
    – mSatyam
    Jul 26, 2014 at 15:10
3
char c;

while (((c = getchar()) != '\n') && (c != EOF));
scanf("%d",&num);
while (num < 0)
{
    printf("You should use positive numbers!");
    while (getchar() != '\n');
    scanf("%d", &num);
}

Inner while makes input buffer to clean if there were some letters in input. Also, it makes sence to move while (((c = getchar()) != '\n') && (c != EOF)); into a function.

7
  • Could be a lot more improved by do whiles and other binding. Jul 26, 2014 at 14:54
  • do {} while dont make it possible not to write error message if the first input attempt was correct without additional check, do it?
    – Xandrmoro
    Jul 26, 2014 at 14:58
  • Well yes, but how about I enter 10a. Your code will say it's ok but the user entered 10a not 10 Jul 26, 2014 at 15:02
  • Ive tried it now. It wont say its ok, it will run into infinite loop in the next number - we need to flush input before reading, cause it leaves "a" in it. Fixed ansver.
    – Xandrmoro
    Jul 26, 2014 at 15:18
  • 1
    fflsuh()is windows only. So this not yet a standard answer. As soon as it becomes one I will remove my down vote. Jul 26, 2014 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.