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:
Exception Handling
Exercise:
Programming Projects
Question:3 | ISBN:9780321531346 | Edition: 7

Question

Write a program that inputs numeric values from 1 through 10 and outputs a textual histogram of the values using *’s to count the number of occurrences of each value. The program should first ask the user how many numbers to enter. If the user enters a value that does not consist of all digits or a number outside the range 1 to 10, then an exception should be caught. Hint: Input each number as a string, and then scan through the string to see if it contains all digits. If not, throw an exception. To convert a string str to an integer, use the following code:

atoi(str.c_str());

The atoi function is described in Chapter 8.


Here is a sample dialogue:

How many numbers to enter?

5

Enter number 1L

one

Please enter your number using digits only. Try again.

Enter number 1:

9

Enter number 2:

3

Enter number 3:

3

Enter number 4:

33

The number must be between 1-10. Try again.

Enter number 4:

3

Enter number 5:

7

Here is the histogram of values:

1 :

2 :

3 : ***

4 :

5 :

6 :

7 : *

8 :

9 : *

10:

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.