function will not return bool variable as true

Pages: 12
So i have a program that has a if statement - below-.
1
2
3
4
5
6
7
8
9
10
11
if (questionint == true)
    {
        string q2 = "\nHave you eaten watermelon recently? ";
        askquestion(q2, questionint);

    }
    else
    {
        string q3 = "\nIs your favorite color blue? ";
        askquestion(q3, questionint);
    }


I cannot get the first option to activate due to the bool variable that I made not being true.

The function which returns the bool variable is here.

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
bool askquestion (string question, bool questionint)
{
    string answer;
    cout << question;
    cin >> answer;
    if (answer == "Yes" || answer == "yes")
    {
        questionint = true;
        return questionint;
    }
    else if (answer == "No" || answer == "no")
    {
        questionint = false;
        return questionint;
    }
    else
    {
        while (answer == "Yes" || answer == "yes" || answer == "No" || answer == "no");
        {
            cout << "\nSorry, your answer did not seem to qualify. You must answer Yes or No. Please \ntry again\n" << endl;
            cout << question;
            cin >> answer;
            if (answer == "Yes" || answer == "yes")
            {
                questionint = true;
                return questionint;
            }
            else if (answer == "No" || answer == "no")
            {
               questionint = false;
               return questionint;
            }
            else
            {

            }
        }
    }
}


Here is the entire program.


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
// Fortune Teller
#include <iostream>
#include <string>

using namespace std;


bool askquestion (string question, bool questionint)
{
    string answer;
    cout << question;
    cin >> answer;
    if (answer == "Yes" || answer == "yes")
    {
        questionint = true;
        return questionint;
    }
    else if (answer == "No" || answer == "no")
    {
        questionint = false;
        return questionint;
    }
    else
    {
        while (answer == "Yes" || answer == "yes" || answer == "No" || answer == "no");
        {
            cout << "\nSorry, your answer did not seem to qualify. You must answer Yes or No. Please \ntry again\n" << endl;
            cout << question;
            cin >> answer;
            if (answer == "Yes" || answer == "yes")
            {
                questionint = true;
                return questionint;
            }
            else if (answer == "No" || answer == "no")
            {
               questionint = false;
               return questionint;
            }
            else
            {

            }
        }
    }
}
int main ()
{
    bool questionint;
    string q1 = "Do you like chicken nuggets? ";
    askquestion(q1, questionint);
    if (questionint == true)
    {
        string q2 = "\nHave you eaten watermelon recently? ";
        askquestion(q2, questionint);

    }
    else
    {
        string q3 = "\nIs your favorite color blue? ";
        askquestion(q3, questionint);
    }
    return 0;
}



The whole program is not finished yet. This is just the basics of it.
Last edited on
Your passing questionint to askquestion(q1, questionint); by value not reference (the value of questionint is undetermined after the return, what ever was on the stack). Also you are actually returning questionint from askquestion.

