-4

I have this below file. I am trying to run it in visual studios 2010. But it keeps giving me an error saying that I need to include the header file stdafx.h. But this file is nowhere to be seen in the list of header files.

#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;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • It might mean to write an `#include` statement for it – PlasmaHH Feb 06 '14 at 17:37
  • Maybe read first, what stdafx.h is http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio – Martin Perry Feb 06 '14 at 17:38
  • -1 for the title! Change title/retag to get more specific, maybe?!? – πάντα ῥεῖ Feb 06 '14 at 17:48
  • I came across this question and I understand that answering a 2year old question is not going to be helpful but it may help anyone who is facing the same issue. I've modified your code a bit and tested in Visual C++ 2010 Express and Visual Studio 2015 Community and it works like charm. I'm attaching download link: https://drive.google.com/file/d/0BwCCbOj3fbZGNk5GTHlrR1lBUms/view?usp=sharing – Anurag Vasanwala Feb 08 '16 at 10:34

1 Answers1

5

That is because msvc uses precompiled header by default and it's named "stdafx.h". You should go to "Project->Properties" then to "C++->Precompiled header" and choose "Not using precompiled headers". Next time you create new projects you may want to uncheck "Use precompiled headers" checkbox in project creation wizard.

Alex Telishev
  • 2,264
  • 13
  • 15