In this exercise you will use nested if
statements to decide if someone can drive. Let's assume that anyone 16 or older can legally drive, but they must also own a car to drive.
If the person is not 16 or older then you should display, "Sorry, come back in n
years and be sure you own a car when you come back.", where n is how many years until the person turns 16 year old.
If the person is 16 or older but they do NOT own a car, then your program should display, "Sorry, you need to buy a car before you can drive!" .
If the person is 16 or older and they DO own a car, then your program should display, "Yes - you can drive!"
The age
will be provided for you, you do NOT need to declare age
.We will also automatically provide a boolean variable named has_car
which will be true
if the person owns a car or false
otherwise. Our test cases will provide different values for age
and has_car
to test your code.
Please do NOT provide '\n
' or endl
in your display statements.
You can find my solution by clicking on the solution.txt file on the left pane. But please make sure you give it a go yourself first, and only check the solution if you really get stuck.
#include "gtest/gtest.h"
#include "helpers/iohelper.h"
void can_you_drive(int, bool);
namespace {
class Evaluate : public ::testing::Test {};
TEST_F(Evaluate, Test1) {
std::pair output = CAPTURE_OUTPUT(can_you_drive(1, true));
std::string stdout = output.first;
EXPECT_EQ("Sorry, come back in 15 years and be sure you own a car when you come back.", stdout) << "One year olds cannot even walk";
}
TEST_F(Evaluate, Test2) {
std::pair output = CAPTURE_OUTPUT(can_you_drive(1, false));
std::string stdout = output.first;
EXPECT_EQ("Sorry, come back in 15 years and be sure you own a car when you come back.", stdout) << "One year olds cannot even walk";
}
TEST_F(Evaluate, Test3) {
std::pair output = CAPTURE_OUTPUT(can_you_drive(15, true));
std::string stdout = output.first;
EXPECT_EQ("Sorry, come back in 1 years and be sure you own a car when you come back.", stdout) << "15 year olds cannot legally drive";
}
TEST_F(Evaluate, Test4) {
std::pair output = CAPTURE_OUTPUT(can_you_drive(15, false));
std::string stdout = output.first;
EXPECT_EQ("Sorry, come back in 1 years and be sure you own a car when you come back.", stdout) << "15 year olds cannot legally drive";
}
TEST_F(Evaluate, Test5) {
std::pair output = CAPTURE_OUTPUT(can_you_drive(16, true));
std::string stdout = output.first;
EXPECT_EQ("Yes - you can drive!", stdout) << "16 year olds can legally drive";
}
TEST_F(Evaluate, Test6) {
std::pair output = CAPTURE_OUTPUT(can_you_drive(16, false));
std::string stdout = output.first;
EXPECT_EQ("Sorry, you need to buy a car before you can drive!", stdout) << "16 year olds can legally drive but need a car";
}
TEST_F(Evaluate, Test7) {
std::pair output = CAPTURE_OUTPUT(can_you_drive(30, true));
std::string stdout = output.first;
EXPECT_EQ("Yes - you can drive!", stdout) << "30 year olds can legally drive";
}
TEST_F(Evaluate, Test8) {
std::pair output = CAPTURE_OUTPUT(can_you_drive(30, false));
std::string stdout = output.first;
EXPECT_EQ("Sorry, you need to buy a car before you can drive!", stdout) << "30 year olds can legally drive but need a car";
}
} // namespace
#include
using namespace std;
void can_you_drive(int age, bool has_car) {
//----WRITE YOUR CODE BELOW THIS LINE----
const int driving_age {16};
if (age >= driving_age) {
if (has_car) {
cout << "Yes - you can drive!";
} else {
cout << "Sorry, you need to buy a car before you can drive!";
}
} else {
cout << "Sorry, come back in " << driving_age - age << " years and be sure you own a car when you come back.";
}
//----WRITE YOUR CODE ABOVE THIS LINE----
}
//