Anytime Input?

Pages: 1234
For my highschool c++ class, we must create a game for an end of the year project. Ive been working on it and decided, I want a menu to be accessible from any point in the code. I want the user to be able to press a key, F1 (or any key like this) for example that will bring them to a paused menu, and then be able to return to the same point ingame. Im not sure if I should use a void menu or a completley new .cpp file. In school we havent learned how to interconnect .cpp files so they run with eachother. But im eager to learn. Also note im using "Microsoft Visual C++ 2010" to code. If anyone can help, it'd be greatly appreciated
That's going to be a problem. Is this a console game or are you using a graphics API? You can't do anything you've mentioned with the console without an API or purely using SDL, SFML, Allegro, or some other graphics API.
I'm not sure about your menu thing, but I do know you can interlink header files by #include "nameoffile.h" and putting that file in the same workspace. Via header files you can put code, just like a .cpp file, and also define functions. I will assume you can also #include for .cpp files.
ProgrammingNoob wrote:
I will assume you can also #include for .cpp files.


Don't #include .cpp files. Interface belongs in .h headers and implementation in .cpp.
it is only a console game, text based rpg style (sorta witha twist, lol) but I could include allegro, if I need it. Im really only looking to score extra points and add a little P-Zaz to the game. My friend has been using allegro in his game so I know I could always ask him for help. My original idea was that I could just add extra case to every switch I have ingame so people know to access by typing 100 or something along those lines will bring them to a menu, but this lead to nested loops and switches and just a pain to code. But currently im not using any graphics API.
closed account (4Gb4jE8b)
@ return 0, why shouldn't you include .cpp files, from a technical standpoint?

if you're using a stringstream to store each iteration that the person types, then perhaps you could make it so that whenever the person types a specific character and hits enter, it would cout a menu. No guarentees as return 0 is usually right. The major problem would be getting it to return to the same point in game, the only way i could think of doing something like that would be an annoying toggle and using goto() functions (in a way that isn't generally accepted). I like you're want for P-zaz, but again I really wouldn't advise trying something like that, even if you did succeed it would take a serious manipulation of what the console is supposed to do and is there for.

I would ask, if you don't mind, to post your code, I'd like to mess with it a bit.
return 0, why shouldn't you include .cpp files, from a technical standpoint?


Because it defeats the point of having a linker.

individual source files get compiled into object files. Object files are then linked together to form the binary.

If you #include all your source files, it's like cramming your entire program into one giant source file. This murders compile time because every file will need to be recompiled after every little change -- and the recompiling will take longer because the file is gigantic.
Currently, being a game the code is quite long (not super long, but 2547 words long, 17570 chars long with spaces, and 534 lines long -thanks word for those nums :D) and Im not sure you'd want me to post that in a reply. But I cant seem to find an attach document button on these forums
closed account (4Gb4jE8b)
pastebin.com or filedropper.com
http://pastebin.com/JYD66BiW

only being in a C++ highschool coding class, for half a school year(4-ish months), I have no problem being critisized, but I'm not familiar with alot of terms beyond what we learn from our book.
i ran your code. for some reason even after i got the crow bar out of the toolbox i couldnt open the crate. i didnt investigate further.
closed account (4Gb4jE8b)
@ disch, Thanks! i had heard not to do it in the past, but never why. But i must ask now, what if we need to call a function defined in another cpp? how are we supposed to do so?

@cstorm
As you're in a highschool class, i'm not gonna lie, i'm pretty impressed.

Well again, as it's for homework, system() isn't a end all or kill all, just don't use it for any more professional work you might do in the future.

