The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Digits display Spoorthi16 UNIX for Dummies Questions & Answers 3 10-01-2007 08:26 PM
Only Digits as input namishtiwari UNIX for Dummies Questions & Answers 2 08-21-2007 06:23 AM
Extract digits at end of string offirc Shell Programming and Scripting 6 11-20-2006 11:57 AM
How to cut last 10 digits off psarava Shell Programming and Scripting 4 08-29-2006 03:52 AM
making a .sh wait for user input verystupid UNIX for Dummies Questions & Answers 9 01-28-2004 06:59 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-21-2006
bebop1111116 bebop1111116 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 30
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 (permalink)  
Old 10-21-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,929
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);
}
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0