Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

using goto/if else and user input in C#

form user input variable type

  • Please log in to reply
8 replies to this topic

#1 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 24 July 2008 - 02:04 PM

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.

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

Edited by Jordan, 24 July 2008 - 03:52 PM.
Added code tags

  • 1

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 25 July 2008 - 12:55 PM

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.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 25 July 2008 - 01:08 PM

Yes like Winged said, avoid gotos.
  • 0

#4 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 27 July 2008 - 08:28 AM

what would you suggest then if you would not recommend goto jumps? loops? and please explain as this is helping me learn.
  • 0

#5 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 27 July 2008 - 01:35 PM

Yes loops or recursive functions.
  • 0

#6 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 28 July 2008 - 05:10 AM

Thanks methodz, however for the recursive functions can you explain or give an example on it for tutorial purposes?
  • 0

#7 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 28 July 2008 - 05:46 AM

Ok this is C++....

#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.
  • 0

#8 Core2quad

Core2quad

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 28 July 2008 - 06:33 AM

Great chapter for beginners, thank you all for posting here....!
  • 0

#9 ArekBulski

ArekBulski

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 30 August 2009 - 01:37 AM

Well, using loops is indeed a better idea. But since this a forum for all sorts of ideas I won't say no to this. What I am missing the most is:

1. Full solution (zipped)
2. Release build (zipped) (important, too!)
3. and a Screenshot with the console output

Please think about all those three things in your next tutorial. ;)
  • 0





Also tagged with one or more of these keywords: form, user input, variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download