array

What do i do about this
#include <iostream>

using namespace std;

int main() {
int Array[20];
int i, n, d;

cout<<"_ _ _ _ _ _ _ _ _ _ The Number Sorter _ _ _ _ _ _ _ _ _ _ "<<endl<<endl;



cout<<" Numbers Entered Are "<<endl;
for (int i = 0; i < 20; ++i)
{
cin>>Array[i];
cout<<" "<<Array[i];
}
cout<<"\n";

int count = 0;
for (count = 0; count<20; count++){

cin>>Array[count];
}

if (count <= 20)
{
cout << "\n Total number of inputs entered: " <<count<< endl;
}
else {
cout << "\n The total number of inputs is exceed or less than 20 "<<endl;
}

I need what input to be counted but i cant
You appear to have TWO separate loops that fetch 20 numbers from the user.

This seems wrong. Shouldn't you only do that ONCE?

Also, you're fetching 20 numbers, always, every time, so the number of entries will always be 20.
Hello NZUineedhelp,

NZUineedhelp wrote:

What do i do about this


First

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



This with the comments should explain everything. if not let me know.
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
#include <iostream>

//using namespace std;  // <--- Best not to use.

int main()
{
    constexpr int MAXSIZE{ 20 };  // <--- Added.

    int Array[MAXSIZE]{};
    //int i{}, n{}, d{};  // <--- None of these variables used. Also they need better names.

    std::cout << "_ _ _ _ _ _ _ _ _ _ The Number Sorter _ _ _ _ _ _ _ _ _ _\n\n";

    //std::cout << " Numbers Entered Are:\n";
    std::cout << "\n Enter " << MAXSIZE << " numbers:\n";

    for (int idx = 0; idx < MAXSIZE; ++idx)
    {
        std::cout << "  Eneter #" << idx + 1 << ": ";
        std::cin >> Array[idx];
        //std::cout << " " << Array[idx];  // <--- Why? You can already see what was entered. Better used in a for loop to print the entire array.
    }

    std::cout << "\n";

    int count{ MAXSIZE };  // <--- Will always end up as 20.

    //for (count = 0; count < 20; count++)  // <--- Same as above.
    //{
    //    std::cin >> Array[count];
    //}

    if (count <= 20)  // <--- "count" will always be 20 because your for loop is always 20. And this makes no sense.
    {
        std::cout << "\n Total number of inputs entered: " << count << '\n';
    }
    else  // <--- This will never be reached because the if statement will always be true.
    {
        std::cout << "\n The total number of inputs is exceed or less than 20 " << '\n';
    }
}

Prefer using the new line (\n) over "endl" as much as possible.

Andy
IMO it would help if the OP explained in more detail what was tried to be accomplished - because I don't know. Where does 'sorter' come into this?
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
#include <iostream>

int constexpr sz = 20;

int main()
{
  int input[sz];
  int count = 0;
    
  while (count < sz && std::cin >> input[count]) ++count; 
  
  std::cout << "Read " << count << " integers.\n";
  
  // Naive bubble sort 
  // See: https://en.wikipedia.org/wiki/Bubble_sort
  for (int i = 0; i < count; ++i) 
    for (int j = 0; j < count - 1; ++j)
      if (input[j] > input[j + 1])
      { 
        int temp = input[j];
        input[j] = input[j + 1];
        input[j + 1] = temp;
        // or: std::swap(input[j], input[j + 1]);
      }
  // or: std::sort(input, input + count);

  for (int i = 0; i < count; ++i) std::cout << input[i] << ' ';
  std::cout << '\n';
}
Topic archived. No new replies allowed.