instead u can use sscanf. Here u go the sample code
ssharish2005Code:#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 */
instead u can use sscanf. Here u go the sample code
ssharish2005Code:#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 */
Or strtod.
If you didn't mean the non-standard itoa, then your compiler is broken.Originally Posted by manutd
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.*
Whoops I messed up the two letters, sorry.
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?
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?Originally Posted by vinds.ruls
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
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
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
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
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
Hi vart,Originally Posted by 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
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
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
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
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