Thread: Help with reading in input

  1. #16
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    #define DELIM " "
    #define BUF_SIZE 100
    void *safe_malloc(void *buf,int size);
        
    int
    main(int argc, char** argv)
    {
        char *buf;
        char *word;
        char *ch;
        char Insertion[] = "i"; 
    
        buf = (char *)safe_malloc((void *)buf,BUF_SIZE+1);
        while((buf=fgets(buf,BUF_SIZE+1,stdin))!=NULL)
        {
            word = strtok(buf, "DELIM , \n");
            switch (strcmp(word,Insertion))
            {
                case 0:
                ch = strtok(NULL, "DELIM, \n");
                    if(ch == NULL)
                
                {
                    printf("insert interger\n");
                }
                else 
                    printf("ok\n");
                
                break;
                default:
                printf("please type into the input a command to use in the BST\n");
                break;
            }
        }
    
            return 0;
    }
    
    void *safe_malloc(void *buf,int size)
    {
    
        if((buf=(char *)malloc(size))==NULL)
        {
            fprintf(stderr,"Cannot allocate memory for buffer\n");
            exit(1);
        }
                              
    
            return(buf);
    }
    Yep heres what i have so far. This produces no compile errors but what i wanna know is how do i check to see if the next token is an integer

  2. #17
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    word = strtok(buf, "DELIM , \n");
    Do you mean to check whether the characters 'D', 'E', 'L', 'I', 'M', ' ', ',', or '\n' are found?
    but what i wanna know is how do i check to see if the next token is an integer
    Try to convert the string to an integer; if that fails, it is not an integer, if it succeeds, it is.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #18
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Hi , thanks for your reply.
    Actually DELIM was defined on top as a " " space.
    How do i convert the string to an integer?

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Actually DELIM was defined on top as a " " space.

    Then take a closer look at your code.

    >How do i convert the string to an integer?

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385:
    strtol, sscanf, atoi
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To further the point, because I know you're not going to grasp Dave's point, #define textual replacement does not replace would-be tokens that are inside strings. But even if it did, look at what you have:
    Code:
    word = strtok(buf, "DELIM , \n");
    Which would translate, if it worked that way, which it doesn't, to this:
    Code:
    word = strtok(buf, "" " , \n");
    Which would translate, due to the automatic concatenating that goes on with consecutive string literals, to this:
    Code:
    word = strtok(buf, " , \n");
    Which is: "Use space and comma, and space, and newline as delimits."

    But it doesn't work that way. So you don't get that. But if it did, it would. But it doesn't. So you don't.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:43 AM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM