• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Cromat

Member
Thanks guys, I knew that question was hard to answer with the limited information given but your answers were useful.

It sounds like a big undertaking. If you needed to prioritize one mobile platform first it would have to be iOS right? I'm asking solely from a business point of view, I actually own a Nexus.
 
You hold down a "meta" key (often next to the left alt) and use keys next to "return" as arrows. This chording is also used for PgUp, PgDown, function keys and so on. It keeps the keyboard small but takes some getting used to.

Gotcha

That's pretty much it, except on the HHKB2 it's a Fn key to the right of the right shift key. Right under Return. I extend my right pinky just slightly to get to it, just as if I was using the right Shift key.

Up = [
Down = /
Left = ;
Right = '

HGxAKK8.jpg


You can just barely see the Fn key layout there on the underside of each key cap.

It definitely takes getting used to, but I'm very much used to it now. And in vim, arrow keys are somewhat redundant anyway, since you're using commands to bounce around pages, words, lines, etc. And if you need fine grain movement, you use HJKL for movement.

It took me about a week to get used to it enough that it didn't really impact productivity much. Now I barely move my wrists at all. Super fast, super comfortable.

The only thing that I occasionally still stumble on is having the tilde to the top-right, instead of to the top-left. Everything else, I have no problems with, and surprisingly, I have no issues switching from that to my Macbook chiclet keyboard when I'm on the go.

Oh okay okay. I see now. Bah, now that I see that, I wish I put in a request for that keyboard instead as opposed to the Realforce. Now that I really understand VI, I would just like to keep my hands on the keys. Maybe there is a way to do that with the Realforce. I too code on a Mac so I will keep that in mind.

Thanks guys!
 

Roubjon

Member
Hey guys, I'm learning how to program in 8086 Assembly and I'm confused about how to store inputs. Any of you have any experience with this?

In my data segment I have:

Code:
paralist	label	byte 		;parameters of input
maxLen		db	5               ;maximum length of input
actLen		db	?               ;actual length of input
dbData		db  	6 ('$')

And in my main I have

Code:
mov	ah, 0ah			;initiate typing
lea 	dx, paralist		;start typing
int 	21h			;execute typing

Once this happens, I'm really confused as to where in memory the characters that were typed in get stored.

Thanks so much for the help, if I get any, haha.
 

tuffy

Member
That's pretty much it, except on the HHKB2 it's a Fn key to the right of the right shift key. Right under Return. I extend my right pinky just slightly to get to it, just as if I was using the right Shift key.
This is something that can be swapped around with the DIP switches (SW4, I believe). I put mine on the lower left and forgot that wasn't the default.

Although it takes up hardly any desk space and there's minimal hand movement, all that key chording just wasn't for me after awhile. So I switched to tenkeyless boards like the Realforce 87U which have physical keys for stuff I use all the time - like arrows and home/end.
 

BreakyBoy

o_O @_@ O_o
Yeah, I can understand not preferring the key-chording.

I was lucky enough to get the keyboard as a gift from an old friend, so I didn't really get to sit down and think about it too long.

In the end though, I'm glad I got the HHKB2 rather than a larger keyboard, if only because it forced me to give it a real shot, which worked out well. That and it's nice that the keyboard is small enough for me to throw it in my bag when I want to take it with me.

Down the line, once I have a proper home office set up, I think I might go for another mechanical to keep permanently at home, and for that I think I might go for a Realforce 87U.
 

cyborg009

Banned
Anyone here good with ruby on rails I'm making a project and hit a road block with my search function not doing anything. It was suppose to search through a database but nothing is coming up.
 

Slavik81

Member
I'm trying to write a hangman game in C++ and I'm having some trouble converting a char variable to a string and I'm not sure why it's not working. I googled it and found this:

Code:
#include <sstream>
#include <string>
stringstream ss;
string s;
char c = 'a';
ss << c;
ss >> s;

Which I adapted into my own code, but it's still not working. It's telling me:

error C2440: 'initializing' : cannot convert from 'const char [1]' to 'char'
There is no context in which this conversion is possible.

For my code, I have the user enter in a char to the variable letterGuess, then:

Code:
	ss << letterGuess;
	ss >> temp;
Where temp is a string variable.

What could I be doing wrong, any ideas? Is there an easier way to do this? Seems like it shouldn't be giving me this much trouble...


EDIT: Lol fuck me I had the letterGuess variable declared with double quotes instead of single... don't you just hate programming sometimes?

When you're creating a string out of chars, it seems a little overkill to use a std::stringstream. Because a string is just an array of chars, it's as simple as:
Code:
char c = 'x';
std::string a(1, c);

// or
std::string b;
b.push_back(c);

This is a good reference for std::string.
 

Kansoku

Member
Anyone here have experience with Chrome Extensions? I want to ask some questions about a project I want to do, but don't know if it's possible.
 

Chris R

Member
So silly when proper techniques for doing things are discarded or skipped over because they might make things "tricky" (not really, but whatever)

Wish I could just take charge and do things the way they need to be done but that isn't my call to make :(
 
So silly when proper techniques for doing things are discarded or skipped over because they might make things "tricky" (not really, but whatever)

Wish I could just take charge and do things the way they need to be done but that isn't my call to make :(
Sounds like my current issue - I "have" to use:

Code:
i = 0;
Label: if (i < val)
{
  do.something();
  ++i;
  goto Label;
}

instead of:

Code:
i = 0;
while (i < 0)
{
  do.something();
  ++i;
}
 

Water

Member
Sounds like my current issue - I "have" to use:

Code:
i = 0;
Label: if (i < val)
{
  do.something();
  ++i;
  goto Label;
}

instead of:
...
How does that happen? If there's no very good reason, have you tried to bring it up and get the coding standard revised? It shouldn't be controvertial since the problems with gotos have been publically recognized and discussed in the field for half a century. Dijkstra's famous "Go to -statement considered harmful" was published in 1968.
 

oxrock

Gravity is a myth, the Earth SUCKS!
Sounds like my current issue - I "have" to use:

Code:
i = 0;
Label: if (i < val)
{
  do.something();
  ++i;
  goto Label;
}

instead of:

Code:
i = 0;
while (i < 0)
{
  do.something();
  ++i;
}

Kinda curious about this myself. I've seen people use recursive functions as opposed to while loops and in some languages that's not the end of the world but this doesn't make much sense to me.
 
Kinda curious about this myself. I've seen people use recursive functions as opposed to while loops and in some languages that's not the end of the world but this doesn't make much sense to me.

How does that happen? If there's no very good reason, have you tried to bring it up and get the coding standard revised? It shouldn't be controvertial since the problems with gotos have been publically recognized and discussed in the field for half a century. Dijkstra's famous "Go to -statement considered harmful" was published in 1968.

I currently try to convert C/C++ code into a very limited (educational) assembler language. So using if and goto makes it easier for me because I can use "branch on zero". Only have 16 commands in that assembler language and half of them is load/store operations makes even a simple BubbleSort algorithm rather complicated to realize. From assembler code I have a "compiler" which translates the code into 0x0000 commands the fictional CPU understands. You learn a lot about the internals but it is horrible to write code like that - being against every standard and good style on purpose is hard :)
 

