SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Introduction To Computers And C++ Programming
Exercise:
Programming Projects
Question:2 | ISBN:9780321531346 | Edition: 7

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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
0 0

Discussions

Post the discussion to improve the above solution.