Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,932 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,754 people online right now. Registration is fast and FREE... Join Now!




it's not working right! help please! =)

 
Reply to this topicStart new topic

it's not working right! help please! =)

krisbam_yueying
30 Dec, 2007 - 12:30 AM
Post #1

New D.I.C Head
*

Joined: 25 Dec, 2007
Posts: 2

/*this program is suppose to compute for the area, or perimeter, of a triangle, circle, or rectangle depending on what the user wants to compute...
but its not working the way it should be.. when the user gives the input.. it does not proceed to the next instead, it automatically exits which is not right.

there are no errors, it's just not working right. and i also have another problem: i don't know how to "end" the program when the user wants to end it.

could somebody please help me?*/

CODE
#include<iostream>
using namespace std;
int main ()
{
char ans, a, b, c;
double pi;
float base, height, length, width, radius, d, e, f, g, h, i, s1, s2, s3;
int p = int (pi);
cout<<"What do you want to compute for?\n";
cout<<"(a)area, (b ) perimeter? Or if u want to exit just press C.\n";
cin>>ans;

if (ans==a )
   {
          cout<<"Choose the type of polygon.\n";
          cout<<"(a)triangle, (B) circle, (c )rectangle\n";
          cin>>ans;
          if (ans==a )
                  {
                    cout<<"What is the value of the base?\n";
                    cin>>base;
                    cout<<"\nWhat is the value of the height?\n";
                    cin>>height;
                    d= base*height/2;
                    cout<<"\nThe area of the triangle is"<< d<<".";
                  }
          else if (ans==b )
                  {
                  cout<<"What is the value of the radius?\n";
                  cin>>radius;
                  pi=3.1416;
                  e=p*radius*radius;
                  cout<<"\nThe area of the circle is"<< e<<".";
                  }
          else
                  {
                  cout<<"What is the value of the length?\n";
                  cin>>length;
                  cout<<"\nWhat is the value of the width?\n";
                  cin>>width;
                  f=length*width;
                  cout<<"\nThe area of the rectangle is"<< f<<".";
                  }
   }
else if (ans==b )
   {
        cout<<"Choose the type of polygon.\n";
        cout<<"(a)triangle, (b)circle, (c )rectangle\n";
        cin>>ans;
        if (ans==a )
                {
                cout<<"What is the length of the first side?\n";
                cin>>s1;
                cout<<"\nHow about the second side?\n";
                cin>>s2;
                cout<<"\nThen how about the third side?\n";
                cin>>s3;
                g=s1+s2+s3;
                cout<<"\nThe perimeter of the triangle is"<< g<<".";    
                }
        else if (ans==b )
                {
                cout<<"What is the value of the radius?\n";
                cin>>radius;
                pi=3.1416;
                h=2*p*radius;
                cout<<"\nThe circumference / perimeter of the circle is"<< h<<".";
                }
        else    
                {
                cout<<"What is the value of the length?\n";
                cin>>length;
                cout<<"\nWhat is the value of the width?\n";
                cin>>width;
                i=(2*length)+(2*width);
                cout<<"\nThe perimeter of the rectangle is"<< i<<".";
                }
   }

return 0;
}


*EDIT - please use code tags when posting source code, like this code.gif

This post has been edited by jjhaag: 30 Dec, 2007 - 12:49 AM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: It's Not Working Right! Help Please! =)
30 Dec, 2007 - 12:52 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Please do not create a new thread dealing with the same source code, especially when you have ignored the suggestions for fixing your code from the original thread. See your previous thread for some things that need to be changed.
User is offlineProfile CardPM
+Quote Post

young2daniel
RE: It's Not Working Right! Help Please! =)
2 Jan, 2008 - 04:20 AM
Post #3

New D.I.C Head
*

Joined: 29 Dec, 2007
Posts: 23


My Contributions
QUOTE(krisbam_yueying @ 30 Dec, 2007 - 01:30 AM) *

/*this program is suppose to compute for the area, or perimeter, of a triangle, circle, or rectangle depending on what the user wants to compute...
but its not working the way it should be.. when the user gives the input.. it does not proceed to the next instead, it automatically exits which is not right.

there are no errors, it's just not working right. and i also have another problem: i don't know how to "end" the program when the user wants to end it.

could somebody please help me?*/

