Since we want to make the punishment more realistic, we will add a typo in one o
ID: 3785710 • Letter: S
Question
Since we want to make the punishment more realistic, we will add a typo in one of the lines of the punishment. This is very similar to what you have already done in punishment1.cpp, so it would be a good idea to copy your punishment1.cpp and start from there. Your new program (punishment2.cpp) will ask the user for the number of lines for the punishment: Enter the number of lines for the punishment: . Once again, if an incorrect value has been entered, your program should output You entered an incorrect value for the number of lines! and stop executing (up to this point, you should not have to change anything from your last exercise. Next, the program will ask on what line a typo should be made by outputting Enter the line for which we want to make a typo: to the screen. Once again, you should check that the value entered by the user is correct (think about what would constitute an incorrect value). If the value is incorrect, display You entered an incorrect value for the line typo! and stop program execution. If both inputs are correct, you should then display the punishment sentence the correct number of times (I will always use object oriented programming.), making sure to change it to I will always use object oriented programing. (the typo) for the line number defined by the user/input.
punishment1.cpp
#include <iostream>
using namespace std;
int main()
{
int nlines;
cout << "Enter the number of lines for the punishment: "<<endl;
cin >> nlines; // take input
if(nlines < 0)
{
cout << "You entered an incorrect value for the number of lines." << endl; // print the output
return 0;
}
for (int i = 0; i < nlines; ++i)
{
cout << "I will always use object oriented programming." << endl; // print the output
}
return 0;
}
Explanation / Answer
#include<iostream>
using namespace std;
class Test
{
public:
int nlines;
int testing()
{
cout << "Enter the number of lines for the punishment: "<<endl;
cin >> nlines; // take input
if(nlines < 0)
{
cout << "You entered an incorrect value for the number of lines." << endl; // print the output
return 0;
}
for (int i = 0; i < nlines; ++i)
{
cout << "I will always use object oriented programming." << endl; // print the output
}
return 0;
}
};
int main()
{
Test t;
t.testing();
return 0;
}
output
Enter the number of lines for the punishment:
-1
You entered an incorrect value for the number of lines.
Enter the number of lines for the punishment:
5
I will always use object oriented programming.
I will always use object oriented programming.
I will always use object oriented programming.
I will always use object oriented programming.
I will always use object oriented programming.