https://rentry.org/PPP2_p358b

// 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 && ! (1 <= n && n <= 10))  // combine the read and range check
    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_p358b.cpp && ./a.out

PrevUpNext

Edit
Pub: 07 Apr 2023 22:12 UTC
Edit: 03 May 2023 09:55 UTC
Views: 359