CrankyJay

Banned
Kinda curious about this myself. I've seen people use recursive functions as opposed to while loops and in some languages that's not the end of the world but this doesn't make much sense to me.

Isn't recursion more processor intensive than a standard loop?
 

tokkun

Member
I currently try to convert C/C++ code into a very limited (educational) assembler language. So using if and goto makes it easier for me because I can use "branch on zero". Only have 16 commands in that assembler language and half of them is load/store operations makes even a simple BubbleSort algorithm rather complicated to realize. From assembler code I have a "compiler" which translates the code into 0x0000 commands the fictional CPU understands. You learn a lot about the internals but it is horrible to write code like that - being against every standard and good style on purpose is hard :)

I'm guessing you are talking about LC3.

What you are seeing is representative of assembly language for every ISA in widespread use for general-purpose processors. The branch instructions available in LC3 are basically the same as what exists in x86, ARM, and MIPS. There is no support for structured programming at the ISA level. The best you can get are assembler macros that allow the substitution of labels and goto instructions to be automated, but that is still what happens under the hood.
 

oxrock

Gravity is a myth, the Earth SUCKS!
Isn't recursion more processor intensive than a standard loop?

Recursion is never as efficient for the same task as a while loop but some people find it more elegant and take the "easier" code as a tradeoff for performance. Not on that wagon myself but if it's not something extensively used and/or performance isn't a concern then whatever floats people's boats I guess.
 
