Unofficial Programming Thread

Post » Thu Sep 23, 2010 10:56 pm

So what exactly are the differences between C++ and C# and why should one use a particular one?

I've done Visual Basic stuff, but I want to start something in Cwhatever because I think it would be fun. No idea which to try, though. They both seem confusing as [censored].
User avatar
Ian White
 
Posts: 3476
Joined: Thu Jul 19, 2007 8:08 pm

Post » Thu Sep 23, 2010 10:02 pm

So what exactly are the differences between C++ and C# and why should one use a particular one?

C++ is a very low level oldish language, with a not terribly great design due to backwards compatibility. It is statically compiled and it makes it very easy to shoot yourself in the foot. For example, you have to manage the programs memory as there is no garbage collection. There is nothing stopping you writing beyond the end of an array etc.

C# is a fairly modern language, basically a cross between Java and C++. It has all the requirements of a modern language including things like garbage collection.

why should one use a particular one?

What do you want to do? C# is a nicer language though.
User avatar
Silencio
 
Posts: 3442
Joined: Sun Mar 18, 2007 11:30 pm

Post » Thu Sep 23, 2010 4:48 pm

Yea, C# is probably closer to VB, if you've done that, in certain ways, but it still looks closer to C++ with certain "limitations". Can I be any more vague? C++ is missing a whole lot of the modern programming language, garbage collection being one. As far as I know, VB has garbage collection too.

C# is a little bit more logical as a programming language compared to Visual Basic that seems flawed in a few ways and is forcing you to make code that has C/C++/C# coders thinking "this can't be right/is this really necessary". Like, trying to find how many elements there are in an array, finding if an array has no elements, trying to clear an array to zero length. Solution was to write your own ArrayLength functions with exception handling, because the UBound and LBound functions on arrays throw exceptions if the array is not initialized. To clear an array you can either have an uninitialized array that you assign to your work array or just accept the anomalies. You'll have an array that has length 1, not zero. All that stuff should be easier in C# and in C++ too. (rant off)
User avatar
Mandi Norton
 
Posts: 3451
Joined: Tue Jan 30, 2007 2:43 pm

Post » Thu Sep 23, 2010 12:58 pm

