collapse collapse

* Who's Online

  • Dot Guests: 64
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - spum789

Pages: 1 [2] 3
31
OpenGL / 4: Rotation
« on: August 11, 2009, 01:31:46 am »
Hey, here we're going to take  a wire cube and rotate it by 1 degree and keep doing it. In this program we need double buffering so that we can see a smoother rotation of the cube. to do this we glutInitDisplayMode(GLUT_DOUBLE);. and we swap the buffers to keep updating with glutSwapBuffers();. Have fun. for excersies try rotatin lines or triangles or some other weird shape that you come up with.


Code: [Select]
#include <gl.h>
#include <glut.h>

GLfloat angle = 0.0; //set the angle of rotation

void cube (void) {
    glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
    glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(2);// draws a wire cube at the center of the screen

}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity(); 
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    cube();
    glutSwapBuffers();//we swap the buffers here so we can see wat we drew.
    angle ++;// here we increment the angle by 1 so the cube keeps rotating
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE); //set up the double buffering so that cube runs smoother
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display);//we use this to keep updating the screen otherwise we'd see no change
    glutReshapeFunc (reshape);
    glutMainLoop ();
    return 0;
}

32
Introductions / spum789 V2 intro
« on: August 11, 2009, 12:59:46 am »
hey, my name is Justin A.K.A. spum789. plz if you will ignore my first intoduction as it is not a reflection of myself. Anyways, I'm currently in the process of college and stuff. I love to play xbox and any other console game. I love to be challenged, and I'm very competitive. one of my passions is to live in Japan for 2 or so years to learn and understand their culture 1st hand. I watch anime to help with my Japanese. My fav food is Pizza. If i had to choose one female celebrity to date it would be Megan Fox. And i'm also into UFC. I'm a pretty decent programmer, my main languages are C++ and Blitz. My favorite scripting language is Lua. And i'm pretty decent at SDL and OpenGL. Well thx for taking the time to read my intro ^_^. c ya.

33
General Talk / Re: Michael Jackson
« on: August 11, 2009, 12:49:25 am »
R.I.P. Michael Jackson

34
OpenGL / 3: Adding color/details on shapes
« on: August 11, 2009, 12:12:10 am »
ok so in the previous lesson we drew a square. Well hoepfully you guessed that OpenGL can draw more than just quadrilaterals. It can draw everything from single points to lines, to triangles, polygons, the whole nine yards. and to do this we just do - glBegin(GL_TRIANGLES) or glBegin(GL_LINES) or glBegin(GL_POLYGONS), etc. so now that that's out of the way, lets talk about adding color to our boring shapes. basically we're going to take the code from the previous lesson and add exactly one line to it!!!! wow thats simple right.


here is the revised section
Code: [Select]
void square() {
glColor3f(255.0,0.0,0.0);// Here we change the drawing color so everything we draw from here is going to
        //red until we specify otherwise!! the parameters are (Red, Green, Blue, Alpha), if you don't already know
        // alpha is use for transparencies.

glBegin(GL_QUADS);

glVertex3f(-1.0,-1.0,0.0);
glVertex3f(-1.0,1.0,0.0);
glVertex3f(1.0,1.0,0.0);
glVertex3f(1.0,-1.0,0.0);

glEnd();
}

35
General Talk / Re: Post your pic v1 thread
« on: August 11, 2009, 12:03:15 am »
i kno. i just have to find wat works for me.

36
General Talk / Re: Post your pic v1 thread
« on: August 10, 2009, 11:35:42 pm »
thats cuz in the first pic i had my shoulders like pushed together like someone trying to flex his pecs. NOTE: that doesn't work with a shirt on lol.

37
General Talk / Re: post your pic v1 thread
« on: August 10, 2009, 11:02:23 pm »
hey here is my second pic, sorry about cappy quality.

38
OpenGL / Re: 1: Getting Started/downloading libraries and headers
« on: August 10, 2009, 05:40:46 pm »
yes, OpenGL lets you program on any platform. You just have to define if compiling on mac, linux, or windows(default). i'll post a tutorial on how to do that. its simple. ^_^

39
General Talk / Re: Post your pic v1 thread
« on: August 10, 2009, 02:45:12 pm »
i'm going to post a new pic as soon as i can get a new cam.

