Sorting with Vectors?

Hey guys,

actually i got a little problem with my code.
In fact it should be easy, but the output of the program is not like it should be.


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include "myError.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;


int main() try {

    int ix{};
    int iy{};
    int iz{};
    std::vector<int> iOrdnen{};

    iOrdnen.push_back(ix);
    iOrdnen.push_back(iy);
    iOrdnen.push_back(iz);

    cout << "Bitte geben Sie drei Werte (ix, iy und iz) an: ";
    cin >> ix >> iy >> iz;

    std::sort(iOrdnen.begin(), iOrdnen.end());

    for (unsigned int i{0U}; i < iOrdnen.size(); ++i) {

        cout << iOrdnen.at(i) << "\n";

        cout << "\n\t" << "Ihre eingebenen Zahlen lauten: " << ix << "," << iy << "," << iz;
    }

    if (ix < 999 && iy < 999 && iz < 999) {
        if (ix > 0 && iy > 0 && iz > 0) {

            int isumme = ix + iy + iz;
            cout << "\n\t" << "Die Summe aller Zahlen betraegt: " << isumme;

            int iprodukt = ix * iy * iz;
            cout << "\n\t" << "das Produkt aller Zahlen betraegt: " << iprodukt;
            cout << "\n\t";

            int idifferenz1 = ix - iy;
            int idifferenz2 = iy - iz;
            cout << "\n\t" << "Die Differenz aus ix und iy betraegt:  " << idifferenz1;
            cout << "\n\t" << "Die Differenz aus iy und iz betraegt: " << idifferenz2;

            cout << "\n\t";
            double iquotient1 = ix / (iy * 1.0);
            double iquotient2 = iy / (iz * 1.0);
            cout << "\n\t" << "Der Quotient aus ix und iy betraegt:  " << iquotient1;
            cout << "\n\t" << "Der Quotient aus iy und iz betraegt:  " << iquotient2;
            cout << "\n\t";
        } else

            error("Negative Zahlen sind nicht erlaubt!");
        cout << "\n\t";
        return main();

    } else
        cout << "Deine eingebenen Zahlen sind ungueltig." << "\n\t";
    cout << "\n\t";
    return main();

} catch (std::exception &e) {
    std::cerr << e.what();
    return -2;
} catch (...) {
    std::cerr << "Ein Fehler";
    return -1;
}



At the beginning of main() there is the method for sorting.
In my case the user have to put three values, each for x,y and z, but they don't get sorted afterwards.

If i change "iOrdnen.at(i)" to "iOrdnen.at(1)" the program adds the value of x to the last digit.

For example: The user sets for x = '2' , for y= 12 and for z = 4
The output will be like: 2,12,4'2'

And that problem affects the calculation.

What did i wrong?
Last edited on
Move the vector pushback statements (lines 18-20) AFTER you retrieve the user supplied values (line 23). You are sorting a vector filled with zeros currently.
You have other problems in your code waiting to strike. Don't EVER call main().

No, not ever. Only the operating system does that.

You are also missing an opening bracket { after main() and before your try statement.

Ignoring your custom header inclusion the code as you've posted it won't compile even if the error function call at line 57 is commented out.
Last edited on
Thank you!

I changed it, but i have one question left:

How can i change the code that the output line says:

"Ihre eingegeben Zahlen lauten: " and then the sorted values?

If i add it into the line 19 before iOrdnen.at(i), it repeats me the text three times.

Do you have any idea?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 int ix{};
    int iy{};
    int iz{};
    std::vector<int> iOrdnen{};

   
    cout << "Bitte geben Sie drei Werte (ix, iy und iz) an: ";
    cin >> ix >> iy >> iz;

    iOrdnen.push_back(ix);
    iOrdnen.push_back(iy);
    iOrdnen.push_back(iz);

    
    std::sort(iOrdnen.begin(), iOrdnen.end());

    for (unsigned int i{0U}; i < iOrdnen.size(); ++i) {

        cout << iOrdnen.at(i) << ',';   
    }

   cout << "\n\t" << "Ihre eingebenen Zahlen lauten: " ;


Last edited on
It has been decades since I studied German, in US high school, so I needed a translator to interpret what your code text meant. ;)

With that said, maybe this is what you are looking for?

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

int main()
{
   std::cout << "Bitte geben Sie drei Werte (ix, iy und iz) an: ";
   int ix;
   int iy;
   int iz;
   std::cin >> ix >> iy >> iz;

   std::vector<int> iOrdnen;

   iOrdnen.push_back(ix);
   iOrdnen.push_back(iy);
   iOrdnen.push_back(iz);

   std::cout << "\nIhre eingebenen Zahlen lauten: ";

   std::sort(iOrdnen.begin(), iOrdnen.end());

   for (size_t i = 0; i < iOrdnen.size(); ++i)
   {
      std::cout << iOrdnen[i] << "  ";
   }
   std::cout << '\n';
}
Bitte geben Sie drei Werte (ix, iy und iz) an: 25 5 15

Ihre eingebenen Zahlen lauten: 5  15  25

Your for loop could also written as:
23
24
25
26
27
   for (auto i = iOrdnen.cbegin(); i != iOrdnen.cend(); i++)
   {
      std::cout << *i << "  ";
   }
   std::cout << '\n';

Or even:
23
24
25
26
27
   for (const auto& i : iOrdnen)
   {
      std::cout << i << "  ";
   }
   std::cout << '\n';

Last edited on
Sorry for writing back so lately,

meanwhile i solved the task.


ps.: studying a little bit german is never bad ;)
Topic archived. No new replies allowed.