|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On September 09 2013 05:23 darkness wrote:I'm looking for any Java GUI book or a tutorial. I need to bring my Java GUI to university standard or even better.  So, Swing?
|
On September 10 2013 07:23 heroyi wrote: Brain fart dammit:
(if it matters I am currently trying to use C) How does one go about validating user's input from a string/char to int or vice versa?
I can do it if there is some sort of scope (choose a number between a range) but when it is obscure as choose an integer (from negative infinity to infinity) to do some calculation how do I make sure the user doesn't put in a string or a char (from my understanding C will try to convert the char into a int value if the variable is casted 'int')
tl;dr how to validate user input
edit: after more google it seems I can use 'isdigit' I was wondering if there is a different way of doing so? I am attempting to do a hw but I don't know if the prof is going to allow us to use that
What is wrong with putting some bound on it? An integer goes from -2,147,483,64 to 2,147,483,647, outside of that and you with run into problems. If you are storing the user input in some data type, validate on the bounds of that data type. I don't remember much of my C, but asking for -any- number is just asking for problems unless you put some bounds on it.
|
On September 10 2013 07:23 heroyi wrote: Brain fart dammit:
(if it matters I am currently trying to use C) How does one go about validating user's input from a string/char to int or vice versa?
I can do it if there is some sort of scope (choose a number between a range) but when it is obscure as choose an integer (from negative infinity to infinity) to do some calculation how do I make sure the user doesn't put in a string or a char (from my understanding C will try to convert the char into a int value if the variable is casted 'int')
tl;dr how to validate user input
edit: after more google it seems I can use 'isdigit' I was wondering if there is a different way of doing so? I am attempting to do a hw but I don't know if the prof is going to allow us to use that
You are wrong, C won't automatically convert a string (char array) to a number if you cast it to an int.
char *input = "1234"; int number = (int)input;
The above will usually put the memory address of the 'input' pointer into the number (though my C is rusty, might even be undefined behaviour), not the numeric value. Instead you have to use strtol(): int strtol(char *input, char **pointer_to_first_error, int number_base)
char *input = "1234"; int number = strtol(input, NULL, 10);
'number' will have the correct numeric value of the 'input' string. If there is an error in the input, you can use the second parameter to find that position:
char *input = "123X"; char *error = NULL; int number = strtol(input, &error, 10);
'number' will be 123 and 'error' will point to the X in 'input'. In most cases it's good enough to just use strtol() with an empty second parameter, which is the behaviour most languages with a weak typing use when you cast a value to a number.
You will probably still see some references to atoi() in some documentation, though it's recommended not to use it because it can lead to some nasty behaviour and some compilers flag it as deprecated. A lot of older documentation still uses it but strtol() does everything atoi() does and more in a safer way.
There are also easy ways to write your own conversion, e.g. something along the lines of the following, though you should avoid doing it manually and instead use strtol() or similar functions that are built-in.
char *input = "123"; char *current = input; int number = 0;
while (isdigit(*current)) number = (number * 10) + (*current++ - '0');
(untested code, might not actually work)
Bonus: The other way around is simple:
int number = 123; char *result = sprintf("%d", number);
|
I'm not a web developer but I've played around with some web design. I notice that whenever people post 'how do I make this layout in css' questions, no one is ever suggesting to use inline-block and absolute positioning with divs. But in my own personal projects I do this all the time. Is there some taboo against this I'm unaware of, and could one of the cool css gurus explain this to me.
|
On September 10 2013 11:00 JeanLuc wrote: I'm not a web developer but I've played around with some web design. I notice that whenever people post 'how do I make this layout in css' questions, no one is ever suggesting to use inline-block and absolute positioning with divs. But in my own personal projects I do this all the time. Is there some taboo against this I'm unaware of, and could one of the cool css gurus explain this to me.
Inline-block has serious white-space/margin issues that are okay for some select uses, but will probably piss you off more often than not. Plus they don't have any attributes that make them mandatory for any kind of design.
Absolute positioning is okay in a world where browsers are all the same, resolution is uniform and every window is always fullscreen. With that said, there are some cool tricks where you can use absolute positioning inside of a relative positioned container. Other than that...would not recommend it at all.
|
On September 10 2013 07:23 heroyi wrote: Brain fart dammit:
(if it matters I am currently trying to use C) How does one go about validating user's input from a string/char to int or vice versa?
I can do it if there is some sort of scope (choose a number between a range) but when it is obscure as choose an integer (from negative infinity to infinity) to do some calculation how do I make sure the user doesn't put in a string or a char (from my understanding C will try to convert the char into a int value if the variable is casted 'int')
tl;dr how to validate user input
edit: after more google it seems I can use 'isdigit' I was wondering if there is a different way of doing so? I am attempting to do a hw but I don't know if the prof is going to allow us to use that
Another way may be to store input as a string, and then perform checks on it if it's a char, an int, etc. It may not be the best approach, it's just what came to my mind. Note that my programming knowledge isn't vast. It's possible this is unnecessarily hard.
|
On September 10 2013 07:23 heroyi wrote: Brain fart dammit:
(if it matters I am currently trying to use C) How does one go about validating user's input from a string/char to int or vice versa?
I can do it if there is some sort of scope (choose a number between a range) but when it is obscure as choose an integer (from negative infinity to infinity) to do some calculation how do I make sure the user doesn't put in a string or a char (from my understanding C will try to convert the char into a int value if the variable is casted 'int')
tl;dr how to validate user input
edit: after more google it seems I can use 'isdigit' I was wondering if there is a different way of doing so? I am attempting to do a hw but I don't know if the prof is going to allow us to use that
Since you're doing this as homework, I'll try not to give you too many hints - but something you might be able to do in a broad sense is to read the input as a char * (string), and then test if each character is a digit by testing whether its ASCII value is between 48 and 57 (the range 0 - 9) - if it is, you can then convert the string into an integer, and if not, you can print an error message.
e: clarity edit
|
On September 10 2013 14:47 Cyx. wrote:Show nested quote +On September 10 2013 07:23 heroyi wrote: Brain fart dammit:
(if it matters I am currently trying to use C) How does one go about validating user's input from a string/char to int or vice versa?
I can do it if there is some sort of scope (choose a number between a range) but when it is obscure as choose an integer (from negative infinity to infinity) to do some calculation how do I make sure the user doesn't put in a string or a char (from my understanding C will try to convert the char into a int value if the variable is casted 'int')
tl;dr how to validate user input
edit: after more google it seems I can use 'isdigit' I was wondering if there is a different way of doing so? I am attempting to do a hw but I don't know if the prof is going to allow us to use that Since you're doing this as homework, I'll try not to give you too many hints - but something you might be able to do in a broad sense is to read the input as a char * (string), and then test if each character is a digit by testing whether its ASCII value is between 48 and 57 (the range 0 - 9) - if it is, you can then convert the string into an integer, and if not, you can print an error message. e: clarity edit
So you suggest what I also thought. Hehe. Don't also forget to use strlen() for your loop to check what is what.
|
You really shouldn't have to write your own method to convert a string to an integer and check for validity at the same time. Every language has proper functions for that since it's an extremely basic task. See strtol() mentioned above.
|
On September 10 2013 15:29 spinesheath wrote: You really shouldn't have to write your own method to convert a string to an integer and check for validity at the same time. Every language has proper functions for that since it's an extremely basic task. See strtol() mentioned above. This, and your professor if a douche if he doesn't allow the use of standard library functions unless the specific problem at hand is to implement your own. It's actually a pretty big problem today that a lot of programmers don't realize that using libraries are 99% of the time superior to reinventing the wheel. Library functions are almost always better optimized, safer and easier for other programmers to read and maintain since unlike the crap you write yourself, they are probably already comfortable with it.
|
On September 10 2013 15:55 Tobberoth wrote:Show nested quote +On September 10 2013 15:29 spinesheath wrote: You really shouldn't have to write your own method to convert a string to an integer and check for validity at the same time. Every language has proper functions for that since it's an extremely basic task. See strtol() mentioned above. This, and your professor if a douche if he doesn't allow the use of standard library functions unless the specific problem at hand is to implement your own. It's actually a pretty big problem today that a lot of programmers don't realize that using libraries are 99% of the time superior to reinventing the wheel. Library functions are almost always better optimized, safer and easier for other programmers to read and maintain since unlike the crap you write yourself, they are probably already comfortable with it. I'm pretty sure the whole point of the question was the the specific problem WAS actually to write your own strtoi() basically - and it's totally a good exercise for an intro C or C++ course, I really don't see any reason for the hate on the prof I remember having to do the same thing a couple years ago. Not to say that learning how to use libraries is a bad thing at all - it's just probably not where they're at in that course =P
|
On September 10 2013 11:00 JeanLuc wrote: I'm not a web developer but I've played around with some web design. I notice that whenever people post 'how do I make this layout in css' questions, no one is ever suggesting to use inline-block and absolute positioning with divs. But in my own personal projects I do this all the time. Is there some taboo against this I'm unaware of, and could one of the cool css gurus explain this to me.
Absolute positions is suicide for any major layout design. When I first started web development classes I was like "yeah, I'll just absolute position everything, looks great." Then you look at the page on a different monitor and realize you're f'd.
I basically (hardly) never use absolutely positing as a professional now. It causes more problems than it solves, generally speaking. You can use it for some cute tricks (bars that follow scrolls, pop-ups, things like that), but using it as a standard way to do layout will never work right.
|
On September 11 2013 06:18 Cyx. wrote:Show nested quote +On September 10 2013 15:55 Tobberoth wrote:On September 10 2013 15:29 spinesheath wrote: You really shouldn't have to write your own method to convert a string to an integer and check for validity at the same time. Every language has proper functions for that since it's an extremely basic task. See strtol() mentioned above. This, and your professor if a douche if he doesn't allow the use of standard library functions unless the specific problem at hand is to implement your own. It's actually a pretty big problem today that a lot of programmers don't realize that using libraries are 99% of the time superior to reinventing the wheel. Library functions are almost always better optimized, safer and easier for other programmers to read and maintain since unlike the crap you write yourself, they are probably already comfortable with it. I'm pretty sure the whole point of the question was the the specific problem WAS actually to write your own strtoi() basically - and it's totally a good exercise for an intro C or C++ course, I really don't see any reason for the hate on the prof  I remember having to do the same thing a couple years ago. Not to say that learning how to use libraries is a bad thing at all - it's just probably not where they're at in that course =P To me it sounded like he had a task that required some user input and that had to be validated. The main task would be performed on the data the user inserted, but for the program to be correct, validation would be necessary. I agree, a couple of low level array access/manipulation exercises are necessary for C/C++. But then it should also be clear that those are array exercises and that you normally are not supposed to write these functions yourself.
|
Got roped into doing a bit of webstuff by my boss, not sure why as it's not my specialty, but I told him you could easily show / hide sections of text so he decided that means I'm the web guy now.
On that note I have this:
<script language="JavaScript" type="text/javascript"> function blocking(nr) // for displaying or hiding parts of the page { displayNew = (document.getElementById(nr).style.display == 'none') ? 'block' : 'none'; document.getElementById(nr).style.display = displayNew; } </script>
Then I have it used as follows:
<div> Something <a href="" onclick="blocking('showHide1'); return false;">+</a> <div id="showHide1"> Write text here... </div> </div> <div> Something else <a href="" onclick="blocking('showHide2'); return false;">+</a> <div id="showHide2"> Write text here... </div> </div>
My question is straight forward enough, do I need to have showHide1 and showHide2 or can I do something to keep all the id's the same but still only act on the correct div tag. The way I have it working now is messy and I don't like it at all.
Appreciate any help
|
Hyrule19060 Posts
|
On September 11 2013 20:54 tofucake wrote: use jquery far simpler
Ok so I just do something like?
<script language="javascript" type="text/javascript"> $(document).ready(function () { $(".showHide").click(function () { $(".someID").is(':hidden') ? $(".someID").show() : $(".someID").hide(); }); }); </script> });
But what do I put as the someID to be general so that with the snippet below it would only close the appropriate div as opposed to both divs with someID? Is that possible or do I have to treat everything as an individual thing?
<div class="showHide" > Some text <div class="someID"> Write text here... </div> </div> <div class="showHide" > Sometext <div class="someID"> Write text here... </div> </div>
EDIT: turns out this will just show / hide the text of the first block, only showing its extra text but nothing happens with the second block, bah.
EDIT2: realised id was unique and I should be using class? updated either way
|
IDs must be unique Use either a class instead or just $("#yourparentid > div") to select only first-level children.
Instead of .is(':hidden') ? $("div").show() : $("div").hide(); you can just use .toggle();
|
Use a class for the selector, not an ID.
I have a form that hides/shows when you click a button. That sounds like basically what you want (maybe not a form, but that doesn't matter). I only have one button, but you should be able to make as many of these as you want.
The javascript looks like :
<script> $(document).ready(function() { $('.nav-toggle').click(function(){ //get collapse content selector var collapse_content_selector = $(this).attr('href'); //make the collapse content to be shown or hide var toggle_switch = $(this); $(collapse_content_selector).toggle(function(){ if($(this).css('display')=='none'){ //change the button label to be 'Show' toggle_switch.html('Show'); }else{ //change the button label to be 'Hide' toggle_switch.html('Hide'); } }); }); }); </script>
Button, and the div it controls, are:
<button type="button" href="#collapse1" class="nav-toggle">More Items</button> <div id="collapse1" style="display:none;"> <p>stuff here</p> </div>
|
Hyrule19060 Posts
On September 11 2013 23:18 adwodon wrote:Ok so I just do something like? <script language="javascript" type="text/javascript"> $(document).ready(function () { $(".showHide").click(function () { $(".someID").is(':hidden') ? $(".someID").show() : $(".someID").hide(); }); }); </script> }); But what do I put as the someID to be general so that with the snippet below it would only close the appropriate div as opposed to both divs with someID? Is that possible or do I have to treat everything as an individual thing? <div class="showHide" > Some text <div class="someID"> Write text here... </div> </div> <div class="showHide" > Sometext <div class="someID"> Write text here... </div> </div>
EDIT: turns out this will just show / hide the text of the first block, only showing its extra text but nothing happens with the second block, bah. EDIT2: realised id was unique and I should be using class? updated either way not even $('#showHide').click(function(){$('#someID').toggle();}); or whatever should have buttons or something. You could probably do toggle on first child instead if you want to have a bunch of .showHides
http://jsfiddle.net/yW7Yz/2/ like that
|
On September 11 2013 23:33 tofucake wrote:Show nested quote +On September 11 2013 23:18 adwodon wrote:On September 11 2013 20:54 tofucake wrote: use jquery far simpler Ok so I just do something like? <script language="javascript" type="text/javascript"> $(document).ready(function () { $(".showHide").click(function () { $(".someID").is(':hidden') ? $(".someID").show() : $(".someID").hide(); }); }); </script> }); But what do I put as the someID to be general so that with the snippet below it would only close the appropriate div as opposed to both divs with someID? Is that possible or do I have to treat everything as an individual thing? <div class="showHide" > Some text <div class="someID"> Write text here... </div> </div> <div class="showHide" > Sometext <div class="someID"> Write text here... </div> </div>
EDIT: turns out this will just show / hide the text of the first block, only showing its extra text but nothing happens with the second block, bah. EDIT2: realised id was unique and I should be using class? updated either way not even $('#showHide').click(function(){$('#someID').toggle();}); or whatever should have buttons or something. You could probably do toggle on first child instead if you want to have a bunch of .showHides http://jsfiddle.net/yW7Yz/2/ like that
Ah perfect, thanks!
|
|
|
|