40
OpenGL / 2: Creating Shapes
« on: August 10, 2009, 02:40:21 pm »
Ok people, you probably already guessed that detailed comments were in the code from last lesson ^_^. I forgot sorry, fail. But anyways we'll just move right along with our next lesson, Creating shpaes!!!

Bassically i'll just use comments to explain functions. ^_^. But i will not recomment parts that we've already covered.

Code: [Select]
#include <gl.h>
#include <glut.h>

//our square drawing funciton
void square (void) {
    glBegin(GL_QUADS); //this call tells OpenGL that we want to begin drawing quadrilaterals.
    //note that every call to glBegin() will have a corresponding call to glEnd().
   
    //here we list the vertexes of our square/quad. Also because we're drawing quads OpenGL will start a new quad
    // every 5th vertex. anyways, i'll break down the syntax. glVertex3f - glVertex means we want to list a vertex.
    // the "3" means we're going to draw in 3D and the "f" means that our values will be of type floats. If you don't
    // already know the values are the coordinates on our x,y,z graph. If you don't know about x,y,z graphs then
    // just google it and it should be real simple to understand
    glVertex3f(-1, -1, 0); //first corner at -0.5, -0.5
    glVertex3f(-1, 1, 0.0); //second corner at -0.5, 0.5
    glVertex3f(1, 1, 0); //third corner at 0.5, 0.5
    glVertex3f(1, -1, 0.0); //fourth corner at 0.5, -0.5
    glEnd(); //here is our corresponding glEnd(); It tells OpenGL that we're done drawing quads
}

//here again is our display function or our "scene".
void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();// don't worry about wat this does yet.
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // set our camera posistion
    square(); // make a call to our square function
    glFlush(); //and flush/push it to the screen
}

//this new function is activated when the user re-sizes the window.
void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);// this is how we're currently viewing the window
    glMatrixMode (GL_PROJECTION); // this lets us play with the "camera"
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); // This tells OpenGL to not draw anything to close or
    // to far
    glMatrixMode (GL_MODELVIEW);// switch back to drawing mode
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);//callls our draw function
    glutReshapeFunc (reshape);// calls our reshape function when the window changes
    glutMainLoop ();
    return 0;
}

41
OpenGL / 1: Getting Started/downloading libraries and headers
« on: August 10, 2009, 02:11:28 pm »
Hey, this is going to be the start of very simple OpenGL tutorials. This first lesson we'll download the files needed to run compile and debug OpenGL. For these lessons we'll use the C++ programming language. To download the Files i'll include a attach link so you can download them. I'm going to assume that you know how to set-up OpenGL on your compiler. Once you've downloaded the files then lets run a test program to see if it worked. I'm going to give the code without any explanation, until the next lesson. Create a new console project and copy and paste this code into your compiler

Code: [Select]
#include <gl.h> //include the gl header file
#include <glut.h> //include the glut header file

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0); //clear the color of the window
    glClear (GL_COLOR_BUFFER_BIT); //Clear teh Color Buffer (more buffers later on)
    glLoadIdentity();  //load the Identity Matrix
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); //set the view
    glFlush(); //flush it all to the screen
}

int main (int argc, char **argv) {
    glutInit (&argc, argv); //initialize the program.
    glutInitDisplayMode (GLUT_SINGLE); //set up a basic display buffer (only singular for now)
    glutInitWindowSize (500, 500); //set whe width and height of the window
    glutInitWindowPosition (100, 100); //set the position of the window
    glutCreateWindow ("A basic OpenGL Window"); //set the caption for the window
    glutDisplayFunc (display); //call the display function to draw our world
    glutMainLoop (); //initialize the OpenGL loop cycle
    return 0;
}

42
General Talk / Re: Post your pic v1 thread
« on: August 07, 2009, 03:06:27 am »
hey all, here is my pic. Enjoy!

43
Challenges / Re: Zone 1 Challenge
« on: August 01, 2009, 05:04:40 pm »
Hey here is my submission for Zone challenge 1.
- EDITED -
Private Message it to me, don't post answers where others can see it.

44
C++ / Re: [7] Defines and Macros
« on: April 19, 2009, 10:45:16 am »
what is the difference between a define and a macro. for example you say in the code comments " here is our first define!!!!" and then " here is our first macro!" i would like 2 know is there difference between the two. becuase the define looks like this
Code: [Select]
#define Message "the answer is"and the macro looks like this.
Code: [Select]
#define Add (a,b) (a + b)so whats the difference

