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.
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.
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.
// askquestion - cplusplusforum
// function that validates a yes/no question
#include<iostream>
#include<string>
usingnamespace 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;
}
elseif (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
*/
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
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
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.
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