One fix would be (with this solution you would not need to pass questionint to the function, I didn't fix it in this example)
1
2
string q1 = "Do you like chicken nuggets? ";
bool questionint = askquestion(q1, questionint);
Passing questionint to the function like you are doing does nothing to the variable in main() after the function has finished. Try assigning the result of the function (the return value) to your bool instead.
binarybob350
One fix would be (with this solution you would not need to pass questionint to the function, I didn't fix it in this example)
string q1 = "Do you like chicken nuggets? ";
bool questionint = askquestion(q1, questionint);


I tried this and it worked... except the fact that now, the else statement will not activate and instead it is always the if statement.
Last edited on
In your function, not all control paths return a value. You should get some sort of warning for that. I think your internal while loop is also incorrect; current it is set to loop while their input is valid.
About the while loop would be my next question. But can you provide an example for how to fix all of this?
Negating the entire loop condition would do it, I think.
Can you show me an example to how you do this? I am kinda new to programming. (And please don't tell me to go to an easier project.)
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
// askquestion - cplusplusforum
// function that validates a yes/no question

#include<iostream>
#include<string>

using namespace std;

bool askquestion (string const question); //prototype

int main ()
{
	bool answer;
    string q1 = "Do you like chicken nuggets? ";
 	answer = askquestion(q1);
	cout << "you answered: " << answer << endl;

	q1 = "are you okay? ";
	answer = askquestion(q1);
	cout << "you answered: " << answer << endl;
	
    return 0;
}


//implementation
bool askquestion (string const question) {
	
	string answer;
	int answerint = -1;

	while(answerint == -1){
	
		cout << question;
		cin >> answer;

		if (answer == "yes" || answer == "Yes"){				
			answerint = 1;
		}
		else if (answer == "no" || answer == "No")
			answerint = 0;
		else {
			answerint = -1;
			cout << "your answer is not valid\n";
		}
	}
	
	return answerint;
}


/*
1st run:
Do you like chicken nuggets? yes
you answered: 1
are you okay? no
you answered: 0

2nd run:
Do you like chicken nuggets? adsfadf
your answer is not valid
Do you like chicken nuggets? vvv   
your answer is not valid
Do you like chicken nuggets? yes
you answered: 1
are you okay? adadf
your answer is not valid
are you okay? yes
you answered: 1

*/
Thanks! I'm going to see if this works tomorrow
I'd shorten it to:

1
2
3
4
5
6
7
8
9
10
11
12
13
bool askquestion (const string& question)
{
  for(;;)
  {
    string answer;
    cout << question;
    cin >> answer;

    if (answer=="yes" || answer=="Yes")return true;
    if (answer=="no" || answer=="No")return false;
    cout << "your answer is not valid" << endl;
  }
}
@Athar: cool man. thanks.
Athar, your solution is hardly shorter. But muratagenc solution is better since he uses the return statement at the end of the function.

You as an experienced programmer (I believe) should know it. If something has to be done after the loop your code has to be entirely rewritten and that's certainly not a good solution.

return in the middle of a function should be avoided
If something has to be done after the loop your code has to be entirely rewritten and that's certainly not a good solution.
1
2
3
4
5
bool askquestion_that_does_something_after_the_loop (const string& question){
  bool answer = askquestion(question);
  //do something
  return answer;
}


Edit: I really hate when an artificial variable is used instead of a break.
However I like the idea of returning an enum {Invalid=-1, No=0, Yes=1}; and later make a wrapper of that
Last edited on
value of questionint cannot be changed inside the function unless you call it by reference
use &questionint....
Um, I just want to state that I consider return in the middle of a function a bad practice. Nothing against Athar!
sometimes, we had to. right? btw, i got a question, i got this from mr. bjarne stoursoup's (i don't remember the exact name, sorry) ebook:
1
2
3
4
5
6
template <class In, class T> int count (In first, In last, connst T& val) {
	int res = 0;
	//i don't understand the *first++ what is that mean?
	while (first != last) if (*first++ == val) ++res;
	return res;
}

this is when it called:
1
2
3
4
5
6
void f (vector <complex>& vc, strin s, list <int> & li) {
	int c1 = count (vc.begin(), vc.end(), complex (0));
	int c2 = count (s.begin(), s.end(), 'x');
	int c3 = count (li.begin(), li.end(), 42);
	//...
}

it is a continuation from the previous example in the book, actually. well i thought, *first++ is increasing the address by 1, but then i found that the syntax is wrong. it should be: first++ . because the while condition is has to be false, but i don't see any increment of first
Stroustrup
*first++ \equiv *(first++) I found that syntax confusing (if it is legal)
It is dereferencing the iterator, comparing it against the value, and then incrementing the iterator.
1
2
for(; first!=last; ++first)
  if( *first==val ) ++res;


Edit: ¿how is that related to this thread?
Last edited on
Um, I just want to state that I consider return in the middle of a function a bad practice.

If you're a C programmer, then I'm not surprised. But this is a C++ forum and it does not have the limitations that made multiple return points problematic in C.

return in the middle of a function should be avoided

No.
@ne: hahaah i know dude, i just don't want to make a new thread, which will only have a short explanation :)) but i tried this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <conio.h>
using namespace std;

int main () {
	int *p, point = 1;
	p = &point;
	cout << *p++ << endl;
	cout << *p << endl;
	cout << "press any key to continue..." << endl;
	getch();
	return 0;
}


and the result is:

1
-858993460
press any key to continue...

don't understand...
Pages: 12