C 编程问题

Problems with the C++ programming

本文关键字:问题 编程      更新时间:2023-10-16

我有以下文件。我正在尝试在2010年Visual Studios中运行它。但是它不断给我一个错误,说我需要包括标题文件stdafx.h。但是该文件在标题文件列表中无处可见。

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int choice();
double cylinder_volume();
double cone_volume();
double sphere_volume();
void display_result(double volume);
int main ()
{
int option;
double volume;
option=choice();
if (option==1)
    {
        volume=cylinder_volume();
        display_result(volume);
    }
else
    if (option==2)
    {
        volume=cone_volume();
        display_result(volume);
    }
else
    if (option==3)
    {
        volume=sphere_volume();
        display_result(volume);
    }
return 0;
}
int choice()
{
    int option;
    cout<<"What would you like to calculate the volume of: ";
    cout<<"nPress 1 for cylinder. ";
    cout<<"nPress 2 for cone. ";
    cout<<"nPress 3 for sphere. ";
    cin>>option;
    return option;
}
double cylinder_volume()
{
    const double pi=3.14159;
    double height,radius,volume;
    cout<<"Enter the height of the cylinder: ";
    cin>>height;
    cout<<"nEnter the radius of the cylinder: ";
    cin>>radius;
    volume=pi*radius*radius*height;
    return volume;
}
double cone_volume()
{
    const double pi=3.14159;
    double height,radius,volume;
    cout<<"Enter the height of the cone: ";
    cin>>height;
    cout<<"nEnter the radius of the cone: ";
    cin>>radius;
    volume=(1/3)*pi*radius*radius*height;
    return volume;
}
double sphere_volume()
{
    const double pi=3.14159;
    double radius,volume;
    cout<<"nEnter the radius of the sphere: ";
    cin>>radius;
    volume=(4/3)*pi*radius*radius*radius;
    return volume;
}
void display_result(double volume)
{
    cout<<volume;
}

这是因为MSVC默认使用预编译标头,并且名为" stdafx.h"。您应该转到" Project->属性",然后转到" C ->预编译标头",然后选择"不使用预编码标题"。下次您创建新项目时,您可能需要取消选中"使用预编译标题"复选框中的"项目创建向导"。