Okay, so I have a question about doing something in Java. How would I go about having it so that after my program has asked for input a certain number of times (let's say 5), it stops looping and ends itself? For background this is a basic program to solve simultaneous equations and I've been having it loop using while(true). I assume it's to do with changing true to something else, but if someone could point me in the right direction without telling me exactly what to do it'd be awesome (just because I'm still learning and I don't want everything on a silver platter).
User avatar
no_excuse
 
Posts: 3380
Joined: Sun Jul 16, 2006 3:56 am

Post » Fri Sep 24, 2010 12:36 am

Okay, so I have a question about doing something in Java. How would I go about having it so that after my program has asked for input a certain number of times (let's say 5), it stops looping and ends itself? For background this is a basic program to solve simultaneous equations and I've been having it loop using while(true). I assume it's to do with changing true to something else, but if someone could point me in the right direction without telling me exactly what to do it'd be awesome (just because I'm still learning and I don't want everything on a silver platter).


Try keeping a count of how many times you've had input.
User avatar
Nicole Elocin
 
Posts: 3390
Joined: Sun Apr 15, 2007 9:12 am

Post » Fri Sep 24, 2010 12:04 am

Okay, so I have a question about doing something in Java. How would I go about having it so that after my program has asked for input a certain number of times (let's say 5), it stops looping and ends itself? For background this is a basic program to solve simultaneous equations and I've been having it loop using while(true). I assume it's to do with changing true to something else, but if someone could point me in the right direction without telling me exactly what to do it'd be awesome (just because I'm still learning and I don't want everything on a silver platter).

Wouldn't it be better to have some other end condition than input 5 times? You could define some input that possibly can't be interpreted as an equation and quit the program. Like "enter Q to quit".

You could still have the while structure as is, but have a condition with break in there or where ever you read input. It would be prettier if the while condition already contained the exit condition though, I dunno.
User avatar
Cayal
 
Posts: 3398
Joined: Tue Jan 30, 2007 6:24 pm

Post » Thu Sep 23, 2010 8:49 am

Wouldn't it be better to have some other end condition than input 5 times? You could define some input that possibly can't be interpreted as an equation and quit the program. Like "enter Q to quit".

You could still have the while structure as is, but have a condition with break in there or where ever you read input. It would be prettier if the while condition already contained the exit condition though, I dunno.

Yeah, I thought about that but this is just an assignment I want to get done now as opposed to during the week (when we are supposed to be taught how to do it) because I'll be busy and whatnot then. I'll look into trying it that way too once I get it working the other way, though.
User avatar
Mr. Allen
 
Posts: 3327
Joined: Fri Oct 05, 2007 8:36 am

Post » Thu Sep 23, 2010 5:52 pm

Wouldn't it be better to have some other end condition than input 5 times? You could define some input that possibly can't be interpreted as an equation and quit the program. Like "enter Q to quit".

You could still have the while structure as is, but have a condition with break in there or where ever you read input. It would be prettier if the while condition already contained the exit condition though, I dunno.

The last console application I wrote used a while loop to check if the input was null and if so then end the loop. Seemed to work fine, probably wan't the prettiest since you do get asked the input question then just hit enter to leave the input blank.
User avatar
Michelle Smith
 
Posts: 3417
Joined: Wed Nov 15, 2006 2:03 am

Post » Thu Sep 23, 2010 3:19 pm

I have a math question. I've been making a game, and have no clue on how to place items of a UI in certain areas.

So, in a plane where (0,0) is the top left, how would you determine the top, bottom, left & right. Reaching the middle is apparently XY/2.
User avatar
Sophie Miller
 
Posts: 3300
Joined: Sun Jun 18, 2006 12:35 am

Post » Thu Sep 23, 2010 11:31 pm

I have a math question. I've been making a game, and have no clue on how to place items of a UI in certain areas.

So, in a plane where (0,0) is the top left, how would you determine the top, bottom, left & right. Reaching the middle is apparently XY/2.


Downwards is positive Y, to the right is positive X. The actual positioning of things really depends on your resolution.
User avatar
patricia kris
 
Posts: 3348
Joined: Tue Feb 13, 2007 5:49 am

Post » Thu Sep 23, 2010 11:15 pm

Anyone got ideas on where to start with c#?

I started up a console app and made a little program that basically was a loop with a prompt. You entered a command and if it was good, it basically went into a different prompt (I made one where you enter 2 numbers and it multiplies them. Of course, if you entered a bad number or letters, it would crash - because I have no idea how to do error stuff.

Basically, I was using stuff like console.write, console.writeline, if ((x < 1) || (x > 10)), while (blah blah), convert.toint32, etc, consoleforegroundcolor (or something like that), etc, etc. Really basic stuff.

So where's a good place to start? I just want to learn to program a bit. I'm not trying to make big games or programs, I just want to learn how to program so I can put it all together later on and try to make something.

I also don't know whether to do console or window app. And are there code differences between them?
User avatar
Javaun Thompson
 
Posts: 3397
Joined: Fri Sep 21, 2007 10:28 am

Post » Thu Sep 23, 2010 8:12 pm

Anyone got ideas on where to start with c#?

I started up a console app and made a little program that basically was a loop with a prompt. You entered a command and if it was good, it basically went into a different prompt (I made one where you enter 2 numbers and it multiplies them. Of course, if you entered a bad number or letters, it would crash - because I have no idea how to do error stuff.

Basically, I was using stuff like console.write, console.writeline, if ((x < 1) || (x > 10)), while (blah blah), convert.toint32, etc, consoleforegroundcolor (or something like that), etc, etc. Really basic stuff.

So where's a good place to start? I just want to learn to program a bit. I'm not trying to make big games or programs, I just want to learn how to program so I can put it all together later on and try to make something.

I also don't know whether to do console or window app. And are there code differences between them?

The code difference is with a Windows form or WPF application you will have controls and their properties to deal with.

I used Visual C# step by Step 2010 after using an older book by Wrox and find it to be helpful. I don't like how it just has you add code and doesn't give projects to work on from the bottom up like the Wrox books but even so its more up to date.

As for program ideas, just find something that is useful to you and work on that. You can get lots of information from MSDN and Intellisense in Visual Studio but using Google never hurts. If you get stuck feel free to post code that generates errors and we will try and help you get it working. I am still learning myself so helping with problems is a good learning tool.
User avatar
Felix Walde
 
Posts: 3333
Joined: Sat Jun 02, 2007 4:50 pm

Post » Thu Sep 23, 2010 11:46 am

Downwards is positive Y, to the right is positive X. The actual positioning of things really depends on your resolution.

Thanks, and that's another problem, resolution is a variable.
User avatar
Amy Masters
 
Posts: 3277
Joined: Thu Jun 22, 2006 10:26 am

Post » Thu Sep 23, 2010 8:22 pm

Thanks, and that's another problem, resolution is a variable.

It depends on the platform you are working, but at least visual studio, or is it mfc-related, way of doing things is that you first design the UI as small as possible and then define multipliers on width, height, pos x, pos y. If 1, it means that while scaling the window, the components move or scale in direct proportion, and with 0, none at all. When you vary the multipliers you can have components scaling and moving logically with the window size.

If you are talking about designing something for full screen mode, and the only variable is resolution, you could have the positions stored as proportional values between 0 and 1 instead of pixels. But then you have a possible problem with aspect ratio. Maybe it's close enough to assume that pixels are still squares even if aspect ratio varied, and only change positions, not width or height.
User avatar
Kevin Jay
 
Posts: 3431
Joined: Sun Apr 29, 2007 4:29 am

Post » Thu Sep 23, 2010 10:03 am

Alright, well I took a shower this morning and it dawned on me. Here we go:

Since 0,0 is the top left of the screen, we get to work with X being the line going across the top and Y going straight down the left side.

So, to turn this into some code.

// Bear with the shortcutsX = GetResolution().x;Y = GetResolution().y;Size = 30; // text size, picture size, etc.topMiddle = 2dVector(X/2,0-Size);bottomMiddle = 2dVector(X/2,Y-Size);leftMiddle = 2dVector(0,Y/2-Size);rightMiddle = 2dVector(X,Y/2-Size);Middle = 2dVector(X/2,Y/2-Size);


Size is what confused me at first, since the object - in this case Size 30 - will be drawn from where Y begins downward (essentially adding to Y), you'd need to subtract the Size. In other cases it may subtract from Y, which would mean either you'd do nothing to Y or just add to Y.

Hopefully this helps anyone else who tried doing this as well.

Edit: Forgot to capitalize X & Y. :P
User avatar
Josee Leach
 
Posts: 3371
Joined: Tue Dec 26, 2006 10:50 pm

Post » Thu Sep 23, 2010 7:58 pm

So, if X = 320, Y = 240, you get
topMiddle = 2dVector(160, -30)
bottomMiddle = 2dVector(160, 210)
leftMiddle = 2dVector(0, 90)
rightMiddle = 2dVector(320, 90)
Middle = 2dVector(160, 90)

Say.. what are these vectors for?
User avatar
Lexy Corpsey
 
Posts: 3448
Joined: Tue Jun 27, 2006 12:39 am

Post » Thu Sep 23, 2010 2:01 pm

It's for a UI overlay for a game. I went a bit crazy with the -Size :P

For putting characters on the right, you'd need to measure their size in pixels(or if you use another measurement).

Here's how it looks...

// Text placementvoid writeOnTop(string text, string font, int size){	DrawText(vector2(GetScreenSize().x/2,0),text, font, size, ARGB(200,255,255,255));}void writeOnBottom(string text, string font, int size){	DrawText(vector2(GetScreenSize().x/2,GetScreenSize().y-size),text, font, size, ARGB(200,255,255,255));}void writeOnLeft(string text, string font, int size){	DrawText(vector2(0,GetScreenSize().y/2),text, font, size, ARGB(200,255,255,255));}void writeOnRight(string text, string font, int size){// text.size() might not be right, but let's use it anyway	DrawText(vector2(GetScreenSize().x-text.size(),GetScreenSize().y/2),text, font, size, ARGB(200,255,255,255));}

User avatar
Steven Hardman
 
Posts: 3323
Joined: Sun Jun 10, 2007 5:12 pm

Post » Thu Sep 23, 2010 2:43 pm

Anyone have a good tutorial for C# functions?

I'm trying to make a console program for fun and instead of just doing everything in the main part, I want to make functions to call.

I googled it and found some things, but some of them just don't work. For instance, when I copy and paste one that uses 'public' as the first part I get some error [censored] about static. So I change 'public' to 'static' and it suddenly works. Makes no sense to me. I just don't understand it. I don't get what public and static even really mean - or why examples copied directly from a c# tutorial site don't work.
User avatar
Nick Tyler
 
Posts: 3437
Joined: Thu Aug 30, 2007 8:57 am

Post » Thu Sep 23, 2010 7:24 pm

At least In C++, the static keyword when applied to a function/method means something you can call without instantiating the class first. Say, you have a class for a user interface form/dialog. You might define a static method inside the class to launch the dialog in a certain way. However, you can't access any member variables inside the class unless it has been instantiated. You could call it directly something like UIDialog::RunDialog (C++). In C# you probably don't use the '::', but a dot.

You can also apply the keyword on member variables, I mean, properties. Technically, it means that you only have a single copy of whatever you defined as static. If you have a property as a static, you can instantiate many classes, but inside each of those the static property still points to the same variable. If you remove the static keyword, you'll have separate variables with no connection, and different values for each property. Note that the static is referenced through the type name, not the instance. Does this make sense?

C# Tutorial? Maybe MSDN helps.
http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx
User avatar
Sunnii Bebiieh
 
Posts: 3454
Joined: Wed Apr 11, 2007 7:57 pm

Post » Thu Sep 23, 2010 5:10 pm

That made no sense to me at all, but thanks for the attempt. I'll try the MSDN and see if it helps. Generally that site is a little too technical, though. My knowledge of programming is extremely limited, so most of it goes over my head. I'm used to VB 6 where functions are really easy to make.
User avatar
christelle047
 
Posts: 3407
Joined: Mon Apr 09, 2007 12:50 pm

Post » Thu Sep 23, 2010 10:05 pm

That made no sense to me at all, but thanks for the attempt. I'll try the MSDN and see if it helps. Generally that site is a little too technical, though. My knowledge of programming is extremely limited, so most of it goes over my head. I'm used to VB 6 where functions are really easy to make.


MSDN is quite frankly rubbish in my opinion. It makes everything much more complicated than it needs to be.
User avatar
Josh Trembly
 
Posts: 3381
Joined: Fri Nov 02, 2007 9:25 am

Post » Thu Sep 23, 2010 11:09 pm

This is obviously nothing much, but I wanted to post a little thing I did just to see if anyone could point out where I could be perhaps more efficient. I've not done any error checking stuff because I don't know how yet, but the basic idea is A and M for the commands, then entering the numbers, then the program quitting. I just want to know if there's an area anywhere that I could be more efficient, so that I can learn early and not get in a bad habit:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            String userinput = "";            int usernumber1 = 0;            int usernumber2 = 0;                        Console.Write("[A]dd or [M]ultiply? > ");            userinput = Console.ReadLine();            userinput = userinput.ToUpper();            if (userinput == "M")            {                Console.Write("Enter First Number > ");                usernumber1 = Convert.ToInt32(Console.ReadLine());                Console.Write("Enter Second Number > ");                usernumber2 = Convert.ToInt32(Console.ReadLine());                multiplynums(usernumber1, usernumber2);                            }            else if (userinput == "A")            {                Console.Write("Enter First Number > ");                usernumber1 = Convert.ToInt32(Console.ReadLine());                Console.Write("Enter Second Number > ");                usernumber2 = Convert.ToInt32(Console.ReadLine());                addnums(usernumber1, usernumber2);            }            else            {                Console.Write("\n\nInvalid Command");            }                        Console.WriteLine("\n\n\n\n\nPress Any Key To Exit...");            Console.ReadKey();                 }        static void multiplynums(int num1, int num2)        {            int num3 = (num1 * num2);            Console.Write("\n{0} * {1} = {2}", num1, num2, num3);        }        static void addnums(int num1, int num2)        {            int num3 = (num1 + num2);            Console.Write("\n{0} + {1} = {2}", num1, num2, num3);        }            }}

User avatar
Lynette Wilson
 
Posts: 3424
Joined: Fri Jul 14, 2006 4:20 pm

Post » Thu Sep 23, 2010 5:47 pm

I gave your code a quick look, since I'm in a bit of a hurry I'll make it quick.

Notice this segment.

            if (userinput == "M")            {                Console.Write("Enter First Number > ");                usernumber1 = Convert.ToInt32(Console.ReadLine());                Console.Write("Enter Second Number > ");                usernumber2 = Convert.ToInt32(Console.ReadLine());                multiplynums(usernumber1, usernumber2);                            }            else if (userinput == "A")            {                Console.Write("Enter First Number > ");                usernumber1 = Convert.ToInt32(Console.ReadLine());                Console.Write("Enter Second Number > ");                usernumber2 = Convert.ToInt32(Console.ReadLine());                addnums(usernumber1, usernumber2);            }            else            {                Console.Write("\n\nInvalid Command");            }


Both options have the same exact lines aside from the last one. A better way to write this would be:

            Console.Write("Enter First Number > ");            usernumber1 = Convert.ToInt32(Console.ReadLine());            Console.Write("Enter Second Number > ");            usernumber2 = Convert.ToInt32(Console.ReadLine());            if (userinput == "M")            {                multiplynums(usernumber1, usernumber2);            }            else if (userinput == "A")            {                addnums(usernumber1, usernumber2);            }            else            {                Console.Write("\n\nInvalid Command");            }


It's true that it creates a new problem for you (Haven't noticed this at first), but you should never fall back to bad coding habits because the right solution is not straightforward.

Aside from that, note that if you are planning to add additional options for this calculator, a switch-case would serve you better. You might also want to document your code, with each segment beginning with a comment that explains what the next lines do and how. In this case, it's not that important because it's a small program. Regardless, it's a very good habit to develop and you'll discover it will save you hours of frustration when you pick up a lengthy piece of code you haven't touched for some time.
User avatar
Manuela Ribeiro Pereira
 
Posts: 3423
Joined: Fri Nov 17, 2006 10:24 pm

Post » Thu Sep 23, 2010 11:44 am

This is obviously nothing much, but I wanted to post a little thing I did just to see if anyone could point out where I could be perhaps more efficient. I've not done any error checking stuff because I don't know how yet, but the basic idea is A and M for the commands, then entering the numbers, then the program quitting. I just want to know if there's an area anywhere that I could be more efficient, so that I can learn early and not get in a bad habit:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            String userinput = "";            int usernumber1 = 0;            int usernumber2 = 0;                        Console.Write("[A]dd or [M]ultiply? > ");            userinput = Console.ReadLine();            userinput = userinput.ToUpper();            if (userinput == "M")            {                Console.Write("Enter First Number > ");                usernumber1 = Convert.ToInt32(Console.ReadLine());                Console.Write("Enter Second Number > ");                usernumber2 = Convert.ToInt32(Console.ReadLine());                multiplynums(usernumber1, usernumber2);                            }            else if (userinput == "A")            {                Console.Write("Enter First Number > ");                usernumber1 = Convert.ToInt32(Console.ReadLine());                Console.Write("Enter Second Number > ");                usernumber2 = Convert.ToInt32(Console.ReadLine());                addnums(usernumber1, usernumber2);            }            else            {                Console.Write("\n\nInvalid Command");            }                        Console.WriteLine("\n\n\n\n\nPress Any Key To Exit...");            Console.ReadKey();                 }        static void multiplynums(int num1, int num2)        {            int num3 = (num1 * num2);            Console.Write("\n{0} * {1} = {2}", num1, num2, num3);        }        static void addnums(int num1, int num2)        {            int num3 = (num1 + num2);            Console.Write("\n{0} + {1} = {2}", num1, num2, num3);        }            }}


I will take a look later tonight and try and see what I can do with it.

I am thinking of trying to make a classifieds page in ASP.NET later too if anyone wants to help with it. Since I won't have a database I will use an XML file which should be fine, but if anyone knows any easy to follow SQL tutorials I will give it a shot, just not sure if the server will support it.
User avatar
Penny Flame
 
Posts: 3336
Joined: Sat Aug 12, 2006 1:53 am

Post » Thu Sep 23, 2010 7:05 pm

use a 'switch' for userinput instead of a bunch of if/elses :wink_smile:
User avatar
Darren Chandler
 
Posts: 3361
Joined: Mon Jun 25, 2007 9:03 am

PreviousNext

Return to Othor Games