Unexpected Output

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

int num;
int y [11];

cout << "Enter the amount of numbers you would like generated: " << endl;
cin >> num;
cout << endl;

int x[num];

for (int j=0; j<=num; j++)
{
x[j]=(rand()%10)+1;
}

for (int j=0; j<=num; j++)
{
switch (x[j])
{
case 1:
y[1]+=1;
break;

case 2:
y[2]+=1;
break;

case 3:
y[3]+=1;
break;

case 4:
y[4]+=1;
break;

case 5:
y[5]+=1;
break;

case 6:
y[6]+=1;
break;

case 7:
y[7]+=1;
break;

case 8:
y[8]+=1;
break;

case 9:
y[9]+=1;
break;

case 10:
y[10]+=1;
break;
}

}

for (int a=0;a<=y[1]+1; a++)
{
switch (a)
{
case 0:
cout << "1: ";
break;

default:
cout << "*";
}

}
}

This program is intended to print an asterisk for every time the number 1 appears in the array. However, the output is an infinite number of asterisks. I'm not sure what is wrong.
I didn't get infinite amounts, but I reliably got 2 *s even when I only generated one number, until I fixed the problem:

...
1
2
3
4
5
6
7
8
9
10
11
int main()
        {

        int num;
        int y [11];

        for (num=0; num<11; num++){
                y[num]=0;
                }

        cout << "Enter t 

...

initialize the array to zero.
 
int x[num];


invalid syntax
The array x is actually unnecessary. If you do use it, make it dynamic. std::vector<int> is a good, dynamic array.

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

using namespace std;

int main()
{
  int num;
  int y [11] {}; // initialize

  cout << "Enter the amount of numbers you would like generated: " << endl;
  cin >> num;
  cout << endl;

  // int x[num]; // ERROR: array size is not constexpr
  std::vector<int> x( num );

  for (int j=0; j<=num; j++) // ERROR: 'num' is not a valid index
  {
    x[j] = (rand()%10)+1;
  }

  for (int j=0; j<=num; j++) // ERROR: 'num' is not a valid index
  {
    if ( 0 < x[j] and x[j] < 11 )
    {
      ++y[ x[j] ];
    }
  }

  // This looks complex
  for (int a=0; a<=y[1]+1; a++)
  {
    switch (a)
    {
    case 0:
      cout << "1: ";
      break;

    default:
      cout << "*";
    }
  }

  cout << "\n\n1: ";
  for ( int a=0; a<=y[1]; a++ )
  {
    cout << "*";
  }
  cout << '\n';

  cout << "1: ";
  for ( int j=0; j<num; ++j )
  {
    int z = (rand()%10)+1;
    if ( 1 == z ) cout << "*";
  }
  cout << '\n';

}

@keski:
 
if ( 0 < x[j] and x[j] < 11 )


you wrote this...?
closed account (E0p9LyTq)
@chipp,

http://en.cppreference.com/w/cpp/language/operator_alternative

and is an alternative token for &&.

With all the different usages of & and &&, using and makes for (somewhat) easier understanding for logical operation.

The main reason for the alternatives is non-English character encodings.
@furry:
compilation error
after <ciso646> included, it works, but why it says this:

Because in C++ these are built into the language, the C++ version of <iso646.h>, as well as <ciso646>, does not define anything.
closed account (E0p9LyTq)
@chipp,

Your particular compiler implementation seems to require the inclusion of <ciso646>, others such as Visual C++ 2017 don't.

Same goes for several versions of minGW compilers, the versions included with Dev C++ and Code::Blocks.

Likely they include <ciso646> in a header chain.

No matter what the reason, the alternative operators are part of the C/C++ standard. Your compiler is not fully compliant with the standard.
Last edited on
Topic archived. No new replies allowed.