CODE
#include<iostream>
using namespace std;
int main ()
{
char ans, a, b, c;
double pi;
float base, height, length, width, radius, d, e, f, g, h, i, s1, s2, s3;
int p = int (pi);
cout<<"What do you want to compute for?\n";
cout<<"(a)area, (b ) perimeter? Or if u want to exit just press C.\n";
cin>>ans;

if (ans==a )
   {
          cout<<"Choose the type of polygon.\n";
          cout<<"(a)triangle, (B) circle, (c )rectangle\n";
          cin>>ans;
          if (ans==a )
                  {
                    cout<<"What is the value of the base?\n";
                    cin>>base;
                    cout<<"\nWhat is the value of the height?\n";
                    cin>>height;
                    d= base*height/2;
                    cout<<"\nThe area of the triangle is"<< d<<".";
                  }
          else if (ans==b )
                  {
                  cout<<"What is the value of the radius?\n";
                  cin>>radius;
                  pi=3.1416;
                  e=p*radius*radius;
                  cout<<"\nThe area of the circle is"<< e<<".";
                  }
          else
                  {
                  cout<<"What is the value of the length?\n";
                  cin>>length;
                  cout<<"\nWhat is the value of the width?\n";
                  cin>>width;
                  f=length*width;
                  cout<<"\nThe area of the rectangle is"<< f<<".";
                  }
   }
else if (ans==b )
   {
        cout<<"Choose the type of polygon.\n";
        cout<<"(a)triangle, (b)circle, (c )rectangle\n";
        cin>>ans;
        if (ans==a )
                {
                cout<<"What is the length of the first side?\n";
                cin>>s1;
                cout<<"\nHow about the second side?\n";
                cin>>s2;
                cout<<"\nThen how about the third side?\n";
                cin>>s3;
                g=s1+s2+s3;
                cout<<"\nThe perimeter of the triangle is"<< g<<".";    
                }
        else if (ans==b )
                {
                cout<<"What is the value of the radius?\n";
                cin>>radius;
                pi=3.1416;
                h=2*p*radius;
                cout<<"\nThe circumference / perimeter of the circle is"<< h<<".";
                }
        else    
                {
                cout<<"What is the value of the length?\n";
                cin>>length;
                cout<<"\nWhat is the value of the width?\n";
                cin>>width;
                i=(2*length)+(2*width);
                cout<<"\nThe perimeter of the rectangle is"<< i<<".";
                }
   }

return 0;
}


*EDIT - please use code tags when posting source code, like this code.gif










I think you should use a switch statement for your menus. Also create functions for the triangle, circle and rectangle.
Then you call the functions for each case. Hope it works. Bye.

To end your program use the break; command. Bye
User is offlineProfile CardPM
+Quote Post

Delta_Echo
RE: It's Not Working Right! Help Please! =)
4 Jan, 2008 - 04:53 AM
Post #4

D.I.C Regular
***

Joined: 24 Oct, 2007
Posts: 480


My Contributions
exit (1); will terminate the program. I think you have to include <stdlib.h>
OR <cstdlib.h> but im not sure if <cstdlib.h> even exists...
try them both. smile.gif
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: It's Not Working Right! Help Please! =)
4 Jan, 2008 - 06:13 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,351



Thanked: 51 times
Dream Kudos: 25
My Contributions
stdlib.h is a c library - <cstdlib> is the corresponding C++ header.
User is online!Profile CardPM
+Quote Post

Tom9729
RE: It's Not Working Right! Help Please! =)
4 Jan, 2008 - 03:17 PM
Post #6

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,589



Thanked: 12 times
Dream Kudos: 325
My Contributions
QUOTE(young2daniel @ 2 Jan, 2008 - 05:20 AM) *

QUOTE(krisbam_yueying @ 30 Dec, 2007 - 01:30 AM) *

/*this program is suppose to compute for the area, or perimeter, of a triangle, circle, or rectangle depending on what the user wants to compute...
but its not working the way it should be.. when the user gives the input.. it does not proceed to the next instead, it automatically exits which is not right.

there are no errors, it's just not working right. and i also have another problem: i don't know how to "end" the program when the user wants to end it.

could somebody please help me?*/

CODE
#include<iostream>
using namespace std;
int main ()
{
char ans, a, b, c;
double pi;
float base, height, length, width, radius, d, e, f, g, h, i, s1, s2, s3;
int p = int (pi);
cout<<"What do you want to compute for?\n";
cout<<"(a)area, (b ) perimeter? Or if u want to exit just press C.\n";
cin>>ans;

if (ans==a )
   {
          cout<<"Choose the type of polygon.\n";
          cout<<"(a)triangle, (B) circle, (c )rectangle\n";
          cin>>ans;
          if (ans==a )
                  {
                    cout<<"What is the value of the base?\n";
                    cin>>base;
                    cout<<"\nWhat is the value of the height?\n";
                    cin>>height;
                    d= base*height/2;
                    cout<<"\nThe area of the triangle is"<< d<<".";
                  }
          else if (ans==b )
                  {
                  cout<<"What is the value of the radius?\n";
                  cin>>radius;
                  pi=3.1416;
                  e=p*radius*radius;
                  cout<<"\nThe area of the circle is"<< e<<".";
                  }
          else
                  {
                  cout<<"What is the value of the length?\n";
                  cin>>length;
                  cout<<"\nWhat is the value of the width?\n";
                  cin>>width;
                  f=length*width;
                  cout<<"\nThe area of the rectangle is"<< f<<".";
                  }
   }
else if (ans==b )
   {
        cout<<"Choose the type of polygon.\n";
        cout<<"(a)triangle, (b)circle, (c )rectangle\n";
        cin>>ans;
        if (ans==a )
                {
                cout<<"What is the length of the first side?\n";
                cin>>s1;
                cout<<"\nHow about the second side?\n";
                cin>>s2;
                cout<<"\nThen how about the third side?\n";
                cin>>s3;
                g=s1+s2+s3;
                cout<<"\nThe perimeter of the triangle is"<< g<<".";    
                }
        else if (ans==b )
                {
                cout<<"What is the value of the radius?\n";
                cin>>radius;
                pi=3.1416;
                h=2*p*radius;
                cout<<"\nThe circumference / perimeter of the circle is"<< h<<".";
                }
        else    
                {
                cout<<"What is the value of the length?\n";
                cin>>length;
                cout<<"\nWhat is the value of the width?\n";
                cin>>width;
                i=(2*length)+(2*width);
                cout<<"\nThe perimeter of the rectangle is"<< i<<".";
                }
   }

return 0;
}


