• What do you need help with? V. 3.0
    4,884 replies, posted
[QUOTE=Chris220;31255982]You can use what is usually called "fall-through", which looks like this: [cpp]switch(x) { case 0: case 1: case 2: cout << "Whatever" << endl; break; }[/cpp][/QUOTE] Thanks for that, finally made an amazingly crappy vending machine DOS simulator. This is probably my first unguided program so excuse the crappiness. [code] #include <iostream> int main() { using namespace std; int input[4] = { 25, 10, 5, 1 }; int x = 0; // x is going to be the number that modifies the bracketed number of input[x] int pocket = 100; int money = 0; Start: do { cout << "Enter a number from 1 to 4: "; cin >> x; // cin modifies x within the brackets } while (x < 0 || x > 4); int* Pointer = &input[--x]; switch(x) { case 0: case 1: case 2: case 3: if (pocket >= *Pointer) { pocket -= *Pointer; money += *Pointer; cout << "You've put " << *Pointer << " cents in the vending machine." << endl; break; } else { cout << "You don't have enough money.." << endl; break; } default: cout << "You didn't put any money in the vending machine." << endl; } if (money < 100) { cout << "There is currently " << money << " cents in the vending machine." << endl; goto Start; } else { cout << "There is currently " << money << " cents in the vending machine." << endl; cout << "You've got a soda!" << endl; } } [/code]
So i'm trying to make my player rotate to the mouse, how do I do this? I know how to get my mouse coords.
Good start man. It's good you're grasping pointers... they're quite powerful. :smile: I've never ever used goto before in C++ but apparently they're really bad practice. When you move onto functions though you probably won't need to use them.
Me again! Sorry for all the questions, I'm very new to desktop programming :( Okay, can someone point me in the right direction as to how I would get a string to duplicate depending on a user input? Basically, say the user enters the integer '5'. How can I get 5 of random.randint(1,365) in the list 'i'? [code] n = int(raw_input("Enter number of people:")) for n in range(0,n): m = ('random.randint(1,365), ') % n i=[m] i = [random.randint(1,365), random.randint(1,365), random.randint(1,365), random.randint(1,365), random.randint(1,365)] should be the same as i=[m] [/code] Currently when I'm duplicating the phrase, it makes the variable 'm' a text string rather than a code string, if you know what I mean. How do i solve this? Thanks!
[QUOTE=Chrispy_645;31256888]Good start man. It's good you're grasping pointers... they're quite powerful. :smile: I've never ever used goto before in C++ but apparently they're really bad practice. When you move onto functions though you probably won't need to use them.[/QUOTE] The structured programming theorem (or something like that) says that you can write programs entirely without gotos, so technically you never need them However, there is nothing inherently and unacceptably wrong with them. There are arguments for and against, with various people supporting both sides (eg linus tor-cant spell thinks they can be useful). They arnt conceptually difficult to understand, nor is there anything spectacular you need to do to use them. The major complaint is that they reduce the ease of reading code, so if you find a situation in which it makes everything much tidier, then go for it if you think its a good idea. As a beginner, you might forget about memory management and the like, so it might be an idea to avoid them. Though at the same time, you learn from your mistakes In the end: do whatever :v:
[QUOTE=Icedshot;31257128]The structured programming theorem (or something like that) says that you can write programs entirely without gotos, so technically you never need them[/QUOTE] You're totally right on that part, I've never needed to use gotos. I've avoided using them simply because I don't want spaghetti code. :smile:
I'll probably avoid gotos once I get into the more in-depth concepts of C++ try and eliminate them from my coding habits. I've only been looking into programming at all for about 3 days and right now I'm just trying to gain a good grasp on concepts and ideas before worrying too much about what commands I use or don't use. How important is it would you say to be able to remember how to do basic things off the top of your head? I know that knowing how to declare variables and operands is vital so I pretty much memorized the ones I thought important, but how important would you say it is to know how to do a simple countdown or remember control statement declarations?
[QUOTE=RiceWarrior;31257503]I'll probably avoid gotos once I get into the more in-depth concepts of C++ try and eliminate them from my coding habits. I've only been looking into programming at all for about 3 days and right now I'm just trying to gain a good grasp on concepts and ideas before worrying too much about what commands I use or don't use. How important is it would you say to be able to remember how to do basic things off the top of your head? I know that knowing how to declare variables and operands is vital so I pretty much memorized the ones I thought important, but how important would you say it is to know how to do a simple countdown or remember control statement declarations?[/QUOTE] I found this was a massive issue for me at first, i kept having to look up all the arguments to functions (i still constantly forget fprintf >.>), but after a while you just remember everything because you do it so regularly. So essentially, youll forget it consistently now, but youll find after a while that you can just remember it. I rarely ever use the online references now One thing i did find helpful though, and still do occasionally, is to write an 'example program' consisting of everything you need to know so you can use it as a handy reference
[QUOTE=Staneh;31256883]So i'm trying to make my player rotate to the mouse, how do I do this? I know how to get my mouse coords.[/QUOTE] Make a right triangle from your player to your mouse then just use trig to find the angle.
[QUOTE=RiceWarrior;31257503]I'll probably avoid gotos once I get into the more in-depth concepts of C++ try and eliminate them from my coding habits. I've only been looking into programming at all for about 3 days and right now I'm just trying to gain a good grasp on concepts and ideas before worrying too much about what commands I use or don't use. How important is it would you say to be able to remember how to do basic things off the top of your head? I know that knowing how to declare variables and operands is vital so I pretty much memorized the ones I thought important, but how important would you say it is to know how to do a simple countdown or remember control statement declarations?[/QUOTE] Eg this was my first reference c++ program i wrote when i moved from c to c++ [url]http://pastebin.com/tyR9nqcB[/url] Edit: Dammit, slow on the automerge
[QUOTE=Icedshot;31257711]Eg this was my first reference c++ program i wrote when i moved from c to c++ [url]http://pastebin.com/tyR9nqcB[/url] Edit: Dammit, slow on the automerge[/QUOTE] Ooo, thanks I'll definitely look at this again when I can actually understand it haha. Bookmarked for later.
[QUOTE=RiceWarrior;31257764]Ooo, thanks I'll definitely look at this again when I can actually understand it haha. Bookmarked for later.[/QUOTE] All the variable names and the like are awful and completely unhelpful there :v: but thats literally just the c+p of it Took me a while to realise why line 117 even compiled heh
I kept having to refer to websites for remembering parameters to functions... because Visual Studio gave me shit like this: [img]http://dl.dropbox.com/u/12453703/STLmindfuck.png[/img] After I got Visual Assist X though the parameter names are much simpler and understandable...
I have a basic password program in C++, and I want the bit of the code that asks for the password and waits for the input to repeat if they got the password wrong. How would I go about doing that?
Someone correct me if I'm wrong. If your code is still the same as on the previous page, I believe this should work. [code] else if( ( input != password) ) { cout << "password incorrect. please re-enter the password." << endl; } [/code] Within this else if control statement, stick something like this in [code] else if( ( input != password) ) { cout << "password incorrect. please re-enter the password." << endl; goto Here; } [/code] I know I might get patronized for using goto, but it seems like the solution here since you don't (I believe) seem to be using functions and I still haven't learned how to do that. The goto command pretty much works by setting up a sort of name you can use to jump back to a certain point in the code if it's used. So if you used goto Here; then you can place Here: (with a colon, that's important) to jump back to before the password input again. So in the end your code should look like it originally did, except with the goto command stuck in the else if control statement and with [code] Here: cout << "Please enter password: " << endl; getline (cin, input); [/code] Here: stuck right above cout on its own line. Note that Here: should can is determined by what your goto refers to later in the code. Hope that works, and again I'm still fairly new so someone else might be able to help you out better than me.
[QUOTE=RiceWarrior;31260395]Someone correct me if I'm wrong. If your code is still the same as on the previous page, I believe this should work. [code] else if( ( input != password) ) { cout << "password incorrect. please re-enter the password." << endl; } [/code] Within this else if control statement, stick something like this in [code] else if( ( input != password) ) { cout << "password incorrect. please re-enter the password." << endl; goto Here; } [/code] I know I might get patronized for using goto, but it seems like the solution here since you don't (I believe) seem to be using functions and I still haven't learned how to do that. The goto command pretty much works by setting up a sort of name you can use to jump back to a certain point in the code if it's used. So if you used goto Here; then you can place Here: (with a colon, that's important) to jump back to before the password input again. So in the end your code should look like it originally did, except with the goto command stuck in the else if control statement and with [code] Here: cout << "Please enter password: " << endl; getline (cin, input); [/code] Here: stuck right above cout on its own line. Note that Here: should can is determined by what your goto refers to later in the code. Hope that works, and again I'm still fairly new so someone else might be able to help you out better than me.[/QUOTE] There was a post on page 32 using functions as a solution to his problem.
My bad, I figured I'd explain the goto way since he apparently didn't see or understand the function way maybe? I'm still pretty newbie at programming, but was just trying to help. Ugh, does anyone have any good resources/tutorials for better help on functions? I understand how they're used and such but I'd like more examples to see more ways they can be implemented relatively simply at a beginner level.
[QUOTE=Chrispy_645;31254557]@ Mr Smartass, I noticed that it didn't keep repeatedly asking for the password, so I created my own function, outside main(): [code]#include <iostream> #include <string> using namespace std; void Password() { string input, password = "test"; getline (cin, input); if( ( input == password ) ) { cout << "password accepted."; cin.get (); } else if( ( input != password) ) { cout << "password incorrect. please re-enter the password." << endl; Password(); } } int main() { cout << "Please enter password: " << endl; Password(); return 0; }[/code] If the password is incorrect, the function simply re-calls itself, and you are asked for the password again.[/QUOTE] Recursion in this case is as bad as goto. The proper way would be to use a do-while loop. Small snippet: [cpp] do { cout << "Enter password: "; getline (cin, input); } while (input != password); [/cpp]
[QUOTE=RiceWarrior;31260395] [code] void Password() { string input, password = "test"; getline (cin, input); if( ( input == password ) ) { cout << "password accepted."; cin.get (); } else if( ( input != password) ) { cout << "password incorrect. please re-enter the password." << endl; Password(); } } [/code] Here: stuck right above cout on its own line. Note that Here: should can is determined by what your goto refers to later in the code. Hope that works, and again I'm still fairly new so someone else might be able to help you out better than me.[/QUOTE] By calling Password(); again inside the Password function, if you call it too many times you'll overflow the stack. [b]My problem:[/b] I want to have one Visual Studios Solution, that has 2 projects inside of it: server client I want them to pull information from a "shared" folder. This code would be automatically built into both projects. How do I set this up? C++
[QUOTE=Lord Ned;31263849]By calling Password(); again inside the Password function, if you call it too many times you'll overflow the stack. [b]My problem:[/b] I want to have one Visual Studios Solution, that has 2 projects inside of it: server client I want them to pull information from a "shared" folder. This code would be automatically built into both projects. How do I set this up? C++[/QUOTE] Why make them seperate projects?
[QUOTE=sim642;31263592]Recursion in this case is as bad as goto. The proper way would be to use a do-while loop. Small snippet: [cpp] do { cout << "Enter password: "; getline (cin, input); } while (input != password); [/cpp][/QUOTE] His is more secure, it locks bruteforcers out after a few million tries.
[QUOTE=Map in a box;31263859]Why make them seperate projects?[/QUOTE] To easily separate between the two? Imagine if all of your code was just mixed up in one big project. They're not different solutions, just different projects within the solution.
A rule of thumb is to have one project per codebase. If the engine and client use the same codebase, stick them together. If you have one central codebase and extra code for the client and server, I see the reasoning there.
[QUOTE=Lord Ned;31264108]To easily separate between the two? Imagine if all of your code was just mixed up in one big project. They're not different solutions, just different projects within the solution.[/QUOTE] Make folders?
[QUOTE=Lord Ned;31263849]By calling Password(); again inside the Password function, if you call it too many times you'll overflow the stack.[/QUOTE] Good point... thanks for that. Also, suppose you called recursively called Password() 4 times... that would mean that when the 4th Password() has finished executing, the 3rd Password() will finish executing... then the 2nd Password() would finish executing... etc? (Much like a chain...?)
[QUOTE=Chrispy_645;31264465]Good point... thanks for that. Also, suppose you called recursively called Password() 4 times... that would mean that when the 4th Password() has finished executing, the 3rd Password() will finish executing... then the 2nd Password() would finish executing... etc? (Much like a chain...?)[/QUOTE] You shouldn't hit the stack limit with something like that, but for different things like sorting, use for loops n stuff, not recursion.
You're all missing the fact that the Password() function actually uses tail recursion, which does not have the stack size limitation. There is nothing wrong with using recursion in this case whatsoever.
[QUOTE=Map in a box;31264492]You shouldn't hit the stack limit with something like that, but for different things like sorting, use for loops n stuff, not recursion.[/QUOTE] You probably would hit it the limit if a guy tried typing the password like 10,000 times... (with the code I posted) But yeah, I'll keep that in mind. Edit: jA_cOp... so C++ uses tail recursion?
[QUOTE=Chrispy_645;31264566] jA_cOp... so C++ uses tail recursion?[/QUOTE] I know for sure tail call optimization is performed by GCC, MSVC and ICC. But you can pretty much assume that any other mature compiler will perform it too.
You shouldn't rely on optimizations that the compiler may or not make to avoid horrible program corrupting bugs.
Sorry, you need to Log In to post a reply to this thread.