C - Write program to read numerical values with dollar sign, monetary amounts separated by commas and spaces

Requirements

Write a program that will read the numerical values

Example input:

$3.50  ,  $4.75 , $9.95 , $2.50

Calculate the total

Hint

use the following specification to ignore space, comma and dollar sign

"%*[ ,$]%lf"

Demo

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#define COUNT 4//from www . j  a  va 2 s  . c  o m

int main(void)
{
  double amounts[COUNT] = { 0.0 };
  double total = 0.0;

  printf_s("Enter the %d amounts:\n", COUNT);
  for (size_t i = 0; i < COUNT; ++i)
  {
    scanf_s("%*[ ,$]%lf", &amounts[i]);
    total += amounts[i];
  }

  printf_s("The total of the input is: $%.2lf\n", total);
}

Result