Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code needs to written in C++ with no use of system pause if possible. Write a pr

ID: 3683334 • Letter: C

Question

Code needs to written in C++ with no use of system pause if possible.

Write a program that does the following: •

Prompts the user for a 12-digit (maximum) double precision number. (e.g.: 1234567890.12, -12345.67, 1234.5, etc.)

Verifies that the input is valid: o There are a maximum of 12 digits (excluding the sign and decimal point).

All input digits are numeric with the following possible exceptions:

Minus sign (-) in the first position

Decimal point (.) in the 2nd or 3rd position before the end digit. Fractions of more than 2 digit positions are truncated .

Converts the number into a currency format using string manipulation. The format for the two examples above: $1,234,567,890.12, -$12,345.67 and $1,234.50

• Please note:

Commas are placed at 3-digit intervals, counted from the position of the decimal dot (.)

A $ is placed in front (to the left) of a positive amount.

A $ is placed after the minus sign (-) of a negative amount.

0 shall be treated in the same manner as a positive number .

Displays the number in the currency format

Explanation / Answer

#include <iomanip>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

void showCurrency(double dv,bool neg, int width = 12)
{
const string radix = ".";
const string thousands = ",";
string unit = "$";
if(neg)
unit = "-$";
unsigned long v = (unsigned long) ((dv * 100.0) + .5);
string fmt,digit;
int i = -2;
do {
if(i == 0) {
fmt = radix + fmt;
}
if((i > 0) && (!(i % 3))) {
fmt = thousands + fmt;
}
digit = (v % 10) + '0';
fmt = digit + fmt;
v /= 10;
i++;
}
while((v) || (i < 1));
cout << unit << setw(width) << fmt.c_str() << endl;
}

double truncate_to_places(double d, int n) {
return d - fmod(d, pow(10.0, -n));
}

double getDigits(double number){

int digits = 0; do { number /= 10; digits++; } while (number != 0);
return digits;
}

int main()
{
bool neg=false;
double x;// = 123456781.2390;
cout << "Enter max 12 digit number" << endl;
cin >> x;
if(x<0){
neg = true;
x = 0 - x;   
}
  
x = truncate_to_places(x,2);
showCurrency(x,neg);

return 0;
}