|
-
February 7th, 2014, 09:58 PM
#1
[Beginner] Looping - Circumference, Diameter, Area
Hey everyone! I'm really, really desperate now. My prof in Computer Science is of no help and is really rude so I'm stuck here not knowing what I did wrong.
Our assignment is:
Write a complete and correct C++ program that calculates and prints
the diameter, the circumference, or the area of a circle, given the
radius.
I have written a code but it won't run! I don't understand anymore... can someone help me?
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
//Declare the variables used
float area, circumference, areaOfCircle, pi, diameter;
bool getNumber = true;
char letter;
int r;
pi = 3.14159265;
while (true) //Enable the program to loop
{
//Prompt the user to type in a letter and a number
cout <<"Enter A for Area, C for Circumference, D for Diameter, Q to Quit; Followed by the radius: " << endl;
cin.get(letter); //Input any letter for option
//Will terminate if Q is typed
if (letter == 'Q')
{
cout << "Terminating as requested." << endl;
return 0; //Stop
}
while(getNumber)
{
if (!(cin >> r)
{
cout <<"Please enter numbers only." << endl;
cin.clear();
cin.ignore(10000, '\n');
}
else if getNumber = false;
if(r > 0)
{
if(letter == 'A')
{
areaOfCircle = pi*r*r;
cout <<"The area of the circle is" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << areaOfCircle << endl;
}
else if (letter == 'D')
{
diameter = 2*r;
cout <<"The diameter of a circle with a radius" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << diameter << endl;
}
else if (letter == 'C')
{
circumference = 2*pi*r;
cout <<"The circumference of a circle with a radius" << fixed << setprecision(5) << r << "is" << fixed << setprecision(5) << circumference << endl;
}
else
{
cout <<"No calculation performed. Invalid letter." << endl << endl;
}
}
else
{
cout <<"No calculation performed. Radius must be positive." << endl;
}
}
}
-
February 7th, 2014, 11:19 PM
#2
Re: [Beginner] Looping - Circumference, Diameter, Area
HI ..Dont be panic .. first break your prog into simple functions with set value of radius . Then add code to get input .. if u single step and debug u can remove errors
-
February 8th, 2014, 07:12 AM
#3
Re: [Beginner] Looping - Circumference, Diameter, Area
My prof in Computer Science is of no help and is really rude so I'm stuck here not knowing what I did wrong
Welcome! We've quite a friendly bunch here and we'll give you guidance and advice. 
See
http://forums.codeguru.com/announcement.php?f=7
http://forums.codeguru.com/showthrea...ork-assignment
Firstly though, when you post code, please format it properly first and use code tags. Go Advanced, select the code and click '#'.
First clue
Code:
else if getNumber = false;
are you sure about the if?
Second clue
count your opening and closing brackets. If the code was formatted properly with indents etc this would be more easily spotted.
Note that you should include cmath and not math.h for c++ programs. cmath has a macro definition for pi called M_PI, so you don't need to define pi in your program. Instead of
use
Code:
#define _USE_MATH_DEFINES
#include <cmath>
and then just use M_PI wherever you used your variable pi.
Once you have corrected the syntax errors and the program compiles, you will then need to debug it to correct a couple of logic (run time) problems.
Last edited by 2kaud; February 8th, 2014 at 08:47 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 8th, 2014, 07:42 AM
#4
Re: [Beginner] Looping - Circumference, Diameter, Area
 Originally Posted by cppnoob
I have written a code but it won't run! I don't understand anymore... can someone help me?
That's because you have syntax errors. What is the first error message that the compiler gives you? What's the corresponding line in your code? What is it about the error message that you don't understand?
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|