Coder's Guild Mailing List

Re: Reading in ints/chars was "C problem..."

Posted by Eian Counts on 1999-06-10

Ok, one way that you can approach it is to actually do your reading 
in strings (array of characters). That way you can catch the change 
easier. The following is some sample code that would give you an 
idea of what I am talking about.

Note: I think that there is a predicate that tests to see if something 
is a character, but I forgot it and so wrote my own.....

Of course, this code will halt and the loops will fall through when 
the first non-number string is encountered.

#define IS_CHAR(x) x >= 'a' && x <= 'Z'
#define TRUE 1
#define FALSE 0

char inputString[100];
int the_int;
int goodInt = TRUE;

while (goodInt)
  {
    scanf("%s",inputString);
    for(int i = 0;i < strlen(inputString);i++)
        if(IS_CHAR(x))
           goodInt = FALSE;
    if(goodInt)
      {
        the_int = atoi(inputString);    
	  /* mess with int here */
      }
  }

On 10 Jun 99, at 16:26, Victoria Bellingham wrote:

> Help!!!!!!!!!!!
> I'm learning C at uni, which has an auto-marking program. My problem is
> that we had to make a program that reads in a liost of integers, and
> terminates when the user enters a character. There's probably some simple
> little trick, but I can't get it to read a character, therefore it won't
> terminate. This gets me in huge trouble because not only will my program
> not work properly, it goes into the auto-mark and takes up all the system
> memory, and crashes the whole system. But, as it's an assignment here, the
> staff can't really give me help fixing it. Input? ideas? code!!??!!??
> Thanks.
> 
> 
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com
>