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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education