Isn't recursion more processor intensive than a standard loop?

That depends on the language, the compiler and whether your function is tail recursive or not. Imperative languages generally have worse support for recursive functions and you'll sometimes run into stack overflow errors when you have too many recursive function calls. In addition, calling a function consumes stack memory, which means you need to allocate memory, which takes some amount of time, which is why recursive algorithms are often slower than their loop counterparts.

Tail recursive functions can be helpful in that case. A function is tail recursive when the recursive call is the last thing the function does. The advantage of this is that the compiler can optimize that type of function and avoid creating additional stack frames. Modern compilers like GCC, MSVC and Clang will do this. The resulting assembly generally looks like a loop. Some functional programming languages can rewrite functions to use tail recursion automatically. (I believe Haskell does this, Clojure's loop/recur scheme produces loop-like code as well)
 

oxrock

Gravity is a myth, the Earth SUCKS!
That depends on the language, the compiler and whether your function is tail recursive or not. Imperative languages generally have worse support for recursive functions and you'll sometimes run into stack overflow errors when you have too many recursive function calls. Tail recursive functions can be helpful in that case. A function is tail recursive when the recursive call is the last thing the function does. The advantage of this is that the compiler can optimize that type of function and avoid creating additional stack frames (this is what causes stack overflow errors). Modern compilers like GCC, MSVC and Clang will do this. The resulting assembly generally looks like a loop. Some functional programming languages can rewrite functions to use tail recursion automatically. (I believe Haskell does this, Clojure's loop/recur scheme produces loop-like code as well)

AFAIK, recursive functions are never actually as efficient as a while loop for that purpose. It is certainly true that some languages are better suited for them as you say however. Python for example will get stack overflow errors with a recursive function that goes too deep. I'm not the most experienced of programmers but this is my understanding anyway.
 

tokkun

Member
AFAIK, recursive functions are never actually as efficient as a while loop for that purpose. It is certainly true that some languages are better suited for them as you say however. Python for example will get stack overflow errors with a recursive function that goes too deep. I'm not the most experienced of programmers but this is my understanding anyway.

As I said a long time ago in this thread, unless you are talking about algorithmic complexity, claims about performance at the instruction level need to be backed up with benchmarks, and even then they should be treated with skepticism. The performance of modern processors is just too dependent on confounding factors like cache behavior and prefetching for simple intuition to prevail.
 
Yeah, worrying about the performance of a recursive function vs a loop is barking up the wrong tree.

Worrying about running out of stack space due to excessive recusion? That's on the mark.
 

oxrock

Gravity is a myth, the Earth SUCKS!
Yeah, worrying about the performance of a recursive function vs a loop is barking up the wrong tree.

Worrying about running out of stack space due to excessive recusion? That's on the mark.

I just tried to prove the calculation time difference of a while loop and a recursion and found python's recursive limit of 994 for my version. Hardly enough to make an overly accurate comparison but I'll post what I have anyway.

Code:
import time

t1 = time.time()
i = 0
while i < 994:
    i+=1
t2= time.time()
print 'time for while loop to count to 994:  ' + str(t2-t1)    #length of time required to complete while loop to 994

t3= time.time()
def counter(count,target):
    if count != target:
        counter(count+1,target)
    return time.time()

print 'time for recursion to count to 994:  ' + str(counter(0,994) - t3)

This is the simple program I wrote to calculate the difference between a simple while loop and a recursion that serves the same purpose. The time for the recursion always changes but is minimal (0.00200009346008) while the while loop result remains constant as 0.0.
 
I just tried to prove the calculation time difference of a while loop and a recursion and found python's recursive limit of 994 for my version. Hardly enough to make an overly accurate comparison but I'll post what I have anyway.

Code:
import time

t1 = time.time()
i = 0
while i < 994:
    i+=1
t2= time.time()
print 'time for while loop to count to 994:  ' + str(t2-t1)    #length of time required to complete while loop to 994

t3= time.time()
def counter(count,target):
    if count != target:
        counter(count+1,target)
    return time.time()

print 'time for recursion to count to 994:  ' + str(counter(0,994) - t3)

This is the simple program I wrote to calculate the difference between a simple while loop and a recursion that serves the same purpose. The time for the recursion always changes but is minimal (0.00200009346008) while the while loop result remains constant as 0.0.

Worth noting: you're calling time.time() 994 times in the recursive test; it should really be moved outside the function. Not sure how much impact time.time() has in python, but at the very least you're doing an extra unneeded operation per iteration in the recursive side.
 
Worth noting: you're calling time.time() 994 times in the recursive test; it should really be moved outside the function. Not sure how much impact time.time() has in python, but at the very least you're doing an extra unneeded operation per iteration in the recursive side.
I'd guess it dominates. Time will ultimately be a system call, which is going to be a hell of a lot more costly than a piddly increment.
 

oxrock

Gravity is a myth, the Earth SUCKS!
Worth noting: you're calling time.time() 994 times in the recursive test; it should really be moved outside the function. Not sure how much impact time.time() has in python, but at the very least you're doing an extra unneeded operation per iteration in the recursive side.

You're absolutely right, very careless of me. Thanks.

updated code:

Code:
import time

t1 = time.time()
i = 0
while i < 994:
    i+=1
t2= time.time()
print 'time for while loop to count to 994:  ' + str(t2-t1)    #length of time required to complete while loop to 994

t3= time.time()
def counter(count,target):
    if count != target:
        counter(count+1,target)

nonimportant = counter(0,994)  #assigning this variable simply forces the calculations to be made before continuing and ceases to have purpose afterwards.

t4 = time.time()
print 'time for recursive function to count to 994:  ' + str(t4-t3)

this changes the recursive solution calculation to approximately 0.0019998550415 with the while loop solution remains at 0.0.

Obviously with python's recursive handling, overloading the stack comes far before calculation times even become an issue. All I was trying to show was that there certainly was a difference and this would most definitely be compounded when dealing with much larger number of iterations.
I'm making a program in my c# class, and I have to include a dispose object button, where if there is an object created in the program, once this button is pressed it deletes said object while the program is still running.

Anyone have any idea how to implement this? I know of destructors, but this seems to be something completely different that I need to do.


Not overly familiar with c# yet or your project for that matter, but i would think an easy way to handle "disposing" objects from a running program would be how I generally do it in my games. Have all the variables that are "disposable" in your main loop referenced and updated in a list/array. When the dispose button is pressed simply remove the object from the array so that it stops being drawn and referenced.
There's probably better ways, but that's what my python brain tells me.
 

Korosenai

Member
I'm making a program in my c# class, and I have to include a dispose object button, where if there is an object created in the program, once this button is pressed it deletes said object while the program is still running.

Anyone have any idea how to implement this? I know of destructors, but this seems to be something completely different that I need to do.
 
Computational photography is awesome. I found a bunch of cool videos yesterday.
https://www.youtube.com/watch?v=p5_tpq5ejFQ
https://www.youtube.com/watch?v=Oie1ZXWceqM
https://www.youtube.com/watch?v=VDNak_-_4tI

The first one is about how you can use a beamer that projects coded light patterns into a scene and a camera to take pictures from the point of view of the projector. To see the crazy mindblowing part, go to 4:19.

The second one is about interactively creating fairly complex 3D shapes from conventional photographs. It's pretty fantastic.

The third is about separating direct and global illumination parts of images. Direct illumination means light going directly from a light source (say, a lamp) to the surface. Global illumination means everything else, like subsurface scattering, light going through transparent surfaces, light going through fog and all that. This technique again uses coded light to separate those two parts and it has some pretty interesting results.
 

Minamu

Member
Unfortunately default functionality isn't great for paths, but it's not very hard to code basic spline stuff manually, and there are plenty of add-ons which also give you that functionality. I'll have to pick one myself soon. Here's two options.

