Being very new at this, it is hard to know what to use where. I can output text and store integers fairly decent, however, I have little to no experience using loops.
What I want to happen is that if the use presses 0, the program will end. However, if the use presses 1 I would like the program to go back to the beginning so it can be run again. I am not entirely sure how to do this, and will the radius value still be stored from before? If so, how do I get rid of it?
#include <iostream>
usingnamespace std;
int main()
{
cout << "Hello! This simple program is designed to find the \n";
cout << "area and circumference of a circle. \n\n";
cout << "Please enter the radius of the circle: \n";
int radius;
cin >> radius;
float Pi = 3.14159;
float area = radius * radius * Pi;
float circumference = 2 * Pi * radius;
cout << "The area of the circle is: " << area << endl;
cout << "The circumference of the circle is: " << circumference << endl;
cout << "\n Would you like to use this program again? Press 0 for no, 1 for yes.";
int response;
cin >> response;
if ( response = 1)
{
return 0;
}
The only difference between "while" and "do while" is that "do while" tests the condition after the loop has executed. With a "do while" loop, when the loop starts for the first time (begins the first iteration), the condition is not tested until the first iteration has finished. With the "while" loop, however, the condition is tested before the first iteration starts.
After the first iteration of either loop construct, the loops behave the same way. However, in your program, you don't want to ask the user if they want to loop again if the loop never started in first place. So by using a "do while, you give the user their output and then ask them if they want to loop again.
#include <iostream>
usingnamespace std;
int main()
{
cout << "Hello! This simple program is designed to find the \n";
cout << "area and circumference of a circle. \n\n";
do
cout << "Please enter the radius of the circle: \n";
int radius;
cin >> radius;
float Pi = 3.14159;
char again;
float area = radius * radius * Pi;
float circumference = 2 * Pi * radius;
cout << "The area of the circle is: " << area << endl;
cout << "The circumference of the circle is: " << circumference << endl;
cout << "Do you want to do this again? ";
cin >> again;
while (again == 'y' || again ++ 'Y');
return 0;
}
@OP for the code you posted, you have to enclose the contents of the loop within brackets { }, in your example the first bracket { goes after do and the other one } goes before while(...);
So:
1 2 3 4
do
{
//Your code here...
} while (again == 'y' or again == 'Y');
That's right and C++ allows single statements without braces, because it's obvious that you want to have at least 1 statement, if you have more it can vary, so you have to tell the compiler which statements belong where
#include <iostream.h> // I think .h extension is important
#include<process.h> // for exit(0)
#include<conio.h> // for clrscr()
int main()
{
label: // label you can give any name to it
clrscr();
cout << "Hello! This simple program is designed to find the \n";
cout << "area and circumference of a circle. \n\n";
cout << "Please enter the radius of the circle: \n";
int radius;
cin >> radius;
float Pi = 3.14159;
float area = radius * radius * Pi;
float circumference = 2 * Pi * radius;
cout << "The area of the circle is: " << area << endl;
cout << "The circumference of the circle is: " << circumference << endl;
cout << "\n Would you like to use this program again? Press 0 for no, 1 for yes.";
int response;
cin >> response;
if ( response == 1)
goto label;
elseif(response == 0)
exit(0);
else
cout<<"wrong choice";
return 0;
}
Do not use goto, at least you shouldn't, because at some point you lose control of your program.
By the way iostream.h is for C iostream is the equivalent in C++
Anyway tripke's program was fine, it was simply missing braces.
Then there are a few other things I wouldn't use in cyberdude's version of the program, like conio.h - it's a non-standard library and its functionality depends on the compiler.
ya i have a real bad c++ compiler .could u suggest a good c++ compiler for 7 (other than code::block)/link for me.it would be really helpful....
nd thanks for the advice :)
cheers
cyber dude
If you refer to coni.h , that's neither here nor there. It is a non-standard feature, but its likely that whichever compiler you you use, it will offer some non-standard features.
The point is, you are not forced to use those features, instead just stick with standard C++.