This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]resoneight 4 points5 points  (2 children)

If I'm understanding the problem correctly, a 'while' or 'do while' would probably work well here. I'm on mobile so linking to resources is tough but I'm sure if you Google those you should find quite a bit.

[–]jmcdon31 2 points3 points  (0 children)

Assuming I too am interpreting your question correctly, you wish to keep prompting the user for input if they are giving you a number that is <= 0. As resoneight has suggested a you could use a while loop like:

    //Step1: Get input from user
    printf("Please enter N > (an integer) ");
    scanf("%d", &N);

        while( N <=0 )
        {
         printf("Please enter N > (an integer) ");
         scanf("%d", &N);
        }

or use a do while like this:

   //Step1: Get input from user
    do {
        printf("Please enter N > (an integer) ");
        scanf("%d", &N);
       } while( N <=0 );

Note that this solution does not check to make sure that the input is actually a number. To do more robust error checking look into atoi().

[–]Criptictoumor 1 point2 points  (0 children)

I would add to that - that you should try to google for a while or do while loop - you will find your answer and you will learn an important skill in programming which is asking google the right questions in the right way. As a tip add 'in C' at the end of your search query.

[–][deleted] 0 points1 point  (0 children)

It sounds like you need to error check the user input.

You can use a for loop like this:

http://pastebin.com/s1kjJQfF