45
BlitzPlus / Re: [1]: The Basics
« on: February 17, 2009, 07:42:05 pm »
FULL VERSION OF BLITZPLUS IDE UP FOR DOWNLOAD!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! its in the blitzplus tutorial section same as this topic

46
BlitzPlus / [1]: The Basics
« on: February 16, 2009, 11:21:56 pm »
Hey everybody If your an ambitious programmer looking to make video games but don't have a full grasp on C++ then this language is perfect for you. This language(BlitzPlus) is game-specific and will get you through your first steps onto becoming a great "game devver" i'm going to attach a file that has the IDE BlitzPlus in it, i'm pretty sure it will work if not then post here and i'll fix it...... so enough of the intros lets start our first lesson "The Basics".

I'm going to list the commands we're going to be using in this lesson and along with their definition and syntax and its main use..... again if there is something that you don't understand post a reply to this topic and I will explain/fix it ASAP. so here it goes

Commands:

1). Graphics
Use: we use this command to go into a graphics mode this command is probably the most used command in BlitzPlus
                  [1]     [2]     [3]    [4]    [5]
SYNTAX: Graphics,width,height,type,mode
[1]: This is The actual command it tells the IDE that you want to go into graphics mode without it you can't have any kind of graphics
[2]: Obviously this is the width-in-pixels so if we were to put in 640 for this value then we would get a screen that is 640 pixels wide
[3]: this is the height as with the width this is the height of the screen in pixels
[4]: this is the type of screen that you want usually we want this value to be 16 a resizable window that has a name and an 'x' button
[5]: this is the mode put 1 for full screen, and 2 for an non-fullscreen 

2).SetBuffer
Use: this command tell your graphics card/screen what buffer to draw on.
                  [1]          [2]
SYNTAX: SetBuffer validbuffer()
[1]: Command to switch the current buffer to any validbuffer()
[2]: this can be any valid buffer such as the FrontBuffer() or BackBuffer(). we almost always want to use the BackBuffer() because it will prevent screen flickering

3).Global
Use: sets any valid variable weather it be of type int,string ($), or of type float (#). a global variable means that the variable can be used throughout the whole program and any function,control statement or anything really can use it, so if we had a function that took x and added it by 1 everytime it was called and then wanted to print the value after 5 times of it being added then the value of x would be 5 assuming the initial value was 0. but be carefull some variables you might want to keep local to a certain function or control statement.
                [1]       [2]
SYNTAX: Global valid-variable
[1]: the command to tell the program that the following variable is Global
[2]: a variable. defaultly the variable will be an interger (int.... a whole-number), but you can change it to be of type string( a variable used to hold text characters with this "$" ex. string1$, or of type float( a number that can have decimal places) use this sign "#" the same way you would declare a string..

4).While loop
Use: this is a control statement that only executes if the condition is met and keeps executing until the condition is either false or the condition is met
              [1]       [2]                [3]
SYNTAX: While condition..........Wend
[1]: command used to say "while this is true then do this"
[2]: this part is the condition to be tested for instance if we had a variable x of type int that equaled 5 ( x = 5 ) then we could use this statement While x < 10. so the program checks to see if x is less than 10 and if so then do what follows within the while.....wend loop
[3]: this is used to tell the program where this particular while loop ends other wise the program will not compile becuase the while loop technically doesn't end

5).Text
Use: this command is used to draw a text onto the screen using the current font(ways to load new fonts will be taught in a later lesson) and the current color(explained in the next in this lesson)
               [1] 2 3           [4]
SYNTAX: Text,x,y,text to be written/drawn
[1]: The actual command
[2]: the x coordinate location on the screen in pixels
[3]: the y coordinate location on the screen in pixels
[4]: this can be either a pre-determined text like a string variable or it can be used like this "text here". so Text,0,0,"text here"

6). Cls
Use: clears the screen ...hopefully self-explanitory SYNTAX: Cls

7). Color
Use: Changes the drawing color
               [1]  [2]   [3]   [4]
SYNTAX: Color,red,green,blue
[1]: The command
[2]: the amount of red to use between 0 for none to 255 a lot
[3]: the amount of green to use
[4]: the amount of blue to use
 
