Язык C: Когда я использую scanf, на консоли вывода отображается «Программа завершилась с кодом выхода: 0 [что бы я ни вводил для scanf]» Как мне это исправить? - PullRequest
0 голосов
/ 12 октября 2018

Например:

printf("Please enter the amount of money you want to accumulate: ");
scanf("%f", &cashGoal);
printf("You would like to save up to $%.2f\n", cashGoal);

Возвращает это:

Please enter the amount of money you want to accumulate: Program ended with exit code: 0123123
You would like to save up to $123123.00

Редактировать: Вот полный код, это мой домашний проект, поэтому он немного длинный

#include <stdio.h>
#include <math.h>

FILE *fp;

#define PENNY 0.01

int daystoCashGoal(float);

int main(void) {
fp = fopen("csis.txt", "w");
float cashGoal;


printf("Please enter the amount of money you want to accumulate: ");
fprintf(fp, "Please enter the amount of money you want to accumulate: ");
scanf("%f", &cashGoal);
printf("\n");
fprintf(fp,"\n");
printf("You would like to accumulate $%.2f.\n\n", cashGoal);
fprintf(fp, "You would like to accumulate $%.2f.\n\n", cashGoal);

daystoCashGoal(cashGoal);

fclose(fp);
return 0;
}

//Calculates the number of days it will take to accumulate the user-desired amount, doubles the deposit for each day, and sums up the balance for each day
int daystoCashGoal(float cashGoal){
int day = 1;
float dailyBalance = 0, dailyDeposit;

printf("DAY          DEPOSIT          BALANCE\n");
fprintf(fp, "DAY          DEPOSIT          BALANCE\n");
printf("___          _______          _______\n");
fprintf(fp, "___          _______          _______\n");

 while (dailyBalance <= cashGoal) {
    dailyDeposit = (pow( 2, day - 1)) * PENNY;
    dailyBalance += dailyDeposit;

    printf("%3d %5s %10.2f %5s %10.2f\n", day,"", dailyDeposit,"", dailyBalance);
    fprintf(fp, "%3d %5s %10.2f %5s %10.2f\n", day,"", dailyDeposit,"", dailyBalance);

     if (dailyBalance <= cashGoal) {
                 day ++;
     }
     else if (dailyBalance >= cashGoal) {
         continue;
     }

}

if (dailyBalance >= cashGoal) {
    printf("\nIt took %d days to accumulate at least $%.2f.\n", day, cashGoal);
    fprintf(fp, "\nIt took %d days to accumulate at least $%.2f.\n", day, cashGoal);
}

return day;
}

Я не ищу совета по остальной части моего кода, так как этот проект уже оценен, но проблема с кодом выхода влияет на остальные мои проекты, и я не уверен, что делаю неправильно.Спасибо

...