Thread: Get character from a word with number

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    19

    Get character from a word with number

    Heyy guys,
    i'm new here,
    im trying to fix a problem but cant find a way to do it...its only works with the first letter.

    so i have a txt file with information such as de3 dn5 dn7 dw9 ds1
    and im trying to figure out how to get the letters and then the number

    exemple:

    if de

    print this is de;
    and print the number that follow de


    thanks.

    i'll post the code that i have

    Code:
    #include <stdlib.h>
    
    int main ()
    {
        FILE * pFile;
        int c;
      
        pFile=fopen ("de3.txt","r");
        if (pFile==NULL) perror ("Error opening file");
        else
        {
    
        c = fgetc(pFile);
         while(c)
         {
             if(isdigit(c))
             {
                printf("the digit is %d", c);
             }
             if( c == 'd' && c == 'e')
                {
                    printf("its on east");
                }
                else
                if( c == 'd' && c == 'n')
                {
                    printf("its on north");
                 
                }
                }
    
        }
        fclose (pFile);
        return 0;
    }
    Attached Files Attached Files
    • File Type: c try.c (652 Bytes, 188 views)

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    You want to "get" the letters and then "get" the number, but where do you want to store them? Also you never change the value of 'C' in your while loop, so when will it ever end?

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Also, nice job formatting your code on your first post! Welcome to the forum!

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    c will never end because its will always going to have to loop to check whether its de3 or else, and i was thinking about storing it to a pointer or array but i was confuse to whether i need different array for different word.

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Quote Originally Posted by Tryhard View Post
    c will never end because its will always going to have to loop to check whether its de3 or else, and i was thinking about storing it to a pointer or array but i was confuse to whether i need different array for different word.
    I wasn't referring to 'C' ending, I was talking about your while loop. You never change the value of 'C', so if you input a non-zero value for your first iteration, your loop will run infinitely.

    I'm not quite sure what the purpose of the program you're trying to write is. Also I don't understand your question.

    http://www.wikihow.com/Ask-a-Question-Intelligently Please read through this link and then re-post your question.
    Last edited by jwroblewski44; 03-02-2013 at 09:34 PM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    if( c == 'd' && c == 'e')
    
    ...
    
    if( c == 'd' && c == 'n')
    A variable can only ever have a single value. The int variable c will never be both 'd' and 'e' (or both 'd' and 'n') at the same time. You will therefore never enter the blocks of code controlled by these statements.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capitalize first Character in word.
    By duongducthieniu in forum C Programming
    Replies: 10
    Last Post: 01-06-2013, 01:16 AM
  2. Character String to Hex Word
    By asic_designer in forum C++ Programming
    Replies: 13
    Last Post: 03-11-2011, 02:35 PM
  3. Function for removing character from a word
    By TheDeveloper in forum C Programming
    Replies: 6
    Last Post: 07-07-2009, 05:30 AM
  4. Replies: 7
    Last Post: 01-01-2008, 12:30 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 01:58 PM

Tags for this Thread