Sponsored By

Dan

Program Just Shutting Down....

Recommended Posts

Hey everyone, can you guys tell me why my C++ proggie just shuts down randomly?

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

int main(int argc, char *argv[])
{
         int choice1;
cout <<"Please enter 1 for volume of a rectangle, or 2 for a cone: ";
 cin >> choice1;

 switch ( choice1 ) {
 case '1':

           float length1;
          cout <<"Please enter the Length of the object: \n";
          cin >> length1;
           float width1;
          cout <<"\nPlease enter the Width of the object: ";
          cin >> width1;

          float lenwit;

          lenwit = length1 * width1;    //lenth times width

          float height1;

          cout <<"\nPlease enter the Height of the object: ";
          cin >> height1;

          float volume1;

          volume1 = lenwit * height1;      //to get volume

          cout <<"\nThe volume of the object is << volume1";

          break;

case '2':

       float diam;
       float rad;
       float rad2;
       float rad3;
       float rad4;

       cout <<"\nEnter the diameter: ";
       cin >> diam;

       rad = diam/2;

       rad2 = rad * rad;

       rad3 = rad2 * 3.14;

       rad4 = rad3*3;

       float height2;
       cout <<"\nEnter the Height: ";
       cin >> height2;

       float vol2;

       vol2 = rad4 * height2;

       cout <<"\nThe Volume is: << vol2";
      break;
       }
     system("PAUSE");
 return 0;
}

Share this post


Link to post
Share on other sites

Well, unrelated to your problem, you are using identifiers form the std namespace without including said namespace.

using namespace std;

after the includes should fix that.

As far as I can tell, you aren't using stdio (and if you were you should include cstdio not stdio.h) so you don't need to include it.

The problems are that you have no error checking and reporting (what happens if the user doesn't specify one of the options available?) and secondly (which would have been much easier to figure out with the first problem solved) you are reading as an int. So when the user types in a number, the int is given that number as a value. You then compare it to characters, which are admitedly ints, but the wrong ones. Those single quotes make a BIG difference. '1' is 49, '2' is 50. If you removed the quotes OR used a char variable instead, it would work.

There are realy much better languages to start out with than C++. Ruby or Python might serve you better for getting you familiar with programming.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...