User2010101
User2010101

Reputation: 92

Loop results in order

I need your help with this problem. What I want to know is how to output of a loop based on the input.

Let's say we have a program that should measure if a triangle is right or not based on the inputs of the user. The input could be something like this:

6 8 10
25 52 60
5 12 13

Using the Pythagoras formula, we can determine if a triangle is or not right

C^2=a^2+b^2

Now, with the numbers provided, the output should be:

right
wrong
right

My question is..how can I do the calculation and check if it's right or not but format the output with the same order as the input?

This is what I've tried :

#include <iostream>
#include <cmath>

using namespace std;

int rightt;
int wrong;

int main()
{
    double a = 0, b = 0, c = 0;
    double formula = 0;

    for (int i = 0; i < 1;)
    {
        cin >> a >> b >> c;
        formula = pow(a, 2) + pow(b, 2);

        if (formula == pow(c, 2) && formula != 0)
        {
            //cout << "Right";
            rightt = rightt + 1;
        }

        if (formula != pow(c, 2) && formula != 0)
        {
            //cout << "Wrong";
            wrong = wrong + 1;
        }

        if (a == 0 && b == 0 && c == 0)
        {
            i = 1;
            cout << "\n";

            while (rightt > 0)
            {
                cout << "\n" << "Right";
                rightt = rightt - 1;

            }

            while (wrong > 0)
            {
                cout << "\n" << "Wrong";
                wrong = wrong - 1;

            }
        }
    }
    system("pause");
}

But my output is not as I desired. The output is first set the right, and then the wrong ones. Thanks, and I hope you understand my problem.

EDIT:

I need to have the output after the 0 0 0 is reached and not before. So If I left the commented sections , the output will be Number-output-Number-output , and what I need is to allow users to enter all numbers and tell the software that he finishes when he enters 0 0 0 , and after that give the output based on the order.

Let's imagine this input :

6 8 10   >> this is right
25 52 60 >> This is wrong
5 12 13  >> This is right
0 0 0    >> This is the values used to end the inputs 


Output should be 
right
wrong
right

Upvotes: 0

Views: 103

Answers (1)

Josh Engelsma
Josh Engelsma

Reputation: 2646

I think that rather than counting the number of right answers and wrong answers, you can STORE all of your answers IN ORDER, in an vector. Once you are done storing all your answers, you can just loop through the answers, and print them out one by one.

If you have not learned about vectors yet, the concept is simple... you have an array like collection of data. "push_back" always tacks the data to the end of the collection of data. So if your first answer was wrong, then right, then right, first you would push_back(wrong)...resulting in a collection of [wrong]. Then you would push_back(right) resulting in a collection of [wrong, right]. Again you would push_back(right) so your final vector would be a collection in the order of [wrong, right, right]

Now you just need to loop through your collection to print out the data. The "iter" is a pointer to each spot in your list. To get the "contents of each spot" you dereference, by saying (*iter) which will provide the string result values.

#include <iostream>
#include <cmath>
#include <vector>
#include <string>

using namespace std;



int main()
{
    double a = 0, b = 0, c = 0;
    double formula = 0;
    int numberOfResults = 0;
    int currentIndex = 0;
    vector<string> answers;

    for (int i = 0; i < 1;)
    {
        cout << "Enter the number of attempts: " << "\n";
        cin >> numberOfResults;
        string results[numberOfResults];
        cout << "Enter a b and c" << "\n";
        cin >> a >> b >> c;
        formula = pow(a, 2) + pow(b, 2);

        if (formula == pow(c, 2) && formula != 0)
        {
            results[currentIndex] = "Right";
            answers.push_back("Right");

        }

        if (formula != pow(c, 2) && formula != 0)
        {
            results[currentIndex] = "Wrong";
            answers.push_back("Wrong");    

        }

        if (a == 0 && b == 0 && c == 0 || currentIndex == numberOfResults-1)
        {
            for (int j = 0; j < numberOfResults; j++){
                cout << "\n" << results[j];
            }

            for(auto iter = answers.begin(); iter != answers.end(); ++iter){
                cout << "\n" << (*iter);
            }
            return 0;
        }
    }
    system("pause");
}

Upvotes: 1

Related Questions