Modify the C++ program you entered in Programming Project 1. Change the program so that it first writes the word Hello to the screen and then goes on to do the same things that the program in Display 1.8 does. You will only have to add one line to the program to make this happen. Recompile the changed program and run the changed program. Then change the program even more. Add one more line that will make the program write the word Good-bye to the screen at the end of the program. Be certain to add the symbols \n to the last output statement so that it reads as follows:
cout << "Good-bye\n";
(Some systems require that final \n, and your system may be one of the systems that requires a final \n.) Recompile and run the changed program.
Modified program code:
//Header file
#include <iostream>
using namespace std;
//main function
int main( )
{
//Variable declaration
int number_of_pods, peas_per_pod, total_peas;
//Prompt the message, Hello
cout << "Hello\n"; //This line is added for question
//Prompt the user to enter input
cout << "Press return after entering a number.";
cout << "Enter the number of pods:";
cin >> number_of_pods;
cout << "Enter the number of peas in a pod:";
cin >> peas_per_pod;
//Calculation
total_peas = number_of_pods * peas_per_pod;
//Display output
cout << "If you have ";
cout << number_of_pods;
cout << " pea pods ";
cout << "and ";
cout << peas_per_pod;
cout << " peas in each pod, then ";
cout << "you have ";
cout << total_peas;
cout << " peas in all the pods. ";
//Prompt the message, Good-bye
cout << "Good-bye\n";//This line is added for question
return 0;
}
Output:
Hello
Press return after entering a number.Enter the number of pods:12
Enter the number of peas in a pod:12
If you have 12 pea pods and 12 peas in each pod, then you have 144 peas in all the pods. Good-bye