If all are true return true

I am trying to write a simple program to learn C++
The program will ask the user to enter a series of yes or no questions
and return true or false if all are true.

The code should ask...

Did a person assaults another, [y/n?]

and inflicts substantial bodily harm, [y/n?]

and the person being harmed is a minor [y/n?]

if all are true print "This is aggravated assault involving a minor."
if all are not true print "This is not aggravated assault involving a minor."

return true or false (another function will use this as an element if true)

This same syntax will be used throughout my program for multiple statutes.
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
#include <iostream>
#include <string>
#include <vector>

bool answer_is_yes( const std::string& question )
{
    std::cout << question << "  [y/n?]? " ;

    char ans ;
    std::cin >> ans ;
    ans = std::tolower(ans) ;

    if( ans == 'y' || ans == 'Y' ) return true ;
    else if( ans == 'n' || ans == 'N' ) return false ;
    else return answer_is_yes(question) ; // try again
}

bool are_all_true( const std::vector<std::string>& questions )
{
    for( const std::string& q : questions ) // for each question
        if( !answer_is_yes(q) ) return false ; // return false if answer is not 'yes'

    return true ; // all questions received the answer 'yes'
}

int main()
{
    const bool result = are_all_true( { "Did a person assault another",
                                        "and inflict substantial bodily harm",
                                        "and the person being harmed is a minor" } ) ;

    std::cout << "\nThis is " << ( result ? " " : "not " )
              << "aggravated assault involving a minor.\n" ;
}
This is awesome. This gives me a start. i have several more complicated statutes I want to apply this to and it will be a learning experience. I received so many, "Copying other people's work is not learning." responses on other sites. I disagree. This gives me direction I can run with. Thank you again.
Registered users can post here. Sign in or register to post.