Instead of the final print out being: The sum of the dollar amounts is
I would like for it to display: The sum of the (%d how ever many amounts they put in) is
How do you set (the value of how many times the user input a dollar amount to be calculated) to a variable so that it can be referenced in the final printf?
Code:void sumDoubles ()
{
double num = 0;
double total = 0;
int doublesEntered = 0;
int index;
double amounts[100];
FILE *myFile;
myFile = fopen("assignment3.txt", "w");
printf("\nI want to sum up dollar amounts.\n\n");
fprintf(myFile, "You chose to sum a list of dollar amounts.\n\nThese are the dollar amounts:\n\n");
do
{
printf("\nEnter an dollar amount (enter -1 to quit): \n\n\t\t\t");
scanf("%lf", &num);
if (num != -1 )
{
amounts[doublesEntered] = num; // store each num in the array
doublesEntered++; // advance the index by 1 for the next num
fprintf(myFile, "$%.2f\n", num);
total += num;
printf("\n\tThe total so far is: $%.2f\n", total);
}
else
break;
} while(num != -1);
for (index = 0; index < num; index++)
{
total += amounts[index];
}
fprintf(myFile, "\nThe sum of the dollar amounts is $%.2f", total);
printf("\nThe sum of the dollar amount is $%.2f\n\n\n", total);
printf("\tThank you for playing!\n\n\n\n");
fclose(myFile);
return;
}