Включение Math.h приводит к ошибкам в cmath - PullRequest
0 голосов
/ 27 июня 2019

В моих включениях я использовал #include, но когда я пытаюсь скомпилировать и запустить программу, она дает мне длинный список синтаксических ошибок. Visual studio сообщает мне, что ошибки находятся в файле cmath, который я не включил и не использовал в программе

Программа отлично работает на C ++, но не на c

Это все, что включает в себя:

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

Это единственное место, где я использую любой тип математики из math.h:

double compoundinterest(double principle, double interest, double years) {
    float a = 1 + (interest);
    float b = pow(a, years);
    return principle * b;

Ошибка вывода журнала:

1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\cstdlib(20,51): error C2061:  syntax error: identifier 'noexcept'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\cstdlib(20,51): error C2059:  syntax error: ';'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\cstdlib(20,60): error C2449:  found '{' at file scope (missing function header?)

(редактировать: большинство ошибок были удалены, потому что они были очень похожи и занимали слишком много места)

Вся программа:

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

int leapyears(int year);
int numberofdays(int month, int day, int year);
int monthlength(int month, int year);
double compoundinterest(double principle, double interest, double years);
double simpleinterest(double principle, double interest, double years);

int main(void) {
    int option = 5;
    int month, day, year;
    printf("option 0: exit program\n");
    printf("option 1: Calculate and display the day of the week of a given date\n");
    printf("option 2: Calculate and display the principal on a savings account after a given number of years for compounded interest.\n");
    printf("option 3: Calculate and display the principal on a savings account after a given number of years for simple interest\n");
    while (option != 0) {
        int days, week;
        double p, i, y;
        printf("enter an option: ");
        scanf("%d", &option);
        switch (option) {
        case 0:
            printf("goodbye\n");
            break;
        case 1:
            printf("enter the date in the format month day year in numerical form: \n");
            scanf(" %d %d %d", &month, &day, &year);
            days = numberofdays(month, day, year);
            week = days % 7;
            switch (week) {
            case 1:
                printf("tuesday\n");
                break;
            case 2:
                printf("wednesday\n");
                break;
            case 3:
                printf("thursday\n");
                break;
            case 4:
                printf("friday\n");
                break;
            case 5:
                printf("saturday\n");
                break;
            case 6:
                printf("sunday\n");
                break;
            default:
                printf("monday\n");
                break;
            }

            break;
        case 2:
            printf("Enter the initial principle, interest rate(as a decimal), and number of years for compound interest: \n");
            scanf(" %lf%lf%lf", &p, &i, &y);
            printf("Your principle would be valued at %lf\n", compoundinterest(p, i, y));
            break;
        case 3:
            printf("Enter the initial principle, interest rate(as a decimal), and number of years for simple interest: \n");
            scanf(" %lf%lf%lf", &p, &i, &y);
            printf("Your principle would be valued at %lf\n", simpleinterest(p, i, y));
            break;
        default:
            printf("invalid entry\n");
            break;
        }
    }

    return 0;
}

int leapyears(int year) {
    if ((year % 4) == 0) {
        if ((year % 100) != 0) return 1;
        if ((year % 100) == 0) {
            if ((year % 400) == 0) {
                return 1;
            }
            else return 0;
        }
    }
    else return 0;
}

int monthlength(int month, int year) {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31;
    if (month == 4 || month == 6 || month == 9 || month == 11) return 30;
    if (leapyears(year) == 1 && month == 2) { 
        return 29; 
    }
    if (leapyears(year) == 0 && month == 2) {
        return 28;
    }
}

int numberofdays(int month, int day, int year) {
    day -= 1;
    while (year != 0001) {
        if (leapyears(year) == 1) {
            day += 366;
        }
        else{
            day += 365;
        }
        year -= 1;
    }
    while (month != 1) {
        month -= 1;
        day += monthlength(month, year);
    }
    return day;
}

double compoundinterest(double principle, double interest, double years) {
    float a = 1 + (interest);
    float b = pow(a, years);
    return principle * b;
}
double simpleinterest(double principle, double interest, double years) {
    return principle * (1 + (years * interest));
}

1 Ответ

2 голосов
/ 27 июня 2019

Вы включаете заголовки C ++ в свой файл C. Если вы удалите строку #include <iostream>, она должна исправить эти ошибки.

...