Help with reading in input

This is a discussion on Help with reading in input within the C Programming forums, part of the General Programming Boards category; Hey guys im new to this forum here. Anyway i was having a bit of a problem with a program ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    Help with reading in input

    Hey guys im new to this forum here.

    Anyway i was having a bit of a problem with a program im doing. See, i read data in line-by-line (not files) using fgets, following that i use strtok to break it into tokens.

    What i want to know now however is find out if the first token matches a word i specify.
    So for instance if i type in k 40 in my command line what should i do to verify that the token is a 'k'. I am thiking of using switch cases for this but am not really sure how to as they require integers.
    Following that how do i ensure that my next token is a integer rather than a character.
    Please give me a few hints as to what i possibily can do.

    Thank you all veyr much

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    assuming you know the first token is a single char then something like this
    Code:
    switch (*charptr)
    {   
       case 'a':
       //do something
       break;
       case 'b'
       //do something
       break;
    }
    For the Number look at atoi()

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Hey thanks.
    But if i am reading it in line - by - line what should i put in my switch statement?
    switch(??)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Tokenizing a string breaks up the line by placing a NULL byte after each token so a tokenized string can be considered seperate strings.
    If you wanted to compare against a full word and not just a char you could use strcmp/ strcmpi like this
    Code:
    if (!strcmp(charptr, "word1"))
    {
       //do something
    }
    else if (!strcmp(charptr, "word2"))
    {
       //do something else
    }
    You would have to go thru each token in the input line untill you rached the end.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Actually i wanna compare the input with a character but i am not sure how to do that since i read in the input line by line.
    Thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    In the switch statement I posted above charptr would be the pointer returned from strtok.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Yes, but what if a string was typed instead of just a character(for debugging purposes) would swithc still work?

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Code:
    #define DELIM " "
     
     int main( int argc , ** argc)
    {
       char *word;
          
          
             /* get buffer of the right size */
             buf = (char *)safe_malloc((void *)buf,BUF_SIZE+1);
    
             /* read in data one line at a time */
             while((buf=fgets(buf,BUF_SIZE+1,stdin))!=NULL)
            {
                   word = strtok(buf , DELIM)
             }
    Safe_malloc is a fuction. how do i go from here?

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    sorry i think i got the code tags the wrong way round

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Yes if the first charactors in your input where a single char followed by a deliminator.
    The code you posted above only checks the first token in each line.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Yep but thats what i need to do, check the first token in each line.
    However how do i use a switch to check if that token is equal to a certain character(regardless of if the token is a string or just a character)

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    *word ,if you get a compile error then (char)*word

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    10
    Yep actually i do get a complie error of smth like caldnt compare integer or something of that sort
    anyway ill try that thanks

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    infact before the switch statement you should check that the token was a single char and not a word by looking at the length of the string pointed to by word.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Yep actually i do get a complie error of smth like caldnt compare integer or something of that sort
    Here's a tip - take the next plane out of vagueworld and start being specific!

    1. Post the code you actually tried to compile - all of it, not just some paraphrased nonsense which would never compile anyway.
    2. Post actual error messages, not the mumble-fest you wrote.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
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, 01: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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21