It's UWAweek 48

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

Displaying the 7 articles in this topic
Showing 7 of 919 articles.
Currently 4 other people reading this forum.


 UWA week 36 (2nd semester, mid-semester break) ↓
SVG not supported

Login to reply

👍?
helpful
10:52am Mon 5th Sep, ANONYMOUS

As the file values are written as strings, how do we convert these values to an int for our struct? I tried using sscanf and storing the formatted values in int values but there's a mismatch error. For example, sscanf("%s", atoi(minute)); doesn't work when compiling.


SVG not supported

Login to reply

👍x1
helpful
2:00pm Mon 5th Sep, Ethan HA.

ANONYMOUS wrote:
> As the file values are written as strings, how do we convert these values to an int for our struct? I tried using sscanf and storing the formatted values in int values but there's a mismatch error. > For example, sscanf("%s", atoi(minute)); doesn't work when compiling.
This is something I think we have to figure out and code-for ourselves. I am not familiar with the usage of sscanf(), so I can't help with any formatting problems, and in fact I might be entirely misreading your problem here, however it seems to me that a big problem with this code is that not all of the file's values will be integers. Some will, but others will be strings (eg the abbreviations for the days and months "jan" "feb" etc.) and some could be characters, ie " * ". I don't know if there is a standard function that could correctly perform ALL of these conversions at once, so you'll need to write code to handle each of these cases for yourself. I'd suggest writing some functions like "month_convert()" that takes parameters of a string and returns an integer corresponding to the described month or to an invalid value. If sscanf() works how I think it does, you can maybe plug this function into sscanf() instead of atoi(), and maybe loop over the various forms the data could take. Personally, I wrote a sub-function using strcmp() and a while loop that checks if the supplied string (eg "tue") exists within a list of valid data values ("jan", "feb", "mar", ... ,"dec") and returns the index for the identical string in the list. Hopefully this will help. If you don't like these any of these methods, there are probably dozens of other ways to go about doing this, you'll just need to be creative enough to think of them. Take a step back, re-evaluate what you actually need to do, and work through it step-by-step. If you can't get a function to do what you want, write your own function instead!


SVG not supported

Login to reply

👍?
helpful
2:06pm Mon 5th Sep, ANONYMOUS

"Ethan Hardy-Atkins" <23*7*0*[email protected]*u*e*t*u*a*e*u*a*> wrote:
> ANONYMOUS wrote: > > > As the file values are written as strings, how do we convert these values to an int for our struct? I tried using sscanf and storing the formatted values in int values but there's a mismatch error. > > For example, sscanf("%s", atoi(minute)); doesn't work when compiling. > > This is something I think we have to figure out and code-for ourselves. > > I am not familiar with the usage of sscanf(), so I can't help with any formatting problems, and in fact I might be entirely misreading your problem here, however it seems to me that a big problem with this code is that not all of the file's values will be integers. Some will, but others will be strings (eg the abbreviations for the days and months "jan" "feb" etc.) and some could be characters, ie " * ". > > I don't know if there is a standard function that could correctly perform ALL of these conversions at once, so you'll need to write code to handle each of these cases for yourself. > > I'd suggest writing some functions like "month_convert()" that takes parameters of a string and returns an integer corresponding to the described month or to an invalid value. If sscanf() works how I think it does, you can maybe plug this function into sscanf() instead of atoi(), and maybe loop over the various forms the data could take. > > Personally, I wrote a sub-function using strcmp() and a while loop that checks if the supplied string (eg "tue") exists within a list of valid data values ("jan", "feb", "mar", ... ,"dec") and returns the index for the identical string in the list. > > Hopefully this will help. If you don't like these any of these methods, there are probably dozens of other ways to go about doing this, you'll just need to be creative enough to think of them. Take a step back, re-evaluate what you actually need to do, and work through it step-by-step. If you can't get a function to do what you want, write your own function instead!
Thanks so much, Ethan. I have those conversion functions and will take your advice. This is great, cheers.


SVG not supported

Login to reply

👍?
helpful
5:13am Tue 6th Sep, Christopher M.

ANONYMOUS wrote:
> For example, sscanf("%s", atoi(minute)); doesn't work when compiling.
For a start, this line doesn't make sense - atoi() is a function returning an integer, but you're then providing it 'to' an input format specifier, the %s, that is trying to match a string (actually a whitespace separated 'word') and to copy what it read into that integer value. It's good that the compiler is identifying this as an error.


SVG not supported

Login to reply

👍?
helpful
11:19am Tue 6th Sep, ANONYMOUS

To account for '*' values, I was thinking of assigning the maximum value to it, such as for minute, hour, day of month, month and day of week: 59, 23, 31, 11 and 6. But then they would actually be read as what they are, not for every value of that mode...


SVG not supported

Login to reply

👍?
helpful
11:27am Tue 6th Sep, ANONYMOUS

"Christopher McDonald" <ch*i*.*c*o*a*[email protected]*a*e*u*a*> wrote:
> ANONYMOUS wrote: > > > For example, sscanf("%s", atoi(minute)); doesn't work when compiling. > > For a start, this line doesn't make sense - atoi() is a function returning an integer, but you're then providing it 'to' an input format specifier, the %s, that is trying to match a string (actually a whitespace separated 'word') and to copy what it read into that integer value. It's good that the compiler is identifying this as an error.
Yes, this makes sense. I'll be using my own functions now which will convert each valid sequence of chars to its designated int value before storing it into an array of structs.


SVG not supported

Login to reply

👍?
helpful
11:15pm Tue 6th Sep, ANONYMOUS

> > This is something I think we have to figure out and code-for ourselves. > > > > I am not familiar with the usage of sscanf(), so I can't help with any formatting problems, and in fact I might be entirely misreading your problem here, however it seems to me that a big problem with this code is that not all of the file's values will be integers. Some will, but others will be strings (eg the abbreviations for the days and months "jan" "feb" etc.) and some could be characters, ie " * ". > > > > I don't know if there is a standard function that could correctly perform ALL of these conversions at once, so you'll need to write code to handle each of these cases for yourself. > > > > I'd suggest writing some functions like "month_convert()" that takes parameters of a string and returns an integer corresponding to the described month or to an invalid value. If sscanf() works how I think it does, you can maybe plug this function into sscanf() instead of atoi(), and maybe loop over the various forms the data could take. > > > > Personally, I wrote a sub-function using strcmp() and a while loop that checks if the supplied string (eg "tue") exists within a list of valid data values ("jan", "feb", "mar", ... ,"dec") and returns the index for the identical string in the list.
Regarding this ^, I have a function that checks if the month crontab value is an actual value, such that given a string, if the characters of this string are digits - convert to an int, but if the characters are letters, then check if the string matches a valid value within a list and return its index; this is all done within one function using two separate for-loops. But when I call them and print these values for debugging, the program doesn't run - it's just blank. This is the way I see it working as we must check if the given string characters are either digits or letters, and convert them to their respective int values.
> > Hopefully this will help. If you don't like these any of these methods, there are probably dozens of other ways to go about doing this, you'll just need to be creative enough to think of them. Take a step back, re-evaluate what you actually need to do, and work through it step-by-step. If you can't get a function to do what you want, write your own function instead!

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  1:17AM Sep 14 2022
Privacy policy