http://wiki.unity3d.com/index.php/Hermite_Spline_Controller
http://itween.pixelplacement.com/index.php
I've been trying this for a while now but I can't really get my head around it :lol I'm using the second one, iTween, but a visual scripting tool plugin instead. I've set up three individual rudimentary script animations and one of them plays automatically just fine. I decided that the moving object could deal with the controller aspect and I've set up a working bool switcher:
Code:
public class TrainController : MonoBehaviour 
{
	public bool changedDirection = false;

	void Update()
	{
		if(Input.GetKeyDown("space") && changedDirection == false)
		{
			Debug.Log("success");
			changedDirection = true;
		}
		else if(Input.GetKeyDown("space") && changedDirection == true)
		{
			Debug.Log("reverted");
			changedDirection = false;
		}
	}
}
The idea is to let the player switch a bool and when the object collides with a trigger box, that box will check the bool status and that way continue playing one of the other two animations:

Code:
public class TrainTracker : MonoBehaviour 
{
	public TrainController trainController;

	void OnTriggerEnter(Collider other)
	{
		if(trainController.changedDirection == false)
		{
			//iTweenEvent.GetEvent(GameObject.FindGameObjectWithTag("trainCart"), "Forward_Crash");
			Debug.Log("false");
		}
		else if(trainController.changedDirection == true)
		{
			//*insert second animation reference here*
			Debug.Log("true");
		}
	}
}
This Debug.Log() code isn't working so I assume connecting an animation to it won't work either for some reason. I also have no idea how to reference the two separate animations or how to get them to play xD There's supposedly a Play() function but I haven't found it. The tutorials on unity3d.com use tags to find objects all the time so that's what I've been trying here as well, via the script names.

The objects in question have their scripts attached of course, and the train has a trigger box as well. When I made the public TrainController in the TrainTracker script, unity is giving me the option to link something to that slot in the editor so I placed the train prefab in it, not that it made any difference. Not sure if it's important that the train doesn't have a rigidbody at the moment.

Edit: Adding a rigidbody to the train solved the issue of the trigger noticing the train. Now i just need to play the different animations somehow.
Edit2: It looks like shit but it finally works! The animation code was right all along, just need to add ".Play();" after it...
 

Jackben

bitch I'm taking calls.
Cursory look: shouldn't that be getCircumference()?
Just getting back to say yes this was part of it, thank you for the help.

My main issue was that my templated class would not run the overloaded stream functions (istream and ostream) unless they were declared AND defined within the class. Which totally confused me. I just hate templates basically.

Thanks for everyone the helped, semester is finished tomorrow and I will be glad for it.
These are awesome!
 
You forgot FlowLayout. If you find yourself using anything other than those three layouts, your UI is probably broken.

Amateurs. GridBagLayout is the only way to go. It just takes a long ass time to get used to. Then again I've been doing Swing UI crap for six years so I am just used to it. For the love of god do yourself a favor and ignore the hell that is Swing.
 

Ya no

Member
I'm working on a C++ program that's just a simple hangman game and I'm having some trouble with guessing individual letters. Currently, I can guess the first letter of the word, but none of the others. If I guess wrong, it also does what it is supposed to do. I'll post the chunk of code that deals with this below:

Code:
		else if (WLguess == 'L')
		{
			cout << "Go ahead and guess a letter: ";
			cin >> letterGuess;
			ss.str("");
			ss.clear();


			for (int x = 0; x < numChars; x++)
			{
				if (letterGuess == correctGuess[x])
				{
					cout << "\n\nGood guess!" << endl;
					location = correctGuess.find(letterGuess, 0);
					ss << letterGuess;
					ss >> temp;
					wordLength.replace(location, 1, temp);
					correctGuess.replace(location, 1, " ");
					cout << wordLength << endl << endl;
					cout << "You have used " << counter << "/6 incorrect guesses." << endl << endl;
					break;
				}
				/*else
				{
					cout << "\n\nYou guessed wrong..." << endl << endl;
					counter++;
					DrawGallows(counter);
					cout << "You have used " << counter << "/6 incorrect guesses." << endl << endl;
					break;
				}*/
			}

			if (wordLength == tempCorrect)
			{
				cout << string(100, '\n');
				cout << "You win!" << endl << endl;
				break;
			}
		}

