windog
Newbie

Posts: 9
|
 |
« on: January 02, 2012, 04:18:50 PM » |
|
I am new to c++ and I am learning c++ by making a text rpg game that my friend and I made using batch files a long time ago. I almost instantly came across a problem that I don’t see a problem with, but my debugger did. Maybe you could take a look at my code and tell me what I did wrong. (Notes: I’m using Visual C++ 2010, for whatever reason “using namespace std;” wasn’t working so I specified each std but I wasn’t sure if the if statement was std or not and I tried it with “using std::if;” and without.)
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() { //start
cout <<"++=======================-Game-=========================++"<< endl; cin.get(); cout <<"Choose"<< endl; cout <<"1) Start Game"<< endl; cout <<"2) Quit"<< endl; endl; cin >> choice1;
if (choice1 == 2){
cout <<"Bye"<< endl; cin.get(); }
//game
if (choice1 == 1){
cout <<"You wake up in a daze head spinning and a Man is standing above you..."<< endl; cin.get(); } return 0; }
|
|
|
|
|
Logged
|
|
|
|
|
Michael Hall
|
 |
« Reply #1 on: January 02, 2012, 07:17:03 PM » |
|
What is the error message that you are getting from the compiler?
|
|
|
|
|
Logged
|
|
|
|
windog
Newbie

Posts: 9
|
 |
« Reply #2 on: January 03, 2012, 02:55:28 PM » |
|
"There were build errors. Would you like to run the last successful build?" And when I hit yes it runs the program until the if statement then when i type a response it ends the program.
|
|
|
|
|
Logged
|
|
|
|
windog
Newbie

Posts: 9
|
 |
« Reply #3 on: January 03, 2012, 03:16:53 PM » |
|
I copied and pasted the code into a new project and the error message was "Unable to start project '(my projects destination on my computer)'. The system could not find file specified.
|
|
|
|
|
Logged
|
|
|
|
windog
Newbie

Posts: 9
|
 |
« Reply #4 on: January 03, 2012, 03:59:18 PM » |
|
I just had to declare choice1. But new question now the only " cin.get();" to work is the first one. Any suggestions on how to fix this.
|
|
|
|
|
Logged
|
|
|
|
|
Michael Hall
|
 |
« Reply #5 on: January 03, 2012, 04:57:02 PM » |
|
Execute the program by pressing (CTRL + F5). This is the "Start without debugging" option, and it will keep the window open.
Mike
|
|
|
|
|
Logged
|
|
|
|
windog
Newbie

Posts: 9
|
 |
« Reply #6 on: January 05, 2012, 03:48:41 PM » |
|
Hello me again, I was wondering if there was a way to collapse whole sections of code (like the warrior class version of the game) so I could collapse it when I’m not working on the warrior class’ version but another class’ version. I thought using data structures would work but the std library wasn’t working in the struct{ } is there any code I could use to collapse sections of code?
|
|
|
|
|
Logged
|
|
|
|
|
Michael Hall
|
 |
« Reply #7 on: January 05, 2012, 04:57:55 PM » |
|
If you're using Visual C++ 2010, you should be able to do that by default. There are little plus boxes next to the code.
Mike
|
|
|
|
|
Logged
|
|
|
|
windog
Newbie

Posts: 9
|
 |
« Reply #8 on: January 05, 2012, 06:03:20 PM » |
|
Yes but only by “int main()” or if I add “struct”s was wondering if I could place these and collapse certain parts of the code instead of the entire main. (On and unrelated note) I will most likely be posting A LOT on this forum thread so do not get too annoyed with me and I thank you for the time that you spend responding to me.
|
|
|
|
|
Logged
|
|
|
|
|
Michael Hall
|
 |
« Reply #9 on: January 05, 2012, 06:32:15 PM » |
|
Don't sweat it. I don't get annoyed. You should be able to collapse any block of code (defined by {}), individually. That includes the code for a struct, class, if, for, etc., as long as you enclose the code in braces ({}).
Mike
|
|
|
|
|
Logged
|
|
|
|
windog
Newbie

Posts: 9
|
 |
« Reply #10 on: January 05, 2012, 07:06:37 PM » |
|
Huh that is strange; in my project the only brackets I can collapse are the “int main()” and the section above were I include. Could it possibly have something to do with my settings, because I also do not have numbered lines it hasn’t bothered me much. I have used other IDEs like Dev C++ in my previous attempts to learn C++ and when I opened Visual C++ for the first time I thought it was weird that it didn’t have the lines numbered and I just figured it was something you could personalize in the settings but I never dug too deep into finding it.
|
|
|
|
|
Logged
|
|
|
|
|
Michael Hall
|
 |
« Reply #11 on: January 05, 2012, 07:25:18 PM » |
|
You should get the collapse-able section without the line numbers, I think. However, you can enable line numbering too. Maybe that will do it. http://xoax.net/comp/cpp/visualcpp/EnablingLineNumbering.phpBTW, once you make a new block, you may need to go away from it for minute before it shows up. Mike
|
|
|
|
|
Logged
|
|
|
|
windog
Newbie

Posts: 9
|
 |
« Reply #12 on: January 06, 2012, 07:23:12 PM » |
|
In my game I have a main menu were you can go to the dungeon, the town, go to your inventory, see your characters stats, read up on the history of my made-up world, but the problem I’m having is when I’m done looking at my Inventory etc. I can’t get back to my main menu. When I first made the game using batch files I just used goto (I’m sure you are familiar with bat files but if not goto just says if “user input” the goto a specific part of the code) I there a goto- like code in c++ I could use.
|
|
|
|
|
Logged
|
|
|
|
|
Michael Hall
|
 |
« Reply #13 on: January 09, 2012, 03:03:33 PM » |
|
Yes, there are gotos, but they are highly frowned upon. You just put a label in your code followed by a colon and then write goto <label name>. EndLoop:
// some to loop over.
goto EndLoop; Mike
|
|
|
|
|
Logged
|
|
|
|
windog
Newbie

Posts: 9
|
 |
« Reply #14 on: January 12, 2012, 12:06:59 AM » |
|
Why frowned upon?
|
|
|
|
|
Logged
|
|
|
|
|
Michael Hall
|
 |
« Reply #15 on: January 12, 2012, 12:54:52 PM » |
|
Frequently, goto statements are abused and cause unreadable spaghetti code, which is difficult to debug. So, they have acquired a bad reputation. I'm not a big believer in such rules, since I think that anything can be abused and create bad code if one isn't careful. However, I don't use goto statements.
Mike
|
|
|
|
|
Logged
|
|
|
|
|