A common punishment for school children is to write out the
A common punishment for school children is to write out the same sentence multiple times. Write a C++ program (punishment1.cpp) that will write out the following sentence a number of times inputted by the user: I will always use object oriented programming. The program will first ask the user to input the number of times the sentence should be written, by outputting the following sentence to the screen: Enter the number of lines for the punishment: . You should check to make sure that the value entered is correct (think about what would constitute an incorrect value). If the value entered is incorrect, output the following sentence to the screen: You entered an incorrect value for the number of lines! and stop the program. If a correct value was entered, display the sentence I will always use object oriented programming. the number of times inputted by the user.
Solution
C++ code:
#include <bits/stdc++.h>
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;
}
Sample Output:
1)
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.
2)
Enter the number of lines for the punishment:
\"name\"
You entered an incorrect value for the number of lines.
3)
Enter the number of lines for the punishment:
aqw
You entered an incorrect value for the number of lines.