EXAMPLE Color,255,255,255 this changes the color to white. you have to figure out how this works cuz i really don't feel like explaining but if you want just red then color,255,0,0.. etc.....

8). Flip
Use: flips the current drawing buffer to see what is on the screen other wise you won't get real-time updates.

9). End
Use: ends the program

EXAMPLE CODE:
copy and paste this or type it into your text field

Code: [Select]
Global gwidth = 640
Global gheight = 480
Global string1$ = "Hello World I am a BlitzPlus program"

Graphics gwidth,gheight,16,2
SetBuffer BackBuffer()

While Not KeyHit(1)

Cls
Color 255,255,255
Text 0,0,string1$
Flip

Wend

End

and i'll post a link to download the full version of blitzplus later but for now download it through this site
http://www.blitzbasic.com/Products/blitzplus.php

47
BlitzBASIC / 1: Hello World
« on: December 17, 2008, 06:44:34 pm »
hey yall now that you have your IDE its time to start learning to code in Blitz Basic.
first off you want to tell your computer what size screen you want to deal with. to do that we use the command Graphics
for example
Graphics 640,480,16,2
look at that! no semicolon or anything aww that was so much simpilar than C++
ok so let me explain this tells the computer to make a screen 640 pixels wide and 480 pixels high. and the 16,2 tell it to to go full screen. 

now this next command is used to set all things that we put onto the screen onto the backbuffer which keeps the screen from flickering.
setbuffer backbuffer()
basically each video car has 2 buffers a front and a back. what we see is on the front but for our program we want to put everything on the backbuffer. now we'll be able to see this by using the flip command later on. just look for it ^_^..
 
now lets tell the computer what we want to do with our program
print "Hello World"
ok this says print the text Hello world onto the screen. simple right?

now we need to use the flip command in order to see what is on the backbuffer.
Flip
wow easy right

ok now we need to tell the screen to pause to see if we did the program right. to do this we use the wait key command
waitkey()

and finally we end our program
End

here is our code all together
oh and btw this language is not case sensitive but whenever u use commands it converts the certain letters to caps automatically. so if you typed graphics after u presses the space bar it would convert to Graphics.

Code: [Select]
Graphics 640,480,16,2

SetBuffer BackBuffer()

Print "Hello World"

Flip

WaitKey()

End

and there you have it just click on the rocket ship to build and run the code and you have your first program in BlitzBasic. feel free to modify the text like to say Hello Justin how was you day. but anyways good luck and i'll see you for the next tutorial on loops and we will make our first image.!!! ^_^

48
BlitzBASIC / Download BlitzBasic IDE
« on: December 17, 2008, 06:17:02 pm »
alright guys here i am with a really good treat for yall. this is the Intro to the tutorials to come. and the reason why its so good is becuz this is a game-specific programing language. its a lot easier to write simple games in and takes a lot less lines of code than C++. Even though this language is not as strong it is still great if your wanting to jump right in to game programing and design. so first off i am going to attach a couple of links. The first is going to be a BitTorrent which will allow you to download torrents and i am going to explain y. first off you need the Blitzbasic IDE and the only way to get the full version without paying is to get it through a torrent.
here is the BitTorrent link. this must be downloaded AND installed first.
[Edit]
now here is the IDE and this is what you will write and compile the programs in.
[Edit]
after bitTorrent has finished downloading that go to where it was put usually C\:downloads and then install it and your all set!!!!!

Edit - Nathan - Please no links to copyright material posted publicly. I live in the US and they are really tough on copyright law, sorry.

49
Beta Releases / Re: My version of pong
« on: December 12, 2008, 05:28:35 pm »
actually it does follow the ball. i'll post the code so you can see what i mean. i genarate a random number and then based on that number the opponent paddle will move up or down at a certain speed. and it does that every time the speed changes. and i will try to fix that one glitch i'll just figure out a way to make the ball start in the middle. and don't be dissin on my very very very first game. there are beta versions of games ^_^.

50
Beta Releases / My version of pong
« on: December 12, 2008, 12:35:06 pm »
hey everybody here is my version of pong. i was going to put this up sooner but i had some more bugs to work out and it took a while for me to figure out how to release my program. but anyways here it is and let me know what you think.

