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

Voters
19. You may not vote on this poll
Page 17 of 22 FirstFirst ... 71516171819 ... LastLast
Results 161 to 170 of 217

Thread: [Beginner] Programming Challenge: 2

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

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by LaRoza View Post
    I didn't read the code so I can't say where. But what it is the maximum size of the data type for the numerical input?
    i made them both longs so for extra exception handling (in case someone one is a couple billion years old)

  2. #162
    Join Date
    Jul 2008
    Beans
    1,491

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by badperson View Post
    Would be interested in seeing code that addressed the unicode issue after the challenge is over...
    Well, Java uses an implementation of Unicode by default...

  3. #163
    Join Date
    Oct 2007
    Beans
    410

    Re: [Beginner] Programming Challenge: 2

    been meaning to read this for awhile...
    bp

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

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by jimi_hendrix View Post
    i made them both longs so for extra exception handling (in case someone one is a couple billion years old)
    Well, I entered a very long string of numbers and it threw an uncaught exception.

    The lesson is to not assume the user will enter near valid data. The article I recommended people read says users will blissfully enter their name, lunch or nothing at any prompt.

    Thread going to be closed soon.

  5. #165
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 2

    ok i believe i fixed the problem...the user will go in an infinite loop until he either loads a profile or creates a new one (wait do i need to fix this so that he can exit while he is making his profile???)

    if so dont start judging for a bit...

  6. #166
    Join Date
    Jul 2008
    Beans
    1,706

    Re: [Beginner] Programming Challenge: 2

    ok here is my revised code with what has hopefully no bugs

    PHP Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;

    namespace 
    User_Input
    {
        class 
    Program
        
    {
            static 
    void Main(string[] args)
            {
                
    //variables we will use
                
    bool keepGoing true//this keeps our while loop going
                
    string name ""//this is your name
                
    long age 0//your age
                
    long nextAge age 1//the age you will be next year
                
    long uID 0//your id
                
    long nextUID uID 1//the next person's id
                
    bool loaded false;//we will use this at the start to make sure the user creates or loads a profile
                
    bool newProfile false;

                
    //asks if the user wants to load or make a new profile
                
    Console.WriteLine("Type load to load a profile or \n anything else to create a new one. \n You can also type quit at any time \n during the creation process to quit");
                
    string answer Console.ReadLine();//set answer to the input by user

                
    if (answer.ToLower() == "load")//here is where we load the profile
                
    {                              //we use the ToLower method to make sure that if the user types Load, lOAD, LOAD or any combination of capitals and lower case will be valid

                    
    while (loaded == false)//makes sure the person wants to load a file                   
                    
    {
                        
    Console.WriteLine("what is the name of the profile you want to load?");

                        try
    //this will attempt to seach for a user file
                        
    {
                            
    string quit Console.ReadLine();
                            if (
    quit.ToLower() == "quit")
                            {
                                return;
                            }
                            
    StreamReader sr = new StreamReader(quit);//creates a Stream Reader to read the file
                            
    name sr.ReadLine();//reads the first line of the file and sets name to it
                            
    age Convert.ToInt32(sr.ReadLine());//reads the second line of the file sets age to it
                            
    uID Convert.ToInt32(sr.ReadLine());//reads the third line of the file and sets uID to it
                            
    Console.WriteLine();//these lines print out the user info
                            
    Console.WriteLine(name);
                            
    Console.WriteLine(age);
                            
    Console.WriteLine(uID);
                            
    sr.Close();//closes the stream reader without this the program will still be accessing the stream even after its close
                            
    loaded true;
                        }
                        catch (
    FileNotFoundException)//error handling
                        
    {
                            
    string reload;
                            
    Console.WriteLine("ERROR NO PROFILE WITH THAT NAME EXISTS!!!");
                            
    Console.WriteLine("do you want to load again? Type no to create a new profile or quit to exit \n (anything else will ask you to load again)");
                            
    reload Console.ReadLine();
                            if (
    reload.ToLower() == "quit")
                            {
                                return;
                            }
                            if (
    reload.ToLower() == "no")
                            {
                                
    newProfile true;
                                
    loaded true;
                            }
                        }
                    }
                }
                else
                {
                    
    newProfile true;
                }

                

                 while(
    newProfile == true)//if the user chooses to load they create a new profile here
                 
    {
                    
    string quit;
                    
    Console.WriteLine("What is your name?");//this is all basic question and answer with the user
                    
    name Console.ReadLine();
                    if (
    name.ToLower() == "quit")
                    {
                        return;
                    }
                    
    Console.WriteLine("What is your age?");
                    try
                    {
                        
    quit Console.ReadLine();
                        if (
    quit.ToLower() == "quit")
                        {
                            return;
                        }
                        
    age Convert.ToInt32(quit);
                        
    nextAge age 1;
                    }
                    catch (
    FormatException)
                    {
                        
    Console.WriteLine("you have entered invalid input please finish filling out the data and then re enter it");
                    }
                    catch
                    {
                        
    Console.WriteLine("i dont think your that old...");
                    }
                    
    Console.WriteLine("What is your user id?");
                    try
                    {
                        
    quit Console.ReadLine();
                        if (
    quit.ToLower() == "quit")
                        {
                            return;
                        }
                        
    uID Convert.ToInt32(quit);
                        
    nextUID uID 1;
                    }
                    catch (
    FormatException)
                    {
                        
    Console.WriteLine("you have entered invalid input please finish filling out the data and then re enter it");
                    }
                    catch
                    {
                        
    Console.WriteLine("i dont think your user ID is that long...");
                    }
                    if (
    age <= uID <= 0)//error handling
                    
    {
                        
    Console.WriteLine("you have entered invalid data for your age and/or id");
                    }

                    else
    //everything is created correctly so we can exit
                    
    {
                        
    newProfile false;
                    }
                 }            

                while (
    keepGoing == true)//the guts of the program start here
                
    {
                    
    Console.WriteLine("Your name is: {0}"name);//display user data
                    
    Console.WriteLine("Your age is {0}, next year you will be {1} years old"agenextAge);
                    
    Console.WriteLine("Your user ID is {0}, next year you will be {1}"uIDnextUID);
                    
    Console.WriteLine();
                    
    Console.WriteLine();

                    
    int choice Menu();//display menu and get user choice
                    
                    
    switch (choice)//act depending on choice
                    
    {
                        case 
    0://display info again
                            
    Console.WriteLine("Your name is: {0}"name);
                            
    Console.WriteLine("Your age is {0}, next year you will be {1} years old"agenextAge);
                            
    Console.WriteLine("Your user ID is {0}, next year you will be {1}"uIDnextUID);
                            
    Console.WriteLine();
                            
    Console.WriteLine();
                            break;
                        case 
    1://change name
                            
    try
                            {
                                
    name nameChange();
                            }
                            catch
                            {
                                
    Console.WriteLine("Error please try again");
                            }
                            break;
                        case 
    2://change age
                            
    try
                            {
                                
    age ageChange();
                                
    nextAge age 1;
                            }
                            catch
                            {
                                
    Console.WriteLine("Error please try again");
                            }
                            break;
                        case 
    3://change id
                            
    try
                            {
                                
    uID idChange();
                                
    uID nextUID 1;
                            }
                            catch
                            {
                                
    Console.WriteLine("Error please try again");
                            }
                            break;
                        case 
    4://save
                            
    try
                            {
                            
    StreamWriter sw = new StreamWriter(name);//here we save the user data to a file with the same name as the user
                            
    sw.WriteLine(name);
                            
    sw.WriteLine(Convert.ToString(age));
                            
    sw.WriteLine(Convert.ToString(uID));
                            
    sw.Close();
                            
    Console.WriteLine("saved");
                            }
                            catch
                            {
                                
    Console.WriteLine("Error please try again");
                            }
                            break;
                        case 
    5://load
                            
    Console.WriteLine("what is the name of the profile you want to load?");

                            try
                            {
                                
    StreamReader sr = new StreamReader(Console.ReadLine());//here we load the data line by line
                                
    name sr.ReadLine();
                                
    age Convert.ToInt32(sr.ReadLine());
                                
    uID Convert.ToInt32(sr.ReadLine());
                                
    Console.WriteLine(name);
                                
    Console.WriteLine(age);
                                
    Console.WriteLine(uID);
                                
    nextAge age 1;
                                
    nextUID uID 1;
                                
    sr.Close();
                            }
                            catch (
    FileNotFoundException)//error handling for no files or invalid data
                            
    {
                                
    Console.WriteLine("ERROR NO PROFILE WITH THAT NAME EXISTS!!!");
                            }
                            
                            break;
                        case 
    6://exit
                            
    return;
                            
                    }
                }
            }

            static 
    int Menu()// method to display the menu and get the user choice
            
    {
                
    int input 0;
               
                
    Console.WriteLine("What would you like to do now?");
                
    Console.WriteLine("[0]: Enter 0 to print your info to screen again");
                
    Console.WriteLine("[1]: Enter 1 to change your name");
                
    Console.WriteLine("[2]: Enter 2 to change your age");
                
    Console.WriteLine("[3]: Enter 3 to change your id");
                
    Console.WriteLine("[4]: Enter 4 to save your profile");
                
    Console.WriteLine("[5]: Enter 5 to load a profile");
                
    Console.WriteLine("[6]: Enter 6 to exit");
                try
                {
                    
    input Convert.ToInt32(Console.ReadLine());
                }
                catch(
    FormatException)//error handling
                
    {
                    
    Console.WriteLine("INVALID INPUT");
                }
                return 
    input;
            }

            static 
    string nameChange()//method to change the user name
            
    {
                
    string newName;
                
    Console.WriteLine("What would you like to change it to?:");
                
    newName Console.ReadLine();
                return 
    newName;
            }

            static 
    int ageChange()//method to change the age
            
    {
                
    int nAge;
                
    Console.WriteLine("What would you like to change it to?:");
                
    nAge Convert.ToInt32(Console.ReadLine());
                return 
    nAge;
            }
             
            static 
    int idChange()//method to change the id
            
    {
                
    int nID;
                
    Console.WriteLine("What would you like to change it to?:");
                
    nID Convert.ToInt32(Console.ReadLine());
                return 
    nID;
            }
        }



  7. #167
    Join Date
    Apr 2008
    Beans
    246

    Re: [Beginner] Programming Challenge: 2

    I fixed it according to feedback:

    PHP Code:
    #!/usr/bin/env python
    # userinfo.py
    #User Info Gatherer by LinkMaster03
    import sys
    name
    ,age,id,valid,namestr,agestr,idstr="","","","Please enter a valid","username.\n(Only letters,\
     numbers, spaces, and underscores. Must have, and start and end with, atleast one letter or\
     number.)"
    ,"age. (integer 5-130)","forum ID. Click on your Ubuntuforums profile, and the number\
     after the = is your forum ID. (integer 1-999999)" 
    #define error messages
    print "\nType exit or quit at any prompt to leave!"
    def getName():
        try:    
    #if an exception is thrown in here
            
    name=raw_input("What is your username? "#get username
            
    if len(name.strip())<or len(name.strip())>20
                print 
    "Invalid username length, please enter a name with 1-20 characters."
                
    name=""
            
    elif name=="exit" or name=="quit"#if user wants to quit the program
                
    sys.exit()
            else:
                if 
    name.strip()!=name:
                    print 
    "Spaces have been stripped from beginning and end of name."
                    
    name=name.strip()
            return 
    name
        except 
    (TypeErrorValueErrorAttributeError):     #handle it here
            
    print valid,namestr
            name
    =""
    def getAge():
        try:    
    #if an exception is thrown in here
            
    age=raw_input("Thanks! Can I have your age? ")
            
    age=age.strip(" _")
            
    age=age.replace(" ",""#remove all spaces
            
    if age=="exit" or age=="quit"sys.exit()
            
    age=int(age)
            if 
    age<or age>130:
                print 
    valid,agestr
                age
    =""
            
    else: 
                
    pass
            
    return age
        except 
    (TypeErrorValueErrorAttributeError):     #handle it here
            
    print valid,agestr
            age
    =""
    def getID():
        try:
            
    id=raw_input("Great! Will you tell me your forum ID? ")
            
    id=id.strip(" _")
            
    id=id.replace(" ","")
            if 
    id=="exit" or id=="quit"sys.exit()
            
    id=int(id)
            if 
    id<or id>999999:
                print 
    valid,idstr
                id
    =""
            
    else: 
                
    pass
            
    return id
        except 
    (TypeErrorValueErrorAttributeError):
            print 
    valid,idstr
            id
    =""
    while not name:
        
    name=getName()
    while 
    not age:
        
    age=getAge()
    while 
    not id:
        
    id=getID()
    print 
    "Your username is %s. You are %s years old, and you will be turning %s on your next birthday!\
     Your Ubuntuforums user ID is %s, and the ID of the person who registered after you is %s. Your profile is at\
     http://ubuntuforums.org/member.php?u=%s." 
    % (name,age,age+1,id,id+1,id
    It will now accept all valid usernames, and notify the user if their name has been stripped of spaces.
    Last edited by linkmaster03; August 9th, 2008 at 12:42 AM.

  8. #168
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [Beginner] Programming Challenge: 2

    Closed for grading. Might take a day to finish.

    Stay tuned...

  9. #169
    Join Date
    Apr 2007
    Beans
    14,781

    Re: [Beginner] Programming Challenge: 2

    Opened for an hour. Going to grocery store.

  10. #170
    Join Date
    Aug 2007
    Location
    Manchester, UK
    Beans
    10,280
    Distro
    Ubuntu

    Re: [Beginner] Programming Challenge: 2

    Quote Originally Posted by LaRoza View Post
    Opened for an hour. Going to grocery store.
    I've still not done it I best get cracking!

Page 17 of 22 FirstFirst ... 71516171819 ... 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
  •