making sure input are digits


 
Thread Tools Search this Thread
Top Forums Programming making sure input are digits
# 1  
Old 10-21-2006
making sure input are digits

hello, everyone. I was wondering if anyone could help me out and tell me how to set up the isdigit() (or another way) to ake sure that the input are all digits and not chars. Also, the way my program is set now you need to rerun it in order to renter the data. Is there any way that i can get it to ask the user to enter data by the program prompting?

thanks

Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char * argv[])
{
    int num;
    int rightd;
    int sum = 0;

    printf ("Please enter a positive integer value:");
    scanf("%i", &num);

    if (num <= 1)
    {
         printf ("Invalid integer type, please reenter\n");
         return EXIT_FAILURE;
    }
    else
    {
         while (num != 0)
         {
               rightd = num % 10;
               sum += rightd;
               num /= 10;
          }
     }
      printf ("the sum of the digits is %i\n", sum);
      return EXIT_SUCCESS;
}

# 2  
Old 10-21-2006
Plain ordinary scanf() isn't guaranteed to scan an entire line. %i will scan up to the point where the input stops being digits, in this case, the newline, after which standard input needs to be flushed to get rid of it.

Instead, I reccomend using fgets to input an entire line. That way there's never anything to flush.

But since you're processing the entire string byte by byte, why use scanf at all? Just check it byte by byte:
Code:
#include <stdio.h>
#include <ctype.h>

// Returns negative on error, zero or positive on success
int sum_of_digits()
{
  int sum=0;
  int n;
  char buf[512];

  // Prompt
  printf("Enter digits:  ");
  // Read in entire line into buf
  if(fgets(buf,512,stdin) == NULL)
  {
    // If couldn't read, return error
    fprintf(stderr,"Couldn't read from stdin\n);
    return(-1);
  }

  // Loop through entire string
  for(n=0; buf[n]; n++)
  {
    // If it's a newline or carriage return, stop scanning
    if((buf[n] == '\n') || (buf[n] == '\r'))
      break;

    // If it's not a digit, return error
    if(! isdigit(buf[n]))
    {
      fprintf(stderr,"Input string is not digits\n");
      return(-1);
    }

    // Add value of digit to sum
    sum += (buf[n]-'0');
  }

  // Return sum
  return(sum);
}

int main()
{
  while(1)
  {
    int val=sum_of_digits();
    if(val < 0)
      break;

    printf("Sum of digits is %d\n",val);
  }

  return(0);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. Shell Programming and Scripting

Showing 4 digits

Hello everybody I'm a little beginer for shell script as I started last night... I have this script cat fichier.txt | while read l ; do #echo $l echo $x x=$(( $x + 1 )) done it's return 1 2 3 4 (4 Replies)
Discussion started by: remibemol
4 Replies

3. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

4. Shell Programming and Scripting

help: single digits inflated to 2 digits

Hi Folks Probably an easy one here but how do I get a sequence to get used as mentioned. For example in the following I want to automatically create files that have a 2 digit number at the end of their names: m@pyhead:~$ for x in $(seq 00 10); do touch file_$x; done m@pyhead:~$ ls file*... (2 Replies)
Discussion started by: amadain
2 Replies

5. Shell Programming and Scripting

total last digits

hi group, How can I count total number of 5's which are continuous in the end. i.e. in the below string, the o/p should be 4 I just know to calculate total number of 5's $ echo "95952325555" | awk -F "5" '{print NF-1}' 6 (3 Replies)
Discussion started by: uwork72
3 Replies

6. Shell Programming and Scripting

Formatting digits

I want to check the argument in KSH. If the user type in the prompt 'find 3' it will format 3 to 003 to match the data in the text file. Same as with 10 to 010. Always begins with 0. eg. >find 3 Output: 003 >find 30 Output: 030 (7 Replies)
Discussion started by: harry0013
7 Replies

7. UNIX for Dummies Questions & Answers

Digits display

Hi there, I am new to scripting. Can anyone help me in writing a script which will display all the digits between 1 and 5 inclusive, one digit per line. Should use a loop to do this. Thanks in advance!! (3 Replies)
Discussion started by: Spoorthi16
3 Replies

8. UNIX for Dummies Questions & Answers

Only Digits as input

Hi All, I am new to shell script. I wrote a very small script that takes only digits as input- but there is some problem in that.can you help me in debugging that. #!/bin/ksh echo "Digits as input" read number digit='eval ' if ] then echo "Entered number is a digit" else echo... (2 Replies)
Discussion started by: namishtiwari
2 Replies

9. UNIX for Dummies Questions & Answers

making a .sh wait for user input

I need a script to halt at the end and wait for the user to hit a key...could be any ket or enter. I know it can be done but I am just starting out.. Thanks (9 Replies)
Discussion started by: verystupid
9 Replies
Login or Register to Ask a Question