Lost Password?


  #1 (permalink)  
Old 07-24-2008, 06:04 PM
Siten0308 Siten0308 is offline
Programmer
 
Join Date: Jun 2008
Posts: 113
Rep Power: 2
Siten0308 is on a distinguished road
Talking using goto/if else and user input in C#

Hello,

Please bare with me as this is my first little tutorial and first time explaining it, please XAV or Jordan or whoever else if i dont make sense or mistaken something, please correct, this is also great to learn any additional info I have missed.

Code:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string inputfirst = "";
            string inputlast = "";
            string choice = "";


        begin:
           
                Console.WriteLine("Hello what is your First name?");
                inputfirst = Console.ReadLine();
                Console.WriteLine("and what is your last name?");
                inputlast = Console.ReadLine();
                Console.WriteLine("Hello " + inputfirst + inputlast);

        decide:


                Console.WriteLine("what would you like to do? continue or quit?");
                choice = Console.ReadLine();

                if (choice == "continue")
                {
                    goto begin;
                }
                if (choice == "quit")
                {
                    Console.WriteLine("Good Bye");
                    System.Threading.Thread.Sleep(3000);

                }
                else
                {
                    Console.WriteLine("please enter continue or quit");
                    goto decide;
                }
                
        }
    }
}
Ok so here is the program, the purpose of this is to ask the user for input then it shoots back out user input but also asks if you want to repeat the program without restarting the program. So first is variable and variable type, string and the strings are inputfirst = first name which asks you: Console.WriteLine("Hello what is your First name?");
inputfirst = Console.ReadLine();

The second user input is the inputlast = user last name = inputlast = Console.ReadLine();
Console.WriteLine("Hello " + inputfirst + inputlast);

but also you see another string called "choice" which happens to be between the word decide: but also you see another word at the beginning called begin: notice it does not have a ; but a : which i hope jordan or Xav or someone can explain in detail but i would say just think of the decide/begin as a bookmarks in a book to reference or go back to your place to re-read etc. so moving on after it tells your first and last name it then asks you a choice which here comes the if and else statements, if choice (<-- string declared earlier) is equal (==) to the word "continue" or someone types it in as to what they want, then perform whatever is inside {} which is but if the user types quit then do what it says in {}, the last thing is what ever is the user inputs then do this. Now the goto as i said is like book marks and it has a part to play in the if/if/else, so if this happens, the goto (self explanatory) this book mark of the book (or program) but make sure you have the : instead of ; which i was having trouble with in the beginning. The last thing is System.Threading.Thread.Sleep(3000); which is thanks to Xav for telling me that means just end the program in .... uhh... is that seconds or miliseconds? .. Well i hope this helps out

Last edited by Jordan; 07-24-2008 at 07:52 PM. Reason: Added code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-25-2008, 04:55 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is online now
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,276
Last Blog:
wxWidgets is NOT code ...
Rep Power: 36
WingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to allWingedPanther is a name known to all
Default Re: using goto/if else and user input in C#

General rule of thumb: don't use goto statements. While there's nothing "bad" about using them here, they can quickly develop into spaghetti code that is almost impossible to maintain.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-25-2008, 05:08 PM
MeTh0Dz|Reb0rn's Avatar   
MeTh0Dz|Reb0rn MeTh0Dz|Reb0rn is online now
My Posts Are Moderated
 
Join Date: Jul 2008
Posts: 15
Rep Power: 0
MeTh0Dz|Reb0rn is an unknown quantity at this point
Default Re: using goto/if else and user input in C#

Yes like Winged said, avoid gotos.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-27-2008, 12:28 PM
Siten0308 Siten0308 is offline
Programmer
 
Join Date: Jun 2008
Posts: 113
Rep Power: 2
Siten0308 is on a distinguished road
Default Re: using goto/if else and user input in C#

what would you suggest then if you would not recommend goto jumps? loops? and please explain as this is helping me learn.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-27-2008, 05:35 PM
MeTh0Dz|Reb0rn's Avatar   
MeTh0Dz|Reb0rn MeTh0Dz|Reb0rn is online now
My Posts Are Moderated
 
Join Date: Jul 2008
Posts: 15
Rep Power: 0
MeTh0Dz|Reb0rn is an unknown quantity at this point
Default Re: using goto/if else and user input in C#

Yes loops or recursive functions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-28-2008, 09:10 AM
Siten0308 Siten0308 is offline
Programmer
 
Join Date: Jun 2008
Posts: 113
Rep Power: 2
Siten0308 is on a distinguished road
Default Re: using goto/if else and user input in C#

Thanks methodz, however for the recursive functions can you explain or give an example on it for tutorial purposes?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-28-2008, 09:46 AM
MeTh0Dz|Reb0rn's Avatar   
MeTh0Dz|Reb0rn MeTh0Dz|Reb0rn is online now
My Posts Are Moderated
 
Join Date: Jul 2008
Posts: 15
Rep Power: 0
MeTh0Dz|Reb0rn is an unknown quantity at this point
Default Re: using goto/if else and user input in C#

Ok this is C++....

Code:
#include <iostream>
using namespace std;

void Recursive_Function();

int main() {
    int i = 0;
    Recursive_Function();
    return 0;
}

void Recursive_Function() {
    cout << hello << " " << i++ << endl;
    Recursive_Function();
}
Basically what this does is call Recursive_Function() and then call it again when the function reaches its end. This is exemplified by the fact that i is printed and it's value increases by one each time the function is called. This can easily be implemented in C#.


Also I typed this all in the quick reply box, so if there is an error that's why.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-28-2008, 10:33 AM
Core2quad Core2quad is offline
Newbie
 
Join Date: Jul 2008
Posts: 3
Rep Power: 0
Core2quad is on a distinguished road
Default Re: using goto/if else and user input in C#

Great chapter for beginners, thank you all for posting here....!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 12:00 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads