Loop results in order

User2010101

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
Josh Engelsma

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

ORDER BY results of a query (nested?)

分類Dev

Reverse chronological order of struct/results

分類Dev

Order MySql results by DateTime value

分類Dev

how to order results of LEFT JOIN

分類Dev

Group by multiple columns, then order results

分類Dev

Using "ORDER BY" changes results of query

分類Dev

For loop producing unexpected results in C

分類Dev

Collecting IronPython Parallel Loop results

分類Dev

Getting duplicated results in foreach loop

分類Dev

Results of code execution appear out of order

分類Dev

Django model order the results according to the entry date

分類Dev

CSS rules in different order leads to different results

分類Dev

Sort results by order (ascending-descending)

分類Dev

Echo outputting results in erratic order in BASH

分類Dev

The order by clause returns wrong results in sub query

分類Dev

How to order joined results in Phalcon Framework

分類Dev

Adding Results to a List and Setting up a for-loop

分類Dev

Place results of predict() in a for loop inside a list

分類Dev

Saving results from getOutlier function in for loop

分類Dev

Multiprocessing a python for loop and saving results as a dictionary

分類Dev

Getting certain pandas dataframe results in a loop statement

分類Dev

store results of a loop in a shell variable in sh

分類Dev

PHP Loop through Results and Parse out Values

分類Dev

Inner loop for calculation and outer loop for saving results in Python

分類Dev

SPARQL query returns different results depending on the order of statements

分類Dev

Scraping multiple webpages and results are being outputted out of order

分類Dev

count the title character in php while grouping array results in alphabetical order?

分類Dev

Why hibernate Criteria not maintaining order in results of criteria.list()?

分類Dev

Problems with reusing LEFT JOIN results in WHERE and ORDER BY Clause

Related 関連記事

  1. 1

    ORDER BY results of a query (nested?)

  2. 2

    Reverse chronological order of struct/results

  3. 3

    Order MySql results by DateTime value

  4. 4

    how to order results of LEFT JOIN

  5. 5

    Group by multiple columns, then order results

  6. 6

    Using "ORDER BY" changes results of query

  7. 7

    For loop producing unexpected results in C

  8. 8

    Collecting IronPython Parallel Loop results

  9. 9

    Getting duplicated results in foreach loop

  10. 10

    Results of code execution appear out of order

  11. 11

    Django model order the results according to the entry date

  12. 12

    CSS rules in different order leads to different results

  13. 13

    Sort results by order (ascending-descending)

  14. 14

    Echo outputting results in erratic order in BASH

  15. 15

    The order by clause returns wrong results in sub query

  16. 16

    How to order joined results in Phalcon Framework

  17. 17

    Adding Results to a List and Setting up a for-loop

  18. 18

    Place results of predict() in a for loop inside a list

  19. 19

    Saving results from getOutlier function in for loop

  20. 20

    Multiprocessing a python for loop and saving results as a dictionary

  21. 21

    Getting certain pandas dataframe results in a loop statement

  22. 22

    store results of a loop in a shell variable in sh

  23. 23

    PHP Loop through Results and Parse out Values

  24. 24

    Inner loop for calculation and outer loop for saving results in Python

  25. 25

    SPARQL query returns different results depending on the order of statements

  26. 26

    Scraping multiple webpages and results are being outputted out of order

  27. 27

    count the title character in php while grouping array results in alphabetical order?

  28. 28

    Why hibernate Criteria not maintaining order in results of criteria.list()?

  29. 29

    Problems with reusing LEFT JOIN results in WHERE and ORDER BY Clause

ホットタグ

アーカイブ