*EDIT - please use code tags when posting source code, like this code.gif










I think you should use a switch statement for your menus. Also create functions for the triangle, circle and rectangle.
Then you call the functions for each case. Hope it works. Bye.

To end your program use the break; command. Bye

I might be wrong, but I think break only breaks out of a loop.
User is online!Profile CardPM
+Quote Post

jjhaag
RE: It's Not Working Right! Help Please! =)
4 Jan, 2008 - 03:34 PM
Post #7

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE(Tom9729 @ 4 Jan, 2008 - 04:17 PM) *

I might be wrong, but I think break only breaks out of a loop.

You are quite correct, sir.

exit() will abort the program, while return can be used to exit the program from main. So exit allows you to leave the entire program from within a function other than main(), while return would just return you to the calling function (except if the function is main(), in which case a return statement will also exit the program).
User is offlineProfile CardPM
+Quote Post

baavgai
RE: It's Not Working Right! Help Please! =)
4 Jan, 2008 - 07:13 PM
Post #8

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
You have a few things going on here.

Your biggest problem is this (ans==a ). Where you're saying here is does the value in variable ans equal the value in variable a. You never initialize a, so you're never going to get what you want from this. It should read (ans=='a').

Reusing the var ans actually compounds confusion, since you must reassign it with the logic. Actually, declaring all those variables up front leads to errors, because it's unclear where they're getting used. A good rule to use, declare your variables in as narrow a scope as possible and try not to use those variables within that scope.

Here's my version of your code. I've cleaned up the errors, by left it pretty much the same. Please make sure you understand what was done.

CODE

#include<iostream>

using namespace std;

const float pi=3.1416;


char get_poly_type() {
    char poly_type;
    cout<<"Choose the type of polygon.\n";
    cout<<"(a)triangle, (b) circle, (c)rectangle\n";
    cin>>poly_type;
    return poly_type;
}

void get_length_width(float &length, float &width) {
    cout<<"What is the value of the length?\n";
    cin>>length;
    cout<<"\nWhat is the value of the width?\n";
    cin>>width;
}

bool menu() {
    float result;
    
    cout<<"What do you want to compute for?\n";
    cout<<"(a)area, (b) perimeter? Or if u want to exit just press c.\n";
    char compute_for;
    cin>>compute_for;
    
    if (compute_for=='a') {
        char poly_type = get_poly_type();
        if (poly_type=='a') {
            float base, height;
            cout<<"What is the value of the base?\n";
            cin>>base;
            cout<<"\nWhat is the value of the height?\n";
            cin>>height;
            result = base*height/2;
            cout<<"\nThe area of the triangle is"<< result<<".";
        } else if (poly_type=='b') {
            float radius;
            cout<<"What is the value of the radius?\n";
            cin>>radius;
            result = pi*radius*radius;
            cout<<"\nThe area of the circle is"<< result<<".";
        } else if (poly_type=='c') {
            float length, width;
            get_length_width(length, width);
            result=length*width;
            cout<<"\nThe area of the rectangle is"<< result <<".";
        }
        return true;
    }
    
    if (compute_for=='b') {
        char poly_type = get_poly_type();
        if (poly_type=='a') {
            float s1, s2, s3;
            cout<<"What is the length of the first side?\n";
            cin>>s1;
            cout<<"\nHow about the second side?\n";
            cin>>s2;
            cout<<"\nThen how about the third side?\n";
            cin>>s3;
            result=s1+s2+s3;
            cout<<"\nThe perimeter of the triangle is"<< result<<".";    
        } else if (poly_type=='b') {
            float radius;
            cout<<"What is the value of the radius?\n";
            cin>>radius;
            result=2*pi*radius;
            cout<<"\nThe circumference / perimeter of the circle is"<< result<<".";
        } else if (poly_type=='c') {
            float length, width;
            get_length_width(length, width);
            result =(2*length)+(2*width);
            cout<<"\nThe perimeter of the rectangle is"<< result<<".";
        }
        return true;
    }
    
    return false;
}

int main () {
    // repleat until not a or b
    while(menu());
    return 0;
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 03:24PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month