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.
", n_max); 18. n_min); 19. 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. error = 1; 29. 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. } "/>Extracted 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.="" apae="" post:="" result="" is="" in="" the="" range="" n="" min="" through="" n_max.="" *="" 4.="" 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.="" */="" printf("enter="" an="" integer="" in="" the="" range="" from="" %d="" ",="" printf("to="" %d="" inclusive=""> ", n_max); 18. n_min); 19. 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. error = 1; 29. 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. }=>
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here