Very simple Loops problem: Need help

I am really trying to understand how a do while loop works, but I cannot figure out the correct configuration.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int main()
{
  char again;
    do
    cout << "Hello C++ Forums!"
    cin >> again;
while ( again = y);
}


What mistakes am I making?
The cleaned up version:
1
2
3
4
5
6
char again;
do
{
    cout << "Hello C++ Forums!";
    cin >> again;
} while ( again == 'y' );


- brackets around the do body
- assignment operator (=) to equality check (==)
- quotation marks around y
Last edited on
I tried implementing what you said into a different piece of code, but it gave me one warning saying that: 'again' is uninitialized in this function.

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
#include <iostream>

using namespace std;

int main()
{
char again;
int radius;
float Pi = 3.14159;

do {

    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";

    cin >> radius;

    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\n Do you want to do this again? ";
} while ( again == 'y' );

}
You must add char again; at the top of the main, because it is a variable and it won't magically appear.
Then after cout << "\n\n Do you want to do this again? "; you have to write cin >> again;
Last edited on
Thank you so much Vidminas! You helped me solve it!
Do you understand how this works? I can explain it in detail if you want.
closed account (3qX21hU5)
Ok I'll run you through this loop step by step.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream> // This includes input and output capabilities

using namespace std; // Qualifies the std namespace (So you dont have to write std::cout, ect)

int main()
{
    char again;

    do
    {
        // Inside the brackets { } of the do loop we tell the program what we want it to do over and over until the condition is met. 

    } while (again == 'y');  // This is the condition. For do while loops it comes after and has a semi colon since it is a end of a statement.
}


Another thing to note about do while loops and while loops and their differences. Is that do while loops check the condition AFTER they run whatever is in the loop. Where as while loops check the condition BEFORE they run whatever is in the loop. That is a very important thing to keep in mind.

Hope this helps a bit.
Topic archived. No new replies allowed.