View Poll Results: Who do you think is the best beginner?

Voters
19. You may not vote on this poll
Page 3 of 22 FirstFirst 1234513 ... LastLast
Results 21 to 30 of 217

Thread: [Beginner] Programming Challenge: 2

  1. #21
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by linkmaster03
    jimi: Well, there's no point for one. And since it's Visual C, the judge probably wouldn't want it any more anti-Linux with Windows GUI.
    curse you microsoft...

    EDIT: wrong quote
    Last edited by jimi_hendrix; August 6th, 2008 at 12:29 PM.

  2. #22
    Join Date
    Apr 2008
    Beans
    246

    Re: [Beginner] Programming Challenge: 2

    I think usernames should be allowed to contain numbers. Considering many usernames contain atleast one number.

  3. #23
    Join Date
    Jul 2006
    Beans
    292
    Distro
    Edubuntu 7.10 Gutsy Gibbon

    Re: [Beginner] Programming Challenge: 2

    Code:
     #include <iostream>
    #include <string>
    using namespace std;
    int main () {
    	int age;
    	
    	int ID;
    	string name;
    	cout << "put in your forum name foo!" << endl; 
    	cin >> name;
        cout << "how old are you?" << endl;
    	cin >> age;
    	while ( age > 100 || age < 1) {
    		cout << "dude stop lying!" << endl;
    		cout << "input your real age" << endl;
    	cin >> age;
    	
    		}
    	
    	cout << "input your forum I.D" << endl;
    	cin >> ID;
    	
    	
    	while ( ID < 1 || ID > 999999)
    	{
    		cout << "I think you put your I.D in wrong, please input it again?" << endl;
    		cin >> ID;
    		
    		
    	}
    		
    cout << "You are " << name << ", aged " << age << " next year you will be " << age + 1 << ", with user id " << ID << ", the next user is " << ID + 1;
    	
    	// You are LaRoza, aged 20 next year you will be 21, with user id 266234, the next user is 266235.
    	
        return 0;
    }

  4. #24
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by British0zzy View Post
    I have a quick question. When will this challenge be judged (final day for submissions?)
    When it is finished. Since you expressed a desire to take part, I would wait for you

    So for C programmers, if a user enters a name longer than 20, it should be truncated? Or should there be an error message? And should there be a error message if a user tries to input a ridiculously long name in the input field?
    I don't know if the forum has a user name limit, and I chose 20 sort of randomly. This is to prevent paranoia about extremely long buffers for C programmers. It shouldn't crash the program. Basically, a user name is a maximum of 20 characters for it to pass. How it handles it is up to you. I don't care if it truncates it or asks for a shorter name.

    If a C version is made that can handle any length, I'd be impressed

    Also, is there a max age?
    Cool contest,
    Rich
    If you want there to be.

    Quote Originally Posted by jimi_hendrix View Post
    i am oveously not allowed to use a GUI right...
    You can have one made with a GUI. It has to be free, and let me know what I need to compile it though. Packages should be in Ubuntu repos.

    Quote Originally Posted by chris200x9 View Post
    how are we supposed to know if the username is valid? or do you just mean a valid age eg. not 500?
    Not blank for a user name (no leading spaces either)

    For a valid age, it has to be a possible age. So 0 is out or negative numbers at the very least.

    Quote Originally Posted by OutOfReach View Post
    One question, a user name can't be a number right? (e.x. 1337).
    Quote Originally Posted by EDavidBurg View Post
    Are there any restrictions as to what characters are allowed in the username?
    No. As I mentioned, ascii will be used for testing, but if there are highly competitive versions (which I doubt, as it would require 10 to be equal), I will use unicode.

  5. #25
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [Beginner] Programming Challenge: 2

    @laroza, i think it would be good to update these "clarifications" to the first post and then remove those posts that ask questions, to avoid cluttering the thread. ie, only posts of solutions should remain.

  6. #26
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by ghostdog74 View Post
    @laroza, i think it would be good to update these "clarifications" to the first post and then remove those posts that ask questions, to avoid cluttering the thread. ie, only posts of solutions should remain.
    I updated my post, but I am not going to remove posts unless they go off topic.

  7. #27
    Join Date
    Apr 2006
    Beans
    579
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: [Beginner] Programming Challenge: 2

    Doing this in python seemed very straight forward, so I thought I'd try it out in bash. Fairly amusing too, since I've only ever used bash scripts to sed and grep through files and string sed, grep, find, and rename commands together..

    Originally I was going to modify the user's input with sed and compare it to the original input. The idea was to force the input to be correct with sed, and then if it wasn't I could tell by seeing if userinput was equal to modified userinput. It was a mess.

    ANYWAY, I still ended up doing it in bash, since I'd already started into it.
    I had some odd problems with this one. For instance, could someone explain to me why [ $userinput -lt 4 ] works and [ $userinput < 4 ] doesn't ?? I thought they'd act exactly the same, but when I used '<' the elif would never run, even when it should have been true.

    Anyway, I supose I still took the easy way out, since bash's variables aren't really typed, but I don't have time to mess with a language I'm not familiar with at all right now, and the only ones I've looked into are bash and python.

    Entry:
    PHP Code:
    #!/usr/bin/env bash
    # It's ugly, but it works
    # Also, -- formated to have no horizontal scroll on the forums.

    promptText(){    # I have no idea why I didn't use the prompt
            # flag read has.  This is a pretty puny function.
    echo -"Input "$currant "('quit' to exit): " 
    }        # $currant is set before each function is called.

    nameCheck(){
    promptText
    read userinput
    if [ $userinput 'quit' ]; then echo "I sure hope 'quit' isn't your username." 
        
    exit; fi
    if [ $userinput =  ];     # Makes sure that something is input
        
    then echo "    Input error, please enter your username!"
        
    echo "    Your username must contain at least 1 character." 
        
    nameCheck 2> /dev/null
    else name=$userinput
    fi
    }

    ageCheck(){
    promptText
    read userinput
    if [ $userinput 'quit' ]; then exit; fi
    if [ ! $(echo "$userinputgrep -"^[0-9]+$") ] # test for positive number
        
    then echo "    Input error."
        
    echo "    Your age must be a positive number between 3 and 120." 
        
    ageCheck 2> /dev/null
    elif 
    $userinput -lt 4 ]    # test to make sure age is > 4
        
    then echo "    Input error: I think you are more then "$userinput" years old."
        
    echo "    Your age must be a number between 3 and 120."
        
    ageCheck 2> /dev/null
    elif 
    $userinput -gt 120 ]    # test to make sure age is < 120
        
    then echo "    Input error: I think you are less then "$userinput" years old."
        
    echo "    Your age must be a number between 3 and 120."
        
    ageCheck 2> /dev/null
    else age=$userinput
    fi
    }

    forumidCheck(){
    promptText
    read userinput
    if [ $userinput 'quit' ]; then exit; fi
    if [ ! $(echo "$userinputgrep -"^[0-9]+$") ] # test for positive number
        
    then echo "    Input error. I doubt "$userinput" is your forum ID."
        
    echo "    Your forum ID must be a positive number greater then 0." 
        
    forumidCheck 2> /dev/null
    elif 
    $userinput -lt 1 ]    # test to make sure forum ID is not 0
        
    then echo "    Input error. I doubt "$userinput" is your forum ID."
        
    echo "    Your forum ID must be a positive number greater then 0."
        
    forumidCheck 2> /dev/null
    else forumid=$userinput
    fi
    }


    currant="your name"clear 
    nameCheck 2
    > /dev/null    # sets $name to entered username
    currant="your age"clear
    ageCheck 2
    > /dev/null    # sets $age to entered age ( 4-120 )
    currant="your forum ID"clear
    forumidCheck 2
    > /dev/null # sets $forumid to entered ID ( > 0 )
    # NOTE: The above functions are called with errors redirected to /dev/null
    #       They were sometimes giving unimportant errors out that should
    #       not effect the script.

    clear; echo " "    # formated the variables bold below  ||
            #                     \/
    echo -ne "Your name is \033[1m"$name"\033[0m, you are \033[1m"$age"\033[0m years old, and \
    you smell bad. \nAlso your forum ID is \033[1m"
    $forumid"\033[0m.  The next user is \
     \033[1m"
    $(($forumid+1))"\033[0m."
    echo -" \n" 
    Last edited by arsenic23; August 6th, 2008 at 06:48 AM. Reason: copy paste typo

  8. #28
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by arsenic23 View Post
    Doing this in python seemed very straight forward,

    explain to me why [ $userinput -lt 4 ] works and [ $userinput < 4 ] doesn't ?? I thought they'd act exactly the same, but when I used '<' the elif would never run, even when it should have been true.
    please see the link in my sig to learn bash. In it, you will come across this. you will understand after reading that. also you might want to implement a while loop
    Code:
    while true
    do
     #ask for input
     # if user ask for quit, then quit
     # else do something else
     ...
     ..
    done
    Last edited by ghostdog74; August 6th, 2008 at 04:59 AM.

  9. #29
    Join Date
    Apr 2006
    Beans
    579
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by ghostdog74 View Post
    please see the link in my sig to learn bash. In it, you will come across this. you will understand after reading that. also you might want to implement a while loop
    Thanks, the bash help I had been using never explained the need for "(( ))" when comparing ints. In fact it had a chart that said that -lt and < where the same thing. I had tried something like [ $((val < 9)) ] thinking it was something like that and having used $(( stuff )) to do simple math is bash before. heheh

    So is there a terribly good reason to use a while loop in this situation? I really only did it like this because I was interested in seeing if bash would get angry if I had functions call themselves. I put in incorrect input over and over again to see if it would raise a fuss, but it seemed to work alright.

  10. #30
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: [Beginner] Programming Challenge: 2

    HTML 4.01 Strict

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    
    <html>
    <head>
    <title>[Beginner] Programming Challenge: 2</title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">google.load("jquery", "1.2.6");</script>
    <script type="text/javascript">
    function as_int(x) {
        var notdigit = function(c){ return ("-0123456789".indexOf(c) == -1); }
        if(!x.length) throw "No integer to parse";
        for(var c in x) if(notdigit(x[c])) throw "Invalid character";
        return parseInt(x);
    }
    $(function() {
        $("#submit_user").click(function() {
            var errors = [];
            var username = $("#username").val();
            if(!username.length) errors.push("* No username given!");
            if(" \t".indexOf(username[0]) != -1)
                errors.push("* Username cannot begin with whitespace!");
            try {
                var age = as_int($("#age").val());
                if(age < 1) errors.push("* Age cannot be < 1");
            } catch(e) {
                errors.push("* Invalid age!");
            }
            try {
                var id = as_int($("#id").val());
                if(id < 1 || id > 999999) errors.push("* Invalid ID range!");
            } catch(e) {
                errors.push("* Invalid ID!");
            }
            if(errors.length > 0) {
                alert("The following errors occurred:\n\n" + errors.join("\n"));
            } else {
                $("#username").val("");
                $("#age").val("");
                $("#id").val("");
                var x = $("<div/>");
                x.text("You are " + username + ", aged " + age +
                       " next year you will be " + (age+1) + ", with user id " +
                       id + ", the next user is " + (id+1) + ".");
                x.hide();
                $("#output").prepend(x);
                x.show(500);
            }
        });
    });
    </script>
    </head>
    <body>
    <table style="text-align:center; border: 2px dashed black; padding:8px; margin-bottom:8px;">
        <tr><td>Username:</td><td><input type="text" id="username"></td></tr>
        <tr><td>Age:</td><td><input type="text" id="age"></td></tr>
        <tr><td>ID:</td><td><input type="text" id="id"></td></tr>
        <tr><td colspan="2"><button id="submit_user">Submit user!</button></td></tr>
    </table>
    <div id="output" style="padding:8px; border: 1px solid black; margin-bottom:8px;"></div>
    <p style="text-align:center;">
        For more programming challenges, visit
        <a href="http://www.challenge-you.com">Challenge-You!</a>
    </p>
    </body>
    </html>

Page 3 of 22 FirstFirst 1234513 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •