https://rentry.org/PPP2_p179

// Code derived from Stroustrup's PPP2 book
// § 6.3.1 First attempt
//  -and beginning on p 179

#include <iostream>

using std::cin;
using std::cout;

int main()
{
  cout << "Please enter expression (we can handle + and –): \n";
  int  lval = 0;
  int  rval = 0;
  char op   = ' ';
  int  res  = 0;
  cin >> lval >> op >> rval;  // read something like 1 + 3

  if (op == '+')
    res = lval + rval;  // addition
  else if (op == '-')
    res = lval - rval;  // subtraction

  cout << "Result: " << res << '\n';
}

build & run:

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

PrevUpNext

Edit
Pub: 13 Mar 2023 04:00 UTC
Edit: 30 Apr 2023 07:17 UTC
Views: 585