cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
help - how to find int from keyboard
help - how to find int from keyboard
Aug 7, 2011 at 5:14pm UTC
orhan kanat
(2)
// input >> abcd234. output>> total= 9
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char c;
c='a';
int sum=0;
while(c!='.'){
scanf("%c",&c);
if (isdigit(c))
sum+=c;
}
printf("%d",sum);
system("PAUSE");
return 0;
}
Aug 7, 2011 at 6:44pm UTC
Breadman
(111)
Chars work in ascii values. so 'a' is 97. '0' is 48. so you have to subtract '0' from c to get the actual number value :P
1
2
3
if
(isdigit(c)) sum+=(c-
'0'
); }
haven't tested will it work :/
Aug 7, 2011 at 7:31pm UTC
orhan kanat
(2)
thank you :P
Topic archived. No new replies allowed.