Thread: Converting char to int

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    Converting char to int

    Hi all,

    I run into a problem. Please help me out.
    I read in a file contains char and int, for example, my data file is

    tomato 2 3
    potato 4 5
    ketchup 6 7

    now, after I read the file in, I need to store this data into a structure,

    struct mystruct
    {
    int ID;
    int time;
    int count;
    };

    while tomato, potato, ketchup are stored in ID;
    2,4,6 are stored in time
    3,5,7 are stored in count
    My problem is how to convert tomato, potato, and ketchup into type int of int ID. Any help would be appreciated.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You can't. You can only store 4 characters in an int. You will need to store your strings in a char array.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    sorry, I think I stated my problem wrong.
    Whenever I read in the data file, if it see tomato then it should be converted into a number and assign to ID.

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    So you want IDs associated with certain strings? For example:
    tomato = ID#1
    potato = ID#2
    ketchup = ID#3

    Then simply read the field in as a string. Compare it with the words you expect and assign the appropriate values.

    Code:
    struct mystruct a;
    char holdsyourstring[LENGTH];
    // read string in from file
    .
    .
    .
    if(strcmp(holdsyourstring, "tomato") == 0)
       a.ID = 1;
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thank you Cshot.
    I also have a question. How can I put 2,4,6 into time and 3,5,7 into count. And if I declare an array like

    struct mystruct table[3];

    and contain the data from the file read in to this array. Please help me to solve this problem. Thanks a lot!

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The easiest way to do this in your case is using the fscanf function. (Ideally you'd want to use fgets)

    i = 0;
    // while not end of file
    fscanf("%s %d %d", tempString, &table[i].time, &table[i].count);
    // find id associated with your string and store in table[i].ID
    ++i;
    // end while
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thank you Cshot. Let me try.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Boooo. Here's the easy fix:

    1) Use binary files.
    2) Use fread.
    3) Be amazed at how easy this was.

    Woah. I swear I just typed this exact same thing!

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

  9. #9
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    That works great too. But you need to write a program to generate those files. Not that user friendly if you just wanna go in and type in a few changes.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    I used

    if(strcmp(holdsyourstring, "tomato") == 0)
    a.ID = 1;

    and it gives me an error "can't conver 'int' to const char*" and type mismatch. Please tell me why I have this error. Thanks!

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by vnrabbit
    I used

    if(strcmp(holdsyourstring, "tomato") == 0)
    a.ID = 1;

    and it gives me an error "can't conver 'int' to const char*" and type mismatch. Please tell me why I have this error. Thanks!
    Because you're using the wrong variable types.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM