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!
/*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
This post has been edited by jjhaag: 30 Dec, 2007 - 12:49 AM
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.
/*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
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.
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.
/*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
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.
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).
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; }