Loops?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace 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;
}
closed account (3CXz8vqX)
If? O.o nahhhhhhh

1
2
3
4
5
6
while ( response = 1)
{

    cin >> response;
    ...code
}


Take a look at while loops.
closed account (zb0S216C)
I would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main( )
{
  char LoopAgain_( 'y' );
  double Radius_( 0.0 );
  double const PI_( 3.14 );
  do
  {
    // Print something here...

    std::cin >> Radius_;
    std::cout << "Area:  " << ( ( Radius_ * Radius_ ) * PI ) << '\n';
    std::cout << "Circ:  " << ( ( 2 * PI_ ) * Radius_ ) << std::endl;
   
    std::cin >> LoopAgain_;
  } while( ( LoopAgain_ == 'y' ) || ( LoopAgain_ == 'Y' ) );

  return( 0 );
}

I haven't tested this code, so your milage may vary.

See here for loops: http://www.cplusplus.com/doc/tutorial/control/#loops

Wazzak
Last edited on
closed account (3CXz8vqX)
What's with the underscores?
closed account (zb0S216C)
It's how I name storage. Why? Problem?

Wazzak
Doesn't the while loop only do it a certain number of times. I am trying to follow your code, but I don't see where it goes back to the beginning.
closed account (zb0S216C)
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.

Wazzak
Last edited on
The program is giving me an error. Could you help me rearrange this to solve it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace 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;
}
closed account (3CXz8vqX)
@tripke

A while loop will loop infinitely.

1
2
3
4
while(true)
{
   cout << "hello_world!" << endl;
}


For loops restrict the number of times a loop can be run, but...that can be worked around...

Edit: Framework is right about the do while loops, I'm just a newb XD
Last edited on
@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');
Last edited on
closed account (3CXz8vqX)
Unless it's a single statement, for some reason C++ is rather forgiving about single line statements.
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
I think technically, keywords such as if, while etc. control just a single statement.

When you add braces { }, a group of one or more statements are then treated together as a single compound statement.
closed account (3CXz8vqX)
I get a feeling I've just been 1up'd.... =D
I guess this will work.

here a label is used along with the jump statement goto.Jump statements is used for transferring program control to a desired part of the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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;
   else if(response == 0)
        exit(0); 
   else     
         cout<<"wrong choice";
    return 0;
}


cheers,
cyber dude
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.
Last edited on
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
What is wrong with the compiler, why is it bad?

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++.

ok
Topic archived. No new replies allowed.