kmick96
kmick96

Reputation: 31

C++ : Making a do while loop repeat

I'm very sorry for the super newbie question, but I cannot for the life of me understand how to make a do while loop repeat. I changed a while loop into a do while loop and now I don't know how to get it to ask "would you like to repeat Y or N?" any explanation?

I've read various posts that accomplish a do while loop repeat, but they don't make sense to me. Am I essentially going to wrap this code into another do while loop? Do I move the boolean expression to before the while?

#include <iostream>
using namespace std;
int main(void)
{
    int x;
    int count = 0;
    int N;
    double sum = 0; 
    double average;
    char ans;

    {
        cout << "Enter number of values, N, to be read in <Enter>:" << endl;
        cin >> N;
        do
        {
            cout << "\n Enter a grade <Enter>: ";
            cin >> x;
            sum = sum + x;
            count++; // 
        } while (count < N);

        if (N == 0)
            cout << "You have entered 0 numbers. No average will be computed. Bye! \n";
        else {
            average = average = sum / N;
            cout << "The average of these " << N << " grades is " << average << endl;
        }

         cout << "Would you like to enter more values to calculate your grade average?\n";

        system("pause");
        return 0;
    }
}

Upvotes: 2

Views: 2729

Answers (3)

GKE
GKE

Reputation: 1000

Although there are a ton of ways to do it better, this code allows me to explain what is happening. Do-While loops are definitely pretty tricky. However, just remember that a do-while loop is designed to run at least once.

In the case of our code, we set up if statements to test against running code where it is not appropriate.

Take a look at how the very first if statement is N > 0, the if-statement does execute, please do not forget that. Once more for emphasis, the if statement DOES execute, it just results in false.

With that being said, you use a do-while loop when you want your code block to execute AT LEAST once. Notice how our while statement has two things we're testing for, one, is the answer 'Y' from the user to continue, and if it is, is the newly inputted N value greater than 0?

You might be wondering if the inner most if statement ever executes at least once, well the answer is it depends on the previous if statement result if(N > 0) and if(count == N && N != 0) both execute once every single time the while loop stays true. However that nested if depends on it's parent result.

I hope this cleared some things up for you on do-while loops.

#include <iostream>
using namespace std;
int main(void)
{
    int x;
    int count = 0;
    int N;
    double sum = 0;
    double average;
    char ans = 'Y';

        cout << "Enter number of values, N, to be read in <Enter>:" << endl;
        cin >> N;

        do
        {
            if(N > 0){ //THIS IF STATEMENT WILL ALWAYS RUN AT LEAST ONCE
            cout << "\n Enter a grade <Enter>: ";
            cin >> x;
            sum = sum + x;
            count++;
            }

            if(count == N && N != 0) {//THIS IF STATEMENT WILL ALSO ALWAYS RUN AT LEAST ONCE
                average = average = sum / N;
                cout << "The average of these " << N << " grades is " << average << endl;
                cout << "Would you like to enter more values to calculate your grade average?\n";
                    cin>>ans;
                    if(ans == 'Y') {//This one depends on it's parents result.
                        x = 0;
                        N = 0;
                        sum = 0;
                        count = 0;
                        cout << "Enter number of values, N, to be read in <Enter>:" << endl;
                        cin >> N;
                    }
            }

        } while (ans == 'Y' && N != 0);

        if (N == 0)
            cout << "You have entered 0 numbers. No average will be computed. Bye! \n";

        system("pause");
        return 0;
}

Upvotes: 0

Programming_Wiz
Programming_Wiz

Reputation: 11

can do this:

 char repeat='y'; 
 cout << "Enter number of values, N, to be read in <Enter>:" << endl;
 cin >> N;

 do
    {
        for(int i=0;i<n;i++){
            cout << "\n Enter a grade <Enter>: ";
            cin >> x;
            sum = sum + x;
            count++; // 
        }

    if (N == 0)
        cout << "You have entered 0 numbers. No average will be computed. Bye! \n";
    else {
        average = average = sum / N;
        cout << "The average of these " << N << " grades is " << average << endl;
    }

     cout << "Would you like to enter more values to calculate your grade average?\n";
     cin>>repeat;

 }while(repeat=='y');

Upvotes: 1

Gimhani
Gimhani

Reputation: 1375

May be this is what you need, with char YorN you are considering if to continue or break the inner do-while.

#include <iostream>
using namespace std;
int main(void)
{
    int x;
    int count = 0;
    int N;
    double sum = 0; 
    double average;
    char ans;
    char YorN;

    do{
        cout << "Enter number of values, N, to be read in <Enter>:" << endl;
        cin >> N;
        do
        {
            cout << "\n Enter a grade <Enter>: ";
            cin >> x;
            sum = sum + x;
            count++; // 
        } while (count < N);

        if (N == 0)
            cout << "You have entered 0 numbers. No average will be computed. Bye! \n";
        else {
            average = average = sum / N;
            cout << "The average of these " << N << " grades is " << average << endl;
        }

        cout << "Would you like to enter more values to calculate your grade average?\n";
        cin>>YorN;

    } while (YorN=='Y');
    return 0;
}

Upvotes: 0

Related Questions