Kamis, 27 Maret 2014

Ebook Chapter 5 Problem Solving and Program Design in C

  • 5.8 Figure 5.14  Validating Input Using do-while Statement
 #include <cstdlib>
#include <iostream>

using namespace std;

 int
 get_int (int n_min, int n_max) {
         int in_val,status,error;
         char skip_ch;

do {
    error=0;
    cout<<"Enter an integer in the range from "<<n_min<<endl;
    cout<<"to inclusive> "<<n_max;
    status = in_val;
  
    if (status != 1){
       error=1;
       cin>>skip_ch;
       cout<<"Invalid character >>"<<skip_ch;
       cout<<"Skipping rest of line."<<endl;
       }
       else if (in_val < n_min || in_val > n_max ) {
            error =1;
            cout<<"Number is not in range."<<in_val;
            }
       do
          cin>> skip_ch;
       while (skip_ch != ' ');
       }
       while (error);
     
       return (in_val);
       }
      

0 komentar:

Posting Komentar

 

Anita © 2010

Blogger Templates by Splashy Templates