|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
If Do statement + more, need help
Ok, Im a beginner using C++ as my first language. I use the program dev c++. Im trying to create my first program by myself but I, it calculates the area and perimeter of the square, circle and triangle. However, I don't know how to make it pick the shape. I've been trying to make it do:
pick: 1 , 2 or 3 and then when you pick it, the program goes to the correct equation to solve it. anyone mind helping me? This is my script. The "if (shape = 1)" does nothing. #include<iostream> using namespace std; int main() { system("TITLE Hanlon's Geometric Calculator"); system("COLOR 2"); char eChar; int shape; double dnumber1; double dnumber2; double dnumber3; double dnumber4; double dradius; char cDoagain; do { system("CLS"); cout << "What Type Of Geometric Shape" << " // 1.Square // 2.Triangle // 3.Circle // " << endl; cin >> shape; if (shape == 1); { cout << "Enter Side 1: " << endl; cin >> dnumber1; cout << "Enter Side2: " << endl; cin >> dnumber2; cout << "The Area Is: " << dnumber1 << " * " << dnumber2 << " = " << (dnumber1 * dnumber2) << endl; cout << "The Perimeter Is: " << dnumber1 << " *2 + " << dnumber2 << " *2 = " << ((dnumber1 *2) + (dnumber2 *2)) << endl; } if (shape == 2); { cout << "Enter Base: " << endl; cin >> dnumber1; cout << "Enter Height: " << endl; cin >> dnumber2; cout << "Enter Side 1: " << endl; cin >> dnumber3; cout << "Enter Side 2: " << endl; cin >> dnumber4; cout << "The Area Is: " << dnumber1 << " * " << dnumber2 << " /2 " << " = " << ((dnumber1 * dnumber2)/2) << endl; cout << "The Perimeter Is: " << dnumber1 << " + " << dnumber3 << " + " << dnumber4 << " = " << (dnumber1 + dnumber2 + dnumber3) << endl; } if (shape == 3); { cout << "Enter Radius: " << endl; cin >> dradius; cout << "The Area Is: " <<" 3.14159265 " << " * " << dradius << " ^2 " << " = " << (3.14159265 * dradius * dradius) << endl; cout << "The Perimeter Is: " << " 2 " << " * " << " 3.14159265 " << " * " << dradius << " = " << (2 * 3.14159265 * dradius) << endl; } cout << "Wanna Start Again? (y or n)" << endl; cin >> cDoagain; }while (cDoagain == 'Y' || cDoagain =='y'); system("PAUSE"); return 0; } Thanks Alot |
|
#2
|
||||
|
||||
|
1. Use code tags to retain your program's indentation. It's unreadable otherwise. If you continue in your sinful ways, then you will be constantly castigated for your transgressions and rightfully so.
2. Read the "READ THIS FIRST" sticky. That's what it's there for. Duh? 3. You're not writing a script; you're writing a program. C++ is not a scripting language. I can't imagine why you would think that it is. Your if (shape = 1) would be wrong, since it assigns the value of one to shape. But at least you don't repeat that mistake in your code, where you properly write it as if (shape == 1) and which is the comparison operation. Also, you should be doing if () {} else if {} else if {} else {}. Or using a switch statement. BTW, that else at the end is to handle the case where the user enters something that's not 1, 2, or 3. Users are undisciplined onery critters who rarely provide proper input; sizablegrin, a seasoned and grizzly denizen of these-here parts, will inform you in no uncertain terms that you must always be ready to handle totally bogus input. But the killer is that none of your if-statements have any body, as you can see highlighted in red: Code:
if (shape == 1);
{
cout << "Enter Side 1: " << endl;
cin >> dnumber1;
cout << "Enter Side2: " << endl;
cin >> dnumber2;
cout << "The Area Is: " << dnumber1 << " * " << dnumber2 << " = " <<
(dnumber1 * dnumber2) << endl;
cout << "The Perimeter Is: " << dnumber1 << " *2 + " << dnumber2 <<
" *2 = " << ((dnumber1 *2) + (dnumber2 *2)) << endl;
}
if (shape == 2);
{
cout << "Enter Base: " << endl;
cin >> dnumber1;
cout << "Enter Height: " << endl;
cin >> dnumber2;
cout << "Enter Side 1: " << endl;
cin >> dnumber3;
cout << "Enter Side 2: " << endl;
cin >> dnumber4;
cout << "The Area Is: " << dnumber1 << " * " << dnumber2 << " /2 " <<
" = " << ((dnumber1 * dnumber2)/2) << endl;
cout << "The Perimeter Is: " << dnumber1 << " + " << dnumber3 <<
" + " << dnumber4 << " = " << (dnumber1 + dnumber2 + dnumber3)
<< endl;
}
if (shape == 3);
{
cout << "Enter Radius: " << endl;
cin >> dradius;
cout << "The Area Is: " <<" 3.14159265 " << " * " << dradius <<
" ^2 " << " = " << (3.14159265 * dradius * dradius) << endl;
cout << "The Perimeter Is: " << " 2 " << " * " << " 3.14159265 " <<
" * " << dradius << " = " << (2 * 3.14159265 * dradius) << endl;
}
By placing those semicolons right after the if condition, you tell the compiler that that's the end of the if-statement. That means that those code blocks that you had intended as being the bodies of the bodies of those if-statements -- well, the complier does not see them as such, but rather as code blocks to be executed unconditionally. Which means that they would have all been executed. Which means that each and every one of those shapes would have been executed. And you neglected to provide us that very valuable piece of information! That wasn't very nice of you. When you have a problem to be solved, one which generates some kind of error, then you need to inform us of that error. If I had a nickel for every time we had to remind a newbie that we don't read minds, I could have retired long ago. We do not read minds. You have to tell us these things (something that my ex-wife could never understand). PS Just in case your reading comprehension is not up to snuff, lose those red semicolons. Last edited by dwise1_aol : May 7th, 2008 at 01:47 AM. |
|
#3
|
|||
|
|||
|
Re:
Srry, like I said Im a noobie, just making noobie mistakes
but thx this helped alot, I finished the program ![]() and it wasnt the fact that it didn't work, just couldnt make it pick a shape thx again |
|
#4
|
||||
|
||||
|
Keep in mind what you need to do when you make another post in this forum. Others won't be anywhere near as nice as I was.
Ie, it doesn't take much to play nice around here. But you'll definitely be told when you're not playing nice. |
|
#5
|
||||
|
||||
|
I think I see the problem. We should rename the thread:
New users - HOW TO POST A QUESTION - READ THIS FIRST to Noobie - HOW TO POST A QUESTION - READ THIS FIRST
__________________
C/C++ pointers (Original in the "Commonly Asked Questions" thread). |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > If Do statement + more, need help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|