| | |
Please help me figure out whats wrong with my code
![]() |
Alright well this is supposed to be a basic Geometry calculator that uses set formulas to identify the "chosen" property of the figure by just pluggin in #'s that are used in that formula.
I have to admit I'm not expirienced at all in C++ and probably made a ton of mistakes (my compiler agrees). I checked the code and it seems like for all operators that I input that are more than 2 digits that my compiler gives me a "multi-character character constant" error. I also have an = # of "comparison" errors meaning there is something that I am doing wrong every single time.
For one, I'm not even sure if your allowed to use parenthesis in C++ coding, and if so how do I implement them? Say we have the formula for the Volume of a sphere (4/3 pie Radius(cubed)) How do I implement that. Since you can't insert fractions (can you) how would I put them in, as decimals? If I put them in my compiler gives me an error, does this mean I need to give my declared variables a special characteristic (ie: short, long ect.)?
Thanks for your help guys, please don't just edit my code, also explain why if you could so I don't make the same mistake next time.
I have to admit I'm not expirienced at all in C++ and probably made a ton of mistakes (my compiler agrees). I checked the code and it seems like for all operators that I input that are more than 2 digits that my compiler gives me a "multi-character character constant" error. I also have an = # of "comparison" errors meaning there is something that I am doing wrong every single time.
For one, I'm not even sure if your allowed to use parenthesis in C++ coding, and if so how do I implement them? Say we have the formula for the Volume of a sphere (4/3 pie Radius(cubed)) How do I implement that. Since you can't insert fractions (can you) how would I put them in, as decimals? If I put them in my compiler gives me an error, does this mean I need to give my declared variables a special characteristic (ie: short, long ect.)?
Thanks for your help guys, please don't just edit my code, also explain why if you could so I don't make the same mistake next time.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { float area; float base; float Sbase; //Small Base (Trapezoid Formula) float Bbase; // Big Base (Trapezoid Formula) float Wbase; //Width of Base (Pyramid Formula) float Lbase; //Length of Base (Pyramid Formula) float circumference; float height; float length; float perimeter; float radius; float side; float sidea; float sideb; float width; char op; char again = 'y'; cout <<"\n Welcome to the Version 0.2 calculator by Niklas Kunkel"; cout <<" " <<endl; cout <<" " <<endl; cout <<"\n Here is a list of all current functions."<<endl; cout <<"Each funton will have an = sign and a number."<<endl; cout <<"Enter the number to use that function and then press enter"<<endl; cout <<"\n " <<endl; cout <<"\n "<<endl; while (again == 'y') { //here the user chooses what he wants to find out cout <<"FUNCTION LIST"<<endl; cout <<""<<endl; cout <<"\nArea of a Square = 1"<<endl; cout <<"Perimter of a Square = 2"<<endl; cout <<"Length of a side of a square = 3"<<endl; cout <<"\nArea of a Rectangle = 4"<<endl; cout <<"Perimeter of a Rectangle = 5"<<endl; cout <<"\nArea of a Circle = 6"<<endl; cout <<"Radius of a Circle = 7"<<endl; cout <<"Circumference of a Cirlce = 8"<<endl; cout <<"\nArea of a Parallelogram = 9"<<endl; cout <<"Perimeter of a Parallelogram = 10"<<endl; cout <<"\nArea of a Trapezoid = 11"<<endl; cout <<"\nVolume of a Cylinder = 12"<<endl; cout <<"Lateral Surface Area of a Cylinder = 13"<<endl; cout <<"\nVolume of a Cone = 14"<<endl; cout <<"Later Surface Area of a Cone = 15"<<endl; cout <<"\nVolume of a Pyramid = 16"<<endl; cout <<"\nVolume of a Rectangular Solid (3d) = 17"<<endl; cout <<"Area of a Rectangular Solid (3d) = 18" <<endl; cout <<"\nVolume of a Sphere = 19"<<endl; cout <<"Surface Area of a Sphere = 20"<<endl; cin >> op; if (op == '1') { cout <<"\nPlease enter the length of any side of the Square"<<endl; cin >> side; cout <<"\n Area of the Square = " << side * side <<endl; } if (op == '2') { cout <<"\n Please enter the length of any side of the Square"<<endl; cin >> side; cout <<"\n Perimtere of the Square = " << side + side + side + side <<endl; } if (op == '3') { cout <<"\n Please enter the area of the square"<<endl; cin >> area; cout <<"\n Length of the side of the square = " << area / 4 <<endl; } if (op == '4') { cout <<" \nPlease enter the Length of the Rectangle"<<endl; cin >> length; cout <<"PLease enter the Width of the Rectangle"<<endl; cin >> width; cout <<"Area of the Rectangle = " << length * width <<endl; } if (op == '5') { cout <<" \nPlease enter the Length of the Rectangle"<<endl; cin >> length; cout <<"Please enter the Width of the Rectangle"<<endl; cin >> width; cout <<"Perimeter of the Rectangle = " << length + length + width + width<<endl; } if (op == '6') { cout <<"\nPlease enter the radius of the Circle"<<endl; cin >> radius; cout <<"\nArea of the Circle = " << radius * radius * 3.14<<endl; } if (op == '7') { cout <<"\nPlease enter the Area of the Circle"<<endl; cin >>area; cout <<"Radius of the Circle = " << area / 3.14<<endl; } if (op == '8') { cout <<"\nPlease enter the Radius of the Cirlce"<<endl; cin >> radius; cout <<"Circumference of the Circle = " << radius * 2 * 3.14<<endl; } if (op == '9') { cout <<"\nPlease enter the Base of the Parallelogram"<<endl; cin >> base; cout <<"Please enter the Height of the Parallelogram"<<endl; cin >> height; cout <<"Area of the Paralleogram = " << base * height<<endl; } if (op == '10') { cout <<"\nPlease enter the length of one side of the Parallelogram"<<endl; cin >> sidea; cout <<"Please enter the length of another side of the Paralellogram that is not parallel to the first"<<endl; cin >> sideb; cout <<"Perimete of the Paralleogram = " << sidea + sidea + sideb + sideb <<endl; } if (op == '11') { cout <<"\n Please enter the Height"<< endl; cin >> height; cout <<"Please enter the length of the smaller base"<<endl; cin >> sidea; cout <<"Please enter the length of the larger base"<<endl; cin >> sideb; cout <<"The Area of the Trapezoid = " << .5 * height * (sidea + sideb) <<endl; } if (op == '12') { cout <<"\nPlease enter the radius of the cylinder"<<endl; cin >> radius; cout <<"Please enter the height of the cylinder"<<endl; cin >> height; cout <<"Volume of the Cylinder = " << radius * radius * height * 3.14 <<endl; } if (op == '13') { cout <<"\nPlease enter the radius of the cylinder"<< endl; cin >> radius; cout <<"Please enter the height of the cylinder"<<endl; cin >> height; cout <<" Lateral Surface Area of the Cylinder = " << 2 * 3.14 * radius * height <<endl; } if (op == '14') { cout <<"\nPlease enter the radius of the cylinder"<<endl; cin >> radius; cout <<"Please enter the height of the cylinder"<<endl; cin >> height; cout <<"Volume of the Cone = " .3 * 3.14 * radius * radius * height <<endl; } if (op == '15') { cout <<"\nPlease enter the height" <<endl; cin >> height; cout <<"Please enter the radius" <<endl; cin >> radius; cout <<"Later Surface Area of the Cone = " << (radius * radius) + (height * height) <<endl; } if (op == '16') { cout <<"\nPlease enter the length of the pyramid" <<endl; cin >> length; cout <<"Please enter the the height of the pyramid" <<endl; cin >> height; cout <<"Please enter the width of the pyramid" <<endl; cin >> width; cout <<\n"Volume of the Pyramid = " << .33(length * width * height) <<endl; } if (op == '17') { cout <<"\nPlease enter the length of the Rectangular Solid" <<endl; cin >> length; cout <<"Please enter the the height of the Rectangular Solid" <<endl; cin >> height; cout <<"Please enter the width of the Rectangular Solid" <<endl; cin >> width; cout <<"\nVolume of the Rectangular Solid = " << length * width * height <<endl; } if (op == '18') { cout <<"\nPlease enter the length of the Rectangular Solid" <<endl; cin >> length; cout <<"Please enter the the height of the Rectangular Solid" <<endl; cin >> height; cout <<"Please enter the width of the Rectangular Solid" <<endl; cin >> width; cout <<"\nArea of the Rectangular Solid = " << 2(length * width) + 2(length * height) + 2(width * height) <<endl; } if (op == '19') { cout <<"\nPlease enter the radius" <<endl; cin >> radius; cout <<"\nVolume of the Sphere = " << radius * radius * radius * 3.14 * 1.33 <<endl; } if (op == '20') { cout <<"\nPlease enter the radius" <<endl; cin >> radius; cout <<"\nSurface Area of the Sphere = " << radius * radius * 4 * 3.14 <<endl; } else cout << "\n Invalid Operation"; cout << "\n Do you want to try again? (y/n): "; cin >> again; } return 0; }
Too long to make all the corrections. Make corrections for number 1 and number 19. You can understand as I have commented the changes. Do the others yourself and post if you have other problems.
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; const double PI = 3.1415; // The constant pie. You may know that you can't output this as a fraction as pie has infinite decimals. int main() { float area; float base; float Sbase; //Small Base (Trapezoid Formula) float Bbase; // Big Base (Trapezoid Formula) float Wbase; //Width of Base (Pyramid Formula) float Lbase; //Length of Base (Pyramid Formula) float circumference; float height; float length; float perimeter; float radius; float side; float sidea; float sideb; float width; int op; // make op an int. if it is a char it can only hold one character. So you can't input 10, 11, 12, .... That is why you got the multi character errors. char again = 'y'; /* Removed your greetings to reduce number of code lines. Add them again */ while (again == 'y') { //here the user chooses what he wants to find out cout <<"FUNCTION LIST"<<endl; cout <<""<<endl; cout <<"\nArea of a Square = 1" <<endl; /* Similar couts for the other functions */ cout <<"\nVolume of a Sphere = 19"<<endl; cout <<"Surface Area of a Sphere = 20"<<endl; cin >> op; // Rather than using nested if else if statements, you could use a case statement too. // But I left it as it is. if (op == 1)// Notice that I have removed the quotes because now op is an int. { cout <<"\nPlease enter the length of any side of the Square"<<endl; cin >> side; cout <<"\n Area of the Square = " << side * side <<endl; } // Added else if. else if (op == '2')// Make changes as for 1. { /*... */ } // Added else if. else if (op == 19) { cout <<"\nPlease enter the radius" <<endl; cin >> radius; cout <<"\nVolume of the Sphere = " << radius * radius * radius * PI * 4 / 3 <<endl; // Of course you can input fractions. :p. See that I have used PI also. } else // Added {}. It is good practice and makes the code clearer. { cout << "\n Invalid Operation"; } cout << "\n Do you want to try again? (y/n): "; cin >> again; } return 0; }
バルサミコ酢やっぱいらへんで
I found the following:
C++ needs you to input all the characters, you can't say ab and mean a*b, the compiler won't understand you... This also appears in your program at:
here:
here:
and here:
Should compile fine now!
gr Niek
•
•
•
•
if (op == '14') { cout <<"\nPlease enter the radius of the cylinder"<<endl; cin >> radius; cout <<"Please enter the height of the cylinder"<<endl; cin >> height; cout <<"Volume of the Cone = " << (<--forget these) .3 * 3.14 * radius * radius * height <<endl; }
•
•
•
•
if (op == '16') { cout <<"\nPlease enter the length of the pyramid" <<endl; cin >> length; cout <<"Please enter the the height of the pyramid" <<endl; cin >> height; cout <<"Please enter the width of the pyramid" <<endl; cin >> width; cout <<"\n (this \n was placed wrong) Volume of the Pyramid = " << .33 * (length * width * height) <<endl; }
•
•
•
•
C++ Syntax (Toggle Plain Text)
cout << "Volume of the Pyramid = " << .33 * (length * width * height) <<endl;
here:
•
•
•
•
cout <<"\nArea of the Rectangular Solid = " << 2 * (length * width) + 2 * (length * height) + 2 * (width * height) <<endl;
•
•
•
•
cout <<"\nArea of the Rectangular Solid = " << 2 * (length * width) + 2 * (length * height) + 2 * (width * height) <<endl;
•
•
•
•
cout <<"\nArea of the Rectangular Solid = " << 2 * (length * width) + 2* (length * height) + 2 * (width * height) <<endl;
gr Niek
Last edited by niek_e; Oct 31st, 2006 at 3:26 am.
Wow thank you so much. Though the fractions had to be implemented somehow in C++, just didn't know I had to declare the operation that was supposed to be distributed into them.
Anyways, thanks a ton guys! All I have left now is a slight parse error , which I should find if I got nit picking through my code again. Compiler says line 177 but that thing is always off by at least 30 lines
Thanks again, you guys always seems to be there when I need something solved fast and reliably.
EDIT: Nm found the parse error: Forgot to add a << between the cout text after the "" and the formula.
EDIT 2: Woops found a small error in one of the formulas. When trying to find the radius of a circle if I have the area, I enter the area / PI but then I have to get the square root of the number that is output. What character is used to display the square root function in C++?
Anyways, thanks a ton guys! All I have left now is a slight parse error , which I should find if I got nit picking through my code again. Compiler says line 177 but that thing is always off by at least 30 lines
Thanks again, you guys always seems to be there when I need something solved fast and reliably.
EDIT: Nm found the parse error: Forgot to add a << between the cout text after the "" and the formula.
EDIT 2: Woops found a small error in one of the formulas. When trying to find the radius of a circle if I have the area, I enter the area / PI but then I have to get the square root of the number that is output. What character is used to display the square root function in C++?
Last edited by Niklas; Oct 31st, 2006 at 4:09 am.
I did notice that you did screw up the string of output, by putting quotes in the wrong places, in line 190, op=16:
You CAN just put in fractions in your formulas, preferably in parentheses so the order of operations is more clear.
As for the square root operation, that is
cout <<"\nVolume of the Pyramid = " is what I had to change it to.You CAN just put in fractions in your formulas, preferably in parentheses so the order of operations is more clear.
As for the square root operation, that is
sqrt(double); which is in the cmath library. Damn computer! It ate everything!
![]() |
Similar Threads
- Why Wont my If Work (C++)
- C problem in strings and recursive check (C)
- Great what did I miss? (C++)
- Whats wrong with my computer??? (Viruses, Spyware and other Nasties)
- Whats wrong with this class??? (C++)
- Whats wrong with this code (PHP)
- Hello Everyone, I need Help with my C++ Code... (C++)
Other Threads in the C++ Forum
- Previous Thread: FindWindow Problems (on Windows 98/ME)
- Next Thread: what doesnt work? plz help guys
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