****EDIT*****
hey ya'll sorry bout the glitch i fixed it and i posted the new version up here. and the opponents paddle should be smarter as well.^_^

51
Introductions / Re: Celestialkey - Site Admin
« on: December 07, 2008, 03:16:30 am »
well ummm. what day do you turn 18 cuz i turn 18 in December as wel.

52
Introductions / Re: spum789: like the only C# user.
« on: December 07, 2008, 03:13:54 am »
acually i am not geeky. i am the coolest guy out there. i have HOT!!! girls lined up to have sex with me. so go shuve it up your ass. no offense.

53
Beta Releases / Re: extremely easy script
« on: December 06, 2008, 12:54:21 pm »
well actually in order to use system commands you have to include <windows.h> otherwise it will not compile. also getchar(); is used just to wait for the user to end the program. its the way i learned from celestial so thats the way i do it. yea cin would work but i say patato and u say potato. and no i guess it doesn't matter if you put void into the argument but it just pointless so y waste the time. and no its not wrong but we're learning C++ not C.

54
Beta Releases / Re: extremely easy script
« on: December 06, 2008, 12:16:46 pm »
first off there are just 1 or 2 things wrong with your program.
the first thing and probably the most wrong is using system("pause"); command.
well its bad to use it because it is hell on the CPU and it goes straight to the system just to pause a simple program so avoid system commands. second in order to use system commands u have in include <windows.h> or you'll get an error.
the better thing to use is getchar(); and if one doesn't work then try 2 back to back. its much safer and better.
also if you are using a main function of type in you don't have to put void in the parenthesis. its just not a good idea so don't do it. that is where you would put an argument and u rarely use arguments for a function like main.
other than that good job.

55
C++ / Re: [3] Onto While Loops!
« on: December 06, 2008, 12:11:47 pm »
the i++ is a post incrementer and it adds 1 to the variable its being applied to. and it adds 1 after the amount of the variable is checked.   

56
Introductions / spum789: like the only C# user.
« on: December 06, 2008, 11:19:05 am »
Hey ya'll wats up. Well basically my main language is C++ but i am learning C# and am posting tutorials on the site about C#. Also i am very determined at wat i do. so think of me as like the coolest person of like all time. later.

57
C++ / Re: when you type something, another thing will happen with c++
« on: December 06, 2008, 08:37:23 am »
thats funny but i don't think he is ready for that just yet. and if he wants to learn there is a tutorial on that in C++. but he needs to learn more commands and syntax b4 that can happen.

58
C++ / Re: when you type something, another thing will happen with c++
« on: December 06, 2008, 08:21:47 am »
Alright this is what you do. first you need to know very basic syntax.
here's the code with comments to explain.
Code: [Select]
#include <iostream>
#include <string> // this must be included to use strings so included it.
using namespace std;

int main()
{
//first declare your variables
string strInput;// basically declaring a varible of type string which can be used
                   // to store a string of characters
cout << "Do you have anything to say?" << endl;
cin >> strInput; // this is where we get the users input
if (strInput == "no") // this is a conditional statement so if the user inputs the word "no" then this code block is executed.
{
cout << "Ah i see" << endl;
}
else // if the user enters anything else then this code of block is executed.
{    // also if you wanted to make it where you could have other input branches besides just "no"
// then you coud use the else if statement and use the same syntax as the if statement.
// so like this.... else if (strInput == "Text here").. and there you have it.
cout << "Well thats great news" << endl;
}
getchar();// we use two getchar(); commands becuase 1 is not strong enough for some reason.
getchar();// and you can change the text around to your liking.
return 0;
}

well if you have anymore questions don't hesitate to get help from either me, celestialkey, or isaro.

59
C# / 5: All loops and control statements.
« on: December 05, 2008, 11:39:49 pm »
Hey everybody for this lesson we are going to go over all kinds of loops for C# and how and why we would use them.
so let me start by explaining each loop and how they work. then i'll give an example program.

The while loop:
syntax. note* syntax is a word that means how a statement should be coded. ex. Console.WriteLine(""); each part of that statement is syntax. and if you ever get syntax errors it is probably becuz u left our or added to much of something.
syntax for the while loop is. while (condition) then ur code block. ex
Code: [Select]
while (intVar < 10)
{
     Console.WriteLine("variable is less than 10");
}