for your save game (since i see you haven't already done it) i suggest reading up on fstreams, it would probably make saving a lot easier, and as for returning to the place after you save, the only way i can think of doing so would be goto() statements, and they really shouldn't be used as such. Actually I lied, You could use a variable referencing which function to go to. But if you're in the middle of a function, it could cause problems.

i suggest you make a function to do your "typing" effect, might make it a little more organized and easier to read.

As you're not in any one function for most of the program, doing what you want would be, as i can see, near impossible. Sorry for the bad news
Last edited on
@ acorn, I fixed the code for that. It was a simple, missing of another nested if else statement. But the code is no-where near done.

@headlessgargoyle, Thank you for the compliment about being impressed, ect. This is my first language, besides for (warcraft 3, map editor triggering, which coincedentally was based after c++, so in class i have no problem grasping concepts). The reason i use the system commands (system("cls"), and system("pause")), Pause is because my computer will not automatically pause after a cout, or cin, ect. I used the system colors because in class we learned a color coding statement, but i lost the code, so I'm relying on the system commands for now. and the cls is used becaus ei dont know how to clear the screen any other way. " i suggest you make a function to do your "typing" effect, might make it a little more organized and easier to read." I'll be honest, i dont know how to create my own functions(only being in a highschool class ) haha
headlessgargoyle

But i must ask now, what if we need to call a function defined in another cpp? how are we supposed to do so?


that's what header files are for.. you are telling the compiler what the function looks like and a place holder is set for it (well, sort of)... then it's the linkers job to connect the code to each other.

so in the include
 
void something();

tells the compiler that this code is defined somewhere, but if it's not there, then it's passed to the linker to resolve, even if it's already an object. so the cpp

1
2
3
4
void something()
{
 // .. doing something
}


get's resolved at the compiler when it's made into it's own object, but your own cpp when compiled just gets the header... when it get's to the linker, then it's resolved to the code that was created before... I think anyway...
@craniumonempty, was that an explanation on linking .cpp files to eachother? because thats also something i'd like to learn from my first post ^ ^ at the top
closed account (4Gb4jE8b)
@cstorm, but you do know how to make your own functions! you declare multiple void functions at the very beginning of your script!

But making this function would be a little more complex, as you would have to break the string into it's individual characters and put a wait() statement inbetween the cout of that specific character and the next. It's not impossible, but it isn't exactly the easiest either.

@cranium... I think I understand haha
@headlessgargoyle, i'm very bad with the terms of programming haha. The only things i know off the top of my head is libraries, variables, and statements(I think haha, I am going on the idea that its pretty much the single line of code and what it does. For ex. cout is the statement, << extractors, and "string" and itll display.) but anyways, for making a function like the wait() i have cant the same thing be done with a menu? I mean because the wait returns right to the same line of code after it does its pause.
Last edited on
closed account (4Gb4jE8b)
by making a function for wait, you would have to call that function and the argument, which would be a const string, after the function is completed it would return to the next line of code following the function, as cout does after the argument is put onscreen. The menu would have to be called by a keypress, which is kind of bad enough as is, because the console doesn't read keypresses except for the enter key (and it doesn't technically read that, cin reads that). If you were to read your keypresses at every available part the user writes into, and checked those keypresses against a const char using an if(keypress == const char){menu();} you would be able to do what you're asking. BUT you would have to use that line of code at every possible moment of entry AND you'd have to find a way to make it read the keypress, not the enter key.

hopefully that makes sense.

as for your issue about how to get rid of system commands, again don't worry about it too much. As far as I know there isn't a way of replacing cls anyway, so if you really want that, you pretty much have to use system calls.

if you want, i've been rather interested in making a function to do what you're asking myself. As long as you mention credit to me in the comments of your source code (and don't try to sell your game ;P), I'd be more than willing to type one up and share it.
it'd be appreciated, but as a school project, I'll have to check it in with the teacher also. And knowing her she'll say lookat that code and develope your own from it. but thats better then nothing right? haha. And i dont plan to sell this game, but if somehow in the future and im some bigshot game programmer, ill remember you and add you into the credits of the game ;) as for now i'll get permission from my teacher so she doesnt flip out
closed account (4Gb4jE8b)
hahahaha, fair enough. Oh and don't think of this as usual behavior here at the forums, I take sympathy with you, as i'm in highschool too (though i've been programming in one language or another for years, still a beginner at c++ though), so i'm being nicer than i would for many.

Anyway here's the code, you'll have to edit it for your own special needs, but in a general form:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <time.h>
#include <iostream>
#include <string>

void wait ( double seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

void timed_text(std::string content, double time)
{
	int length = content.length();
	for(int i = 0; i<length+1;i++)
	{
		char a = content[i];
		std::cout << a;
		wait(time);
	}
}


this gives a constant time delay, i like this though, and it's entertaining, so i'm going to make a more realistic version of it :P
Pages: 1234