Do-While Loop help!

Can someone help me write this as a do-while loop?
I'm having some trouble. I know that it is stuck in a constant loop and I need to change it to a do-while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// PLACE YOUR NAME HERE

#include <iostream>
using namespace std;

int main()
{
	char letter = 'a';

    while (letter != 'x')
	{
           cout << "Please enter a letter" << endl;
           cin >> letter;
           cout << "The letter you entered is " << letter << endl;
    }

    return 0;	
}
just changing it to a do-while won't fix anything. I don't see anything that needs fixing anyway.
Last edited on
I'm also a beginner but I just want to help out. This is actually my first post so here goes...

When I run your code, I don't get a constant loop. I'm not sure what problem you're having.

First, you don't need to initialize letter in line 8, you can just declare it like this char letter;. The logic of your program tells me it will keep asking user to enter a letter until user enters 'x' in which case program terminates. There's no loop there. If you want to change this to a do-while loop, you can easily convert your code to this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// PLACE YOUR NAME HERE

#include <iostream>
using namespace std;

int main()
{
	char letter;
        do
        {
              cout << "Please enter letter 'x'" << endl; //since that's the letter you want user to eventually enter?
              cin >> letter;
              cout << "The letter you entered is " << letter << endl;
        }
        while (letter != 'x');
        // maybe cout something here to let user know program is about to terminate?
        return 0;	
}


Hope this helps!
Topic archived. No new replies allowed.