This loop will check the condition to see if it is true. then if it its true then the program will execute the the code block that follows. and it is not garenteed that the while loop will execute at least 1 time and it is possible for it to always execute resulting in an infanite error.

The Do while loop:
syntax.
Code: [Select]

do
{   
      Console.WriteLine("your number is less than 10");
}while(intYourNumber < 10);

The difference about this loop is that it is guaranteed to execute at least once, this is because it doesn't check to see if the condition is true until the end of the code. and then it decides weather it needs to loop. this is used like if a menu has to be printed. or if u want the code to be executed and then possibly be executed even more times. its very cool.

Also u can use loops inside of loops if you need to do so. i'll show you in an example program.
Code: [Select]
using System;

class Learning
{
public static void Main()
{
string strChoice;
int intChoice;
int variable1 = 15; // just go with it for now usually i would make a program to ask for a number but i think you can figure that out on ur own.

do //start of our loop
{
Console.WriteLine("How old are you?"); // question
strChoice = Console.ReadLine();
intChoice = Int32.Parse(strChoice); // get user input and conversion from type string to type int.
while (intChoice < 18) // first while statement. execute if user input is less than 18
{
Console.WriteLine("your not of legal age yet.");
break; // used to avoid the infinate loop to see what i'm talking about remove the break; and recompile and run.
}
while (intChoice >= 18) // second while statement. execute if user input is 18 or above
{
Console.WriteLine("Your 18 or over congratz");
break;
}
}while(variable1 < 0);// this condition is checked and if it was true then it would loop to the beginning of the do loop.
Console.Read(); // and our usuall ending brackets and statements.
}
}

Well here we use a do while loop and then insert another 2 while loops just for the hell of it. so there you have it. try and modify this program to do other things. and don't take the next lesson till you fully grasp this one and all previous ones.

60
C# / 4: switch statements and loops!
« on: December 05, 2008, 08:33:25 pm »
hey everybody its time for another lesson on C# programming language.
switch statements are used for a user to select a function depending upon the topic of it.
switch statements are a more organized version of multiple else if statements. and should be used if you have 4 or more choices.
so lets get straight to the coding
open up your Learning project and use this code
Code: [Select]
using System;

    class Learning
    {
        public static void Main()
        {
            string myInput;
            int myInt;

            begin: // this is a basic loop that tells the program to go back here if the program runs across the command "goto" followed by begin: .. pretty cool huh.

            Console.Write("Please enter a number between 1 and 3: ");
            myInput = Console.ReadLine(); // this is used to convert myInput from type string to type int which is needed for the switch statement.
            myInt = Int32.Parse(myInput);// code that actually converts it. this is the easiest way to get input just go with it.

            // switch with integer type
            switch (myInt) // the switch statement evaluates what was typed because we stored it into myInt. 
            {
                case 1: // then the statement goes here if the case "1" was typed and executes this code. note* we use a colon here not a semicolon.
                    Console.WriteLine("Your number is {0}.", myInt);
                    break; // break statements are used to exit the switch statement.
                case 2:
                    Console.WriteLine("Your number is {0}.", myInt);
                    break;
                case 3:
                    Console.WriteLine("Your number is {0}.", myInt);
                    break;
                default:
                    Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
                    break;
            }

            decide: // another loop

            Console.Write("Type \"continue\" to go on or \"quit\" to stop: "); these slashes are used to ignore the quotes and write them as actuall text..
            myInput = Console.ReadLine(); //because we've already converted myInput once we don't need to convert it again.

            // switch with string type
            switch (myInput) // another switch statement that asks the user if he wants to rerun the whole program.
            {
                case "continue": // if user typed continue here then the code goto begin; would be executed and the program will go to the begin: part of the program.
                    goto begin;
                case "quit":
                    Console.WriteLine("Bye."); // here if quit is typed then the program exits because when a break; statement is used then it tells the program that the user is done with the switch statement and to not execute switch anymore. .
                    break;
                default: // default is used just like else. so if nothing is typed then the switch statement goes here and then imediatly exits.
                    Console.WriteLine("Your input {0} is incorrect.", myInput);
                    goto decide;
            }
        }// since there is not Console.Read(); here then once the programs finishes with the last switch statement then the program ends.
    }             

Pages: 1 [2] 3

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]