https://rentry.org/PPP2_p358a

// Code derived from Stroustrup's PPP2 book
// § 10.7 Reading a single value
//  -and beginning on p 358

#include <iostream>

using namespace std;

int main()
{
  cout << "Please enter an integer in the range 1 to 10 (inclusive):\n";
  int n = 0;

  while (cin >> n) {        // read
    if (1 <= n && n <= 10)  // check range
      break;                // exit while() loop when a good value is obtained

    cout << "Sorry " << n << " is not in the [1:10] range; please try again\n";
  }

  // what could possibly go wrong here?
  // … use n here …
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_10/main_p358a.cpp && ./a.out

PrevUpNext

Edit
Pub: 07 Apr 2023 22:10 UTC
Edit: 03 May 2023 09:54 UTC
Views: 342