5.8 The do-while Statement and H FIGURE 5.14 Validating Input Using do-while Statement 1. /* Returns the first integer between n_min and n_max en * Pre : n_min <= n_max 4. * Post: Result is in the range n_min through n_max. 2. 3.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter2: Problem Solving Using C++using
Section2.3: Data Types
Problem 9E: (Practice) Although the total number of bytes varies from computer to computer, memory sizes of...
icon
Related questions
icon
Concept explainers
Question

If we wanted to rewrite the code on page 275 to use while loops instead of do-while loops, what would we have to do differently? Explain.

275
5.8 • The do-while Statement and Flag-Controlled Loops
FIGURE 5.14 Validating Input Using do-while Statement
1. /*
Returns the first integer between n min and n max entered as data.
Pre : n_min <= n_max
2.
3.
4.
Post: Result is in the range n min through n_max.
* /
5.
6.
int
7.
get
_int (int n_min, int n_max)
8.
{
9.
int
in_val,
/* input - number entered by user
* /
10.
/* status value returned by scanf
* /
status;
11.
char
skip_ch;
/* character to skip
* /
12.
int
/* error flag for bad input
* /
error;
13.
/* Get data from user until in val is in the range.
* /
14.
do {
15.
/* No errors detected yet. */
16.
error = 0;
17.
/* Get a number from the user. */
18.
printf("Enter an integer in the range from %d ",
n_min);
19.
printf("to %d inclusive> ", n_max);
20.
status = scanf("%d", &in_val);
21.
22.
/* Validate the number. */
23.
if (status != 1) { /* in_val didn't get a number */
24.
error = 1;
scanf("%c", &skip_ch);
printf("Invalid character >>%c>>. ", skip_ch);
printf("Skipping rest of line.\n");
} else if (in_val < n_min || in_val > n_max) {
25.
26.
27.
28.
29.
error = 1;
30.
printf("Number %d is not in range.\n", in_val) ;
31.
32.
/* Skip rest of data line. */
33.
do
34.
scanf ("%c", &skip_ch);
while (skip_ch != '\n');
35.
36.
} while (error);
37.
38.
return (in_val);
39.
40. }
12.
Transcribed Image Text:275 5.8 • The do-while Statement and Flag-Controlled Loops FIGURE 5.14 Validating Input Using do-while Statement 1. /* Returns the first integer between n min and n max entered as data. Pre : n_min <= n_max 2. 3. 4. Post: Result is in the range n min through n_max. * / 5. 6. int 7. get _int (int n_min, int n_max) 8. { 9. int in_val, /* input - number entered by user * / 10. /* status value returned by scanf * / status; 11. char skip_ch; /* character to skip * / 12. int /* error flag for bad input * / error; 13. /* Get data from user until in val is in the range. * / 14. do { 15. /* No errors detected yet. */ 16. error = 0; 17. /* Get a number from the user. */ 18. printf("Enter an integer in the range from %d ", n_min); 19. printf("to %d inclusive> ", n_max); 20. status = scanf("%d", &in_val); 21. 22. /* Validate the number. */ 23. if (status != 1) { /* in_val didn't get a number */ 24. error = 1; scanf("%c", &skip_ch); printf("Invalid character >>%c>>. ", skip_ch); printf("Skipping rest of line.\n"); } else if (in_val < n_min || in_val > n_max) { 25. 26. 27. 28. 29. error = 1; 30. printf("Number %d is not in range.\n", in_val) ; 31. 32. /* Skip rest of data line. */ 33. do 34. scanf ("%c", &skip_ch); while (skip_ch != '\n'); 35. 36. } while (error); 37. 38. return (in_val); 39. 40. } 12.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr