Thread: Check if input is an integer using only loops

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    4

    Check if input is an integer using only loops

    Hi there I have a homework that needs to verify if the input of the user is an integer using only loops no if statements Here's the problem:

    A program is required that prompts the user for a number. The program will then print a series of asterisks to represent the number. If the user enters a number less than 1, the program stops. For example:

    Enter a number: 5
    *****
    Enter a number: 3
    ***
    Enter a number: 9
    *********
    Enter a number: 0

    All user input must be validated:
    • Check for non-numeric input when reading numeric input
    • Check that values entered are within the expected range for their purpose, or in range based on the requirements statement

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Post your current code, if you want help you are going to have to show some effort.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    Code:
    #include <stdio.h>
    main()
    {
       int num;   // The number entered by the user
       int i;     // The number of times the for loop has to loop until matching the
       
          do
          {
             printf ("\nEnter a number: ");       // Requests a number input from the user
             scanf ("%d", &num);                  // Stores the input to the variable num
    
    
               for (i = 0; i < num; i++)          // For loop that prints the asterisk depending on the num entered
               {
                  printf("*");
               }
    
    
          }while (num != 0);                      // The problem is probably here where I could include && or || to verify if the input is an integer
    }

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    How big can the number be? You mentioned earlier that there was an "expected range". If the range has to be between 0-9 then your program gets a little easier, if you have to account for any integer value then you are going to have to think a little bit more.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    I'm not sure about the "expected range" I will have to deal with that later. Right now i'm just focused on validating the input if it is not an integer.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mababaan View Post
    I'm not sure about the "expected range" I will have to deal with that later. Right now i'm just focused on validating the input if it is not an integer.
    Right now scanf is doing that for you -- if they type in an integer it will read it; if they type in not an integer it won't[footnote1]. You have to listen to what scanf tells you, though, by looking at what it returns.
    Code:
    status = scanf("%d", &x);
    if (status == 1) {
        //good times
    } else {
        //bad input -- clear it out and try again
    }
    [1]Note that if they type something that starts with an integer, like 12x or even 14.73, then it will report success that it read an integer (and left the other stuff still waiting to be processed). If that's not what you want, then scanf is not the tool for the job.

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by tabstop View Post
    Right now scanf is doing that for you -- if they type in an integer it will read it; if they type in not an integer it won't[footnote1]. You have to listen to what scanf tells you, though, by looking at what it returns.
    Code:
    status = scanf("%d", &x);
    if (status == 1) {
        //good times
    } else {
        //bad input -- clear it out and try again
    }
    [1]Note that if they type something that starts with an integer, like 12x or even 14.73, then it will report success that it read an integer (and left the other stuff still waiting to be processed). If that's not what you want, then scanf is not the tool for the job.
    The only other problem with that is that the OP cannot use if statements :/

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SirPrattlepod View Post
    The only other problem with that is that the OP cannot use if statements :/
    Good point. I had made a mental note to try to turn it into a while ((status = scanf()) == 1) statement or similar, but then forgot. I should start writing this stuff down.

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by tabstop View Post
    Good point. I had made a mental note to try to turn it into a while ((status = scanf()) == 1) statement or similar, but then forgot. I should start writing this stuff down.
    You're ruining my fun. I was going to write a solution using buckets of water :-(

  10. #10
    Registered User
    Join Date
    Nov 2013
    Posts
    4
    Thanks tabstop, Im still new to c programming and im still in the loops chapter. I'm not sure if pointers or functions can be included here but it specifically says that:
    "These programs are to be written using loops ONLY. Do not use any “if” statements in your code. For this assignment, a properly designed solution does not require “if” statements"

    The prof showed the working program which can be accessed but the code cannot be shown and this are the sample inputs that i've tried:

    Code:
    Enter a number: 9
    *********
    Enter a number: 2.2
    **
    Enter a number: 2..2
    **
    Enter a number: 2.c
    **
    Enter a number: 2xx
    **
    Enter a number: xxx
    Invalid number entered, please re-enter: 3?xc;
    ***
    Enter a number:x
    Invalid number entered, please re-enter: x3x
    Invalid number entered, please re-enter: 3x3
    ***

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Isn't it a little silly to restrict the use of if statements but still allow loops?

    You can throw a loop into a function then have the loop return and it's practically the same as an if:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    
    long int checkNum(const char * s)
    {
        long int n;
        char * e;
    
        /* Basically the same as if (!s) return -1 */
        while (!s) {
            printf("Enter a number\n");
            return -1;
        }
    
        n = strtol(s, &e, 10);
    
        while (errno == ERANGE) {
            printf("Number is out of possible range\n");
            return 0;
        }
        while (n == 0 && e == s) {
            printf("Not a number\n");
            return n;
        }
    
        return n;
    }
    
    int main(int argc, char ** argv)
    {
        long int i, n = checkNum(argv[1]);
        for (i = 0; i < n; ++i)
            putchar('*');
        putchar('\n');
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having "if statement" check input for integer.
    By Cameron Taylor in forum C Programming
    Replies: 2
    Last Post: 06-12-2013, 10:48 PM
  2. Check to see if an integer is equal to 1-9?
    By BC2210 in forum C Programming
    Replies: 9
    Last Post: 09-24-2009, 05:46 PM
  3. Check if an Integer
    By pandadc in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2006, 11:11 AM
  4. Using loops for check a roman number input.
    By eryell in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2006, 11:04 AM
  5. Check for an integer, fail if anything else
    By derek tims in forum C++ Programming
    Replies: 19
    Last Post: 03-17-2006, 01:54 PM