Another issue I just realized is that the string stream isn't updating the temp variable upon enter a second guess. Figured this out, had to reset the string stream.

With the inner else here commented out like I updated above, I'm now able to guess all the letters of the word properly. Now I just need to figure out how to incorporate the incorrect guesses...

I'm still pretty new to C++, but I believe the problem has something to do with the way I'm using the break lines? Hoping I didn't code myself into a hole here and there's a simple fix that I'm just not seeing at the moment. Just need someone to explain why it only works for the first letter of the word and not anything after. Thanks!
 

Chris R

Member
I'm making a program in my c# class, and I have to include a dispose object button, where if there is an object created in the program, once this button is pressed it deletes said object while the program is still running.

Anyone have any idea how to implement this? I know of destructors, but this seems to be something completely different that I need to do.

Does your object need to implement IDisposable? I'm guessing that is what your teacher wants? =
 
I'm guessing you are talking about LC3.

What you are seeing is representative of assembly language for every ISA in widespread use for general-purpose processors. The branch instructions available in LC3 are basically the same as what exists in x86, ARM, and MIPS. There is no support for structured programming at the ISA level. The best you can get are assembler macros that allow the substitution of labels and goto instructions to be automated, but that is still what happens under the hood.

Just looked at LC-3 - it is similar to what I do. Some university learning tool called "TOY". Really great to look and learn about the internals but painfully difficult for any bigger project. Just studying some code snippets and go from C to assembler and ASM to Verilog :)

Well that is kinda silly. It just sucks knowing that something is being done in a "wrong" manner because others are set in their ways and afraid to learn.
I think you misunderstood me - there is no standard forced upon me but I don't have much choice in converting C to assembler without making such hidious exceptions.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
What IDE's/editors do you guys prefer for C++ development?

JetBrains has one in works that I'll probably switch to....but it sadly hasn't launched yet.
 

hateradio

The Most Dangerous Yes Man
I personally like NetBeans as far as non-IntelliJ Java IDEs go. However, VisualStudio has C++ support, if you're working on Windows.
 

iapetus

Scary Euro Man
Amateurs. GridBagLayout is the only way to go. It just takes a long ass time to get used to. Then again I've been doing Swing UI crap for six years so I am just used to it. For the love of god do yourself a favor and ignore the hell that is Swing.

Throwing everything into a GridBagLayout is the amateur way to go, in my experience. It's less maintainable, and more prone to unexpected behaviour. And like I said, if you can't do your UI with the simpler components that's probably a bad UI design smell.
 
I just started with PHP and I have to say that it's really dumb to use dollar sign before every variable. I know it's based on Perl but that doesn't make it any less dumb. I'm going to have to ALT GR + 4 all over the place.
 

Water

Member
What IDE's/editors do you guys prefer for C++ development?

JetBrains has one in works that I'll probably switch to....but it sadly hasn't launched yet.

Visual Studio 2013 is hard to beat. Otherwise I'll go with Vim and YouCompleteMe plugin instead of an IDE.
 

Minamu

Member
I got a tip today about a book sale here:

http://www.packtpub.com/content/day-against-drm

But I'm not sure if they're any good and worthwhile. Anyone got experience with the game dev books primarily? I've already done games in C#/XNA, Unity, UDK and SFML after fours years of uni game dev studies and I have books on Unity and UDK as well as regular books on C++ and C# besides game design literature.
 

Water

Member
I got a tip today about a book sale here:

http://www.packtpub.com/content/day-against-drm

But I'm not sure if they're any good and worthwhile. Anyone got experience with the game dev books primarily? I've already done games in C#/XNA, Unity, UDK and SFML after fours years of uni game dev studies and I have books on Unity and UDK as well as regular books on C++ and C# besides game design literature.

Thanks for the heads up. I recommend Unity 4.x Cookbook for intermediate Unity. I just picked up an ebook copy on this sale.
 

Slavik81

Member
What IDE's/editors do you guys prefer for C++ development?

JetBrains has one in works that I'll probably switch to....but it sadly hasn't launched yet.
Qt Creator is quite good, though I've been forcing myself to really learn vi so I haven't used it recently.
 
Top Bottom