1

here is the code to add the numbers present in an Alphanumeric string :

#include<stdio.h>
#include<stdlib.h>
int main()
{
int total=0;
char ch;
printf("enter the string\n");
ch=getchar();
while(ch!='\0')
{
    printf("I am here !!");
    if (!(isalpha(ch)))
        total+=(int)ch;
    ch=(char)getchar();
    printf("I am here !!");
}
printf("\ntotal is %d",total);
return 0;
}

No matter what characters I input , it gives 4 " I am here " for each character.

I tried to use

while((ch=getchar())!='\0');

but it gives the same problem .

yashC
  • 887
  • 7
  • 20
  • 5
    I really want to know how you input the `nul` character? how is it? – Iharob Al Asimi May 18 '15 at 12:38
  • Use `while(ch!='\n')` and make `ch` an `int`, not a `char`. Also, remove all the casts. – Spikatrix May 18 '15 at 12:39
  • 2
    Your loop never encounters a NULL character, so it never breaks. – user4520 May 18 '15 at 12:40
  • @iharob I wanted to run it till I encounter '\n' . I am a newbie at C sorry !! – yashC May 18 '15 at 12:47
  • 1
    @yashC It's ok, but can I give you advice? Ok I will, don't ask someone about something you can figure out by yourself, it's an important part of teaching yourself and learning how to teach yourself, if you discover this by your own, you will **never** forget it, so ask only when you really run out of ideas. :) Also, just for courtesy accept one of the ansewers below, please don't pick mine for courtesy, pick one that really helped understand. You can't delete your question because there are answers with positive score and it would be rude. – Iharob Al Asimi May 18 '15 at 12:55
  • 1
    @iharob Thanks alot for your help and guidance (: . I was accepting the answer below but at first it showed " Cant Accept with in 3 mins " and then " error occurred , try again " . – yashC May 18 '15 at 13:02
  • 1) getchar() returns an 'int', not a 'char'. 2) the return key press is what is causing the double execution problem. at the bottom of the loop insert 'while( getchar() != '\n');' – user3629249 May 19 '15 at 20:29

2 Answers2

9

getchar does not return '\0' at the end of the input: it is not reading from a null-terminated C string, but from a console, file, or some other stream.

When no additional input is available, getchar returns EOF. That is the condition you should be checking to decide when to stop your loop.

Stack Overflow offers many good examples of how to implement a loop reading getchar (link#1; link#2; please note the data types used in the examples).

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

The reason it doesn't work is because '\0' cannot be inserted from the keyboard, so getchar() is unlikely to be able to return '\0', a correct way of testing for the end of input would be

int ch;

while (((ch = getchar()) != EOF) && (ch != '\n'))

This is because EOF means, that the user intentionally wanted to stop entering data, and '\n' is usually the last thing that will be seen when stdin is flushed, since it triggers the flushing.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97