bool loop?

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdlib.h>
#include <basicP.h>
int main()
{
    string question;
    bool he;

                ball();
                ScreenSize(70, 25);
                title();
                getline(cin,question);
                if (question == "settings"){
                    //settings();
                }
                else if(question == "exit" || question == "nevermind" || question == "no" || question == "never mind" || question == "Nevermind" || question == "Never mind" || question == "No" || question  == "Exit"){
                    return 0;
                }
                               else if(question == "Who made you?" || question == "Who programmed you?" || "who programmed you?" || "who made you?"){
                wait(4);
                cout << "\nThe all-powerful one";
                cin.get();
                he = true;
                main();
                }
                else if(question == "Who is the all-powerful one?" || "Who is the all powerful one?" || "Who is the allpowerful one?" || "Who is the All-Powerful One?" || "Who is the All-powerful One?" || "who is the all-powerful one?" || "who is the all powerful one?" || "who is the allpowerful one?" || "who is the All-Powerful One?" || "who is the All-powerful One?"){
                    wait(4);
                    switch(random(4)){
                    case 0:
                        cout << "\nHe is";
                        break;
                    case 1:
                        cout << "\nGummybear_alpha";
                        break;
                    case 2:
                        cout << "\nHe is";
                        break;
                    case 3:
                        cout << "\nGumybear_alpha";
                        break;
                    case 4:
                        cout << "\nPerry";
                        break;
                    default:
                        system("color 00");
                        break;
                    }
                }
                else if(he == true && (question == "Who is that?" || question == "who is that?" || question == "Who's that?" || question == "Who's that?")){
                        wait(4);
                    switch(random(4)){
                    case 0:
                        cout << "\nHe is";
                        break;
                    case 1:
                        cout << "\nGummybear_alpha";
                        break;
                    case 2:
                        cout << "\nHe is";
                        break;
                    case 3:
                        cout << "\nGumybear_alpha";
                        break;
                    case 4:
                        cout << "\nPerry";
                        break;
                    default:
                        system("color 00");
                        break;
                    }
                        }
                wait(4);
                cout << "\n";
            switch (random(19)){   //Main Answers
            case 0:
                cout << "It is certain";
                break;
            case 1:
                cout << "It is decidedly so";
                break;
            case 2:
                cout << "Without a doubt";
                break;
            case 3:
                cout << "Yes, definetly";
                break;
            case 4:
                cout << "You may rely on it";
                break;
            case 5:
                cout << "As I see, yes";
                break;
            case 6:
                cout << "Most likely";
                break;
            case 7:
                cout << "Outlook good";
                break;
            case 8:
                cout << "Yes";
                break;
            case 9:
                cout << "Signs point to yes";
                break;
            case 10:
                cout << "Reply hazy, try again";
                break;
            case 11:
                cout << "Ask again later";
                break;
            case 12:
                cout << "Better not tell you now";
                break;
            case 13:
                cout << "Cannot predict now";
                break;
            case 14:
                cout << "Concentrate and ask again";
                break;
            case 15:
                cout << "Don't count on it";
                break;
            case 16:
                cout << "My reply is no";
                break;
            case 17:
                cout << "My sources say no";
                break;
            case 18:
                cout << "Outlook not so good";
                break;
            case 19:
                cout << "42";
                break;
            default:
                system("color 00");
                break;
                }
                cin.get();
                he = false;
                main();
}
At line 140 - it is illegal in C++ to call main from a function. Even from within the main function.

main is the entry point to your program. It should never be called any other way.
Then how am I supposed to make this work? I haven't had problems with that for all the months I have been using this.
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
#include <iostream>
#include <string>

int main(void)
{
	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
	}
	return 0;
}


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:
else if (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.

heebleworp
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
Last edited on
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.
http://pastebin.com/embed_js.php?i=nCi03G8y
I added basicP.h, it's not a proper header file, though.
Last edited 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void stuff()
{
 
    //statements
    if (question == "music" || question == "Music")
    {
        //statements
        stuff();
    }
    else if(question == "exit" || ...)
    {
        return;
    }
    else if(question == "Are you sure?" || ...)
    {
        //statements
        stuff();
    }
    else if(question == "Do you hear the people sing?" || ...)
    {
 
       //statements
        stuff();
    }
    else if(question == "Who are you?" || question == "who are you?")
    {
        //statements
        stuff();
    }
    else if(question == "What is the answer to the universe and everything?" || ...)
    {
        //statements
        stuff();
    }
    else if(question == "why?" || ...)
    {
        //statements
        stuff();
    }
    else if(question == "colors" || ...)
    {
        //statements
        stuff();
    }
    //statements
    stuff()


Basic outline should look more like this:
1
2
3
4
5
6
//statements
while (question != "exit" || ...)
{
    //statements
}
//statements 
Topic archived. No new replies allowed.