Cpp - Output float value with precision settings

Introduction

The input buffer is cleared and error flags are reset by calling the sync() and clear() methods.

This ensures that the program will wait for new input for the price.

Demo

#include <iostream>    // Declarations of cin, cout,... 
#include <iomanip>     // Manipulator setw() 
#include <string> 
using namespace std; 

int main() /*  ww  w  .  j ava2  s . c o m*/
{ 
    string label; 
    double price; 

    cout << "\nPlease enter an article label: "; 

    // Input the label (15 characters maximum): 
    cin >> setw(16);        // or:  cin.width(16); 
    cin >> label; 

    cin.sync();    // Clears the buffer and resets 
    cin.clear();   // any error flags that may be set 

    cout << "\nEnter the price of the article: "; 
    cin >> price;           // Input the price 

    // Controlling output: 
    cout << fixed << setprecision(2) 
          << "\nArticle:" 
          << "\n  Label:  " << label 
          << "\n  Price:  " << price << endl; 

    return 0; 
}

Result

Related Topic