Thread: strcpy - cannot convert

  1. #16
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    instead u can use sscanf. Here u go the sample code

    Code:
    #include<stdio.h>
    
    int main()
    {
        char str[]="123";
        int num;
        
        sscanf(str,"%d", &num);
        
        printf("The num is %d\n", num);
        
        getchar();
        return 0;
    }
    /* my output
    The num is 123
    */
    ssharish2005

  2. #17
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Or strtod.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by manutd
    Please take note that atoi() is non-standard, and will not work with all compilers(Xcode for example).
    If you didn't mean the non-standard itoa, then your compiler is broken.
    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.*

  4. #19
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Whoops I messed up the two letters, sorry.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #20
    Registered User
    Join Date
    Dec 2006
    Posts
    7

    Question integer or float instead of char

    hello everyone,

    Im also new to this programming language. Please, forgive me if I say something wrong.
    Actually the person want to display the time taken between two destinations, so he has to manuplate the data .i.e. by some mathematical operation then, how one can use character datatype, one must go with integer or float etc.
    Am I wrong in thinking like this?

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by vinds.ruls
    Actually the person want to display the time taken between two destinations, so he has to manuplate the data .i.e. by some mathematical operation then, how one can use character datatype, one must go with integer or float etc.
    Am I wrong in thinking like this?
    He never explained what he wanted. He described the compilation errors. We give some advices how to fix them. If the resulting code will compile but will not do what the OP was thinking it will do - it is another problem, isn't it? There are no telepaths here?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Hey guys,

    Yeah - I am trying to get the time taken between destination and arrival but think I may have been going about it the total wrong way ?

    I was trying to get the user to enter the times and these would be stored in a string for output later in a report to screen if you user wishes. With these being stored in a string, I then wanted to calculate the difference and then get that into a seperate string for reporting later too. Does that make sense ?

    Sorry for replying back to this so late, but been away at a family get together.

    Thanks

    H_M

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should convert string to number (for example calculating number of seconds from some given moment) take a difference of two numbers and convert this number back to string representing time.

    or you do it manually or read something about time modification routines like strftime
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #24
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Hi vart,

    Thank you for your reply.

    That sounds pretty good - how would I go about doing that ? I'm not sure how I could do that.

    Any help will be greatly appreciated.

    Thanks

    H_M

  10. #25
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Doing what? converting string to int?
    calculating time based on hours, minutes, secs?
    formating the time difference back to string?

    Code:
    char hours[] = "10";
    char minutes[] ="15";
    char sec[] = "45";
    char* dummy; //is set to the point where the conversion is stopped can be use for parsing long strings
    long l_hours = strtol(hours,&dummy,10);
    long l_minutes = l_hours* 60 + strtol(minutes, &dummy,10);
    long l_secs = l_minutes*60 + strtol(sec, &dummy,10);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #26
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Quote Originally Posted by vart
    Doing what? converting string to int?
    calculating time based on hours, minutes, secs?
    formating the time difference back to string?

    Code:
    char hours[] = "10";
    char minutes[] ="15";
    char sec[] = "45";
    char* dummy; //is set to the point where the conversion is stopped can be use for parsing long strings
    long l_hours = strtol(hours,&dummy,10);
    long l_minutes = l_hours* 60 + strtol(minutes, &dummy,10);
    long l_secs = l_minutes*60 + strtol(sec, &dummy,10);
    Hi vart,

    Thanks very much for your input on this subject. Yes, I do wish to do all the above, converting, calculating and then format it back into a string. I am not using seconds though, don't need them for what I was working on - do I need to use them ?

    Also, can I use that example of code when I have the hours and minutes in a string ? I have it so the user would be prompted and would enter like following:

    Departure Hour : 15
    Departure Mins : 30
    Arrival Hour : 17
    Arrival Mins : 00

    And after these have been entered, the program should calculate that it has taken 1 hr 30 mins to deliver the load and store that in a string (even seperate ones for the hours and minutes again if need be)

    Does that make any sense !? Sorry if it's rather vague! My apologies for lateness in replying but I have been trying to work on it again tonight and now it's late and I need to get ready for bed.

    Any help will be greatly appreciated.

    Thanks

    H_M

  12. #27
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Still can't get it to work - anyone any ideas on the above ?

    Thinking of stopping this one and starting another one as I can't get this working, would hate to do that though !

    Thanks

    H_M

  13. #28
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what step exactly makes a problem?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #29
    Registered User
    Join Date
    Mar 2006
    Posts
    23
    Hi Vart,

    Many thanks for your reply.

    Well, it prompts for the data to be entered ok and I can enter it but it's after that part that I'm having trouble trying to calculate it. Does it need to save the data before it calculates it ?

    I even tried a 'do, while' type of loop (is that the correct term?), but still couldn't get it to work - that failed with far too many errors to list !

    Thanks, and sorry if I'm sounding stupid!

    H_M

  15. #30
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you have to perform following steps
    1. convert strings to numbers
    2. Calculate number of minutes from the midnight
    3. Calculate difference between two numbers
    4. convert the result back to Hours and minutes
    5. convert back the numbers to strings

    Acctually item 1 and 5 are optional because you can enter and output the numbers directly...

    So you should start programming step-by-step checking your results on every step, to see if you get what you expect.
    Only when the sep is fullfild successfully - you go to the next one.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM