So I have been poking at this program for a few months now, just in my spare time. It's just a magic 8-ball with some custom responses. I'm trying to use a boolean variable to signify if the question "who made you?" (or something like that) has been asked. When I run it, it just keeps saying "The all-powerful one"
I know that there is bound to be another problem with my organization, but I like it how it is. Also, anyhelp with the lengthy if statements would be greatly appreciated. Don't be rude, please, remember that you're answering questions in the BEGINNERS section!
How to make what work? A loop? You could try using one of the perfectly good loop constructs that C and C++ have, such as for, while or do... while. Even the most rudimentary of textbooks or tutorials should cover them.
OK, fine. I moved it all to a separate function which recurses. I'm thinking you didn't understand my problem. When I run it, it only says 'The All Powerful One" When I take ut the whole boolean thing (if he == TRUE) it works fine.
Post Scribitum; I'm a beginner not a noob, I have been doing this a while and am fairly good with using the small amount of C++ I know. Please don't treat me like I know nothing.
Fairly new myself but will try to help. First, having main() call itself is usually a bad idea even if it is allowed as it can lead to unexpected behaviour, and for this problem there are better solutions (the control loops mentioned). However having main() call itself isn't what's causing the problem, it's line 18. Here's a very simple test program that illustrates what's going on:
1 2 3 4 5 6 7 8 9 10 11 12
std::string question = "blargh";
if (question == "Who made you?" || question == "Who programmed you?" || "who programmed you?" || "who made you?") //problem
std::cout << "here"; //we don't want to be here
The if statement is always true! The reason is because you can't string ||'s together the way you have. if(*stuff* || "some string")
evaluates to true since "some string" is first converted to a nonzero value then used in the evaluation. Try the following: elseif (question == "Who made you?" || question == "Who programmed you?" || question == "who programmed you?" || question == "who made you?")
The same problem pops up on line 25. Note also that I believe you have another smaller problem with unnecessary cin.get(); calls, which will cause the program to wait for a character to input before continuing. If your program only prints one "The all powerful one" (is this the case?) then what's happening is that it is waiting for you to enter stuff before continuing, though you might have to wait a bit before doing so to see if this is the case (I'm unsure how wait() works).
Finally you really should use while() loops for this program, the concept is easy to grasp if you are unfamiliar. Either read a quick tutorial on how to use them or see if you can grasp the concept in 10 seconds by understanding the following syntax:
1 2 3 4
while (*condition you want to check*)
*things you want done*
Like I said in the question, the rest of this program works, the problem is around line 48, since that is the new code.
What's up there is shortened since it was saying I couldn't have so many characters in my question. I took out the extra question == s since I figured people would know that that is not what I was asking about, so it probably works, since (like I said) it worked perfectly before I added the whole 'who made you?' part. (There are 157 lines I erased)
and also, I shall restate, I already removed the recursion of the main function
I'll need to see your new code then since what I thought was happening was that the program was stuck in an infinite loop due to the always true if() and then the recursive main() call on line 23. Little nifty site that can help get around the character limit (you'll have to include a link to your paste): http://pastebin.com/
Or just post the relevant code that's causing the problem. Remember that you can always comment out parts of code while testing for errors to isolate the problem code. Also is the output just one "The all powerful one" or a load of them which indicates an infinite loop? Finally what is basicP.h? As far as my compiler is concerned it doesn't exist, did you add it to the standard library?
P.S. Just for informational purposes the recursive main() call works on some compilers but not on others (I use g++ and it works on it). However the standard says that main() should not be allowed to call itself so even if your compiler allows it don't do it as your code will not be portable. This is especially confusing since recursive main() calls are allowed in plain old C which is what C++ was built on.
Here's the basic structure of your program. If your question is anything other than "exit" (or equivalent) you recurse stuff(), sometimes twice (assuming input is correctly monitored). However if you then type "exit" you've only bumped off one level of recursion, and the program continues to run. It will be much, much simpler and efficient to use a proper control loop like while(), not to mention using it will lead to proper behaviour.