18 c pts 121 l6 3 10209 pro f c hris hundhaus e n

Info icon This preview shows pages 18–21. Sign up to view the full content.

View Full Document Right Arrow Icon
18 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Flag-Controlled Loops (cont.) The final C interative construct, the do-while loop, can be used to trap this situation and re- prompt the user: int my_int, input_status; char skip_ch; do { printf("Please enter an integer: "); input_status = scanf("%d",&my_int); do { /* nested do-while skips rest of data line */ scanf("%c", skip_ch); } while (skip_ch != '\n'); } while (input_status == 0); Notice that, unlike the while and for loop constructs, the do-while loop construct is guaranteed to execute at least once.
Image of page 18

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
19 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Flag-Controlled Loops (cont.) You try it: Given this pattern, tailor interactive versions of read_num_axles so that they only accept valid input (an int or a double ) from the user (work with a partner for 5 minutes) int read_num_axles() { int num_axles, input_status; /* your code goes here */ } double read_weight() { double weight; int input_status; /* your code goes here */ }
Image of page 19
20 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Flag-Controlled Loops (cont.) Now let's go one step further. Let's do a reality check on each input value to make sure it's not only of the correct type, but within an acceptable range: Num_axles should be between 2 and 5 (no unicycles allowed!) int read_num_axles() { int num_axles, input_status, input_ok; char skip_ch; do { printf("Please enter the number of axles: "); input_status = scanf("%d",&num_axles); input_ok = (input_status != 0 && num_axles > 1 && num_axles < 6); /* status flag */ if (!input_ok) printf("The value you entered is invalid."); do { /* skip rest of data line */ scanf("%c", &skip_ch); } while (skip_ch != '\n'); } while (!input_ok); return num_axles; }
Image of page 20

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
21 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Flag-Controlled Loops (cont.) Now let's go one step further (cont.). Weight should be between 200.0 and 4000.0 double read_weight() { int input_status, input_ok; char skip_ch; double weight; do { printf("Please enter the vehicle weight: "); input_status = scanf("%lf",&weight); input_ok = (input_status != 0 && weight >= 200.0 && weight <= 4000.0); if (!input_ok) printf("The value you entered is invalid."); do { /* skip rest of data line */ scanf("%c", &skip_ch); } while (skip_ch != '\n'); } while (!input_ok); return weight; }
Image of page 21
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern