![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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;
}
|
|
||||
|
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);
}
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|