Why does this program not output anything?

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

int main(){
    int x;
    char c;
    int o;
    std::cin>>x>>c;
    bool run1=true,run2=false;
    while(x!=0 ){
        x=o;
        while(x>0 && run1){
            x--;
            for(int n=0;n<x;n++){
                std::cout<<" ";
            }
            std::cout<<c<<"\n";
        }
        if(x==0){
            run1=false;
            run2=true;
        }
        while(x<o && run2){
            x++;
            for(int t=0;t<x;t++){
                std::cout<<" ";
            }
            std::cout<<c<<"\n";
        }
        
        
        std::cin>>x>>c;
    }
    
    return 0;
}
Why does this program not output anything?

What should it output?

‘o’ is uninitialized, hence after this line
x=o;
x is assigned an unspecified value.

Therefore this loop:
1
2
3
for(int n=0;n<x;n++){
    std::cout<<" ";
}

might iterate millions of times and millions of std::couts may take a while.
Just sit down and wait: I think it’s a proper punishment for having written that code.
- just kidding ;-)
I was trying to print the left border of a hexagon with the char inputed.
But o gets initialized in every loop so it should not matter, or does it?
I was trying to print the left border of a hexagon with the char inputed.

Thank you for having kept that secret: it would have been very stressful for us to get a description of your code before reading it.

But o gets initialized in every loop so it should not matter, or does it?

Is o initialized before being assigned to ‘x’?

By the way, may I suggest you to change your variable name from lowercase ‘o’ to uppercase ‘O’?
It would be far funnier to tell apart these two instructions:
1
2
x=0;
x=O;

Hi @yabi2943993,

I am taking a guess that a positive number will draw the upper part of a hexagon, and a negative number will draw the lower part of a hexagon. I added the text "Number of Columns: " and "Character: " to help make it clearer for the user to understand the input.

Entering the number 5 with a character T will show the following output
    T
   T
  T
 T
T


Entering the number -5 with a character G will show the following output
G
 G
  G
   G
    G


Yes, the variable o will need to be initialized with a number for it to be valid. I limited modifications to your code and here is working code that I have. A variable called Startx was also added which contained the inverse of the original value so that spacing can start at zero and progress to a higher number for the lower-half of the hexagon.


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

int main()
{
    int x;
    char c;
    int o = 0;
    std::cout << "Number of Columns: ";
    std::cin >> x;
    std::cout << "Character: ";
    std::cin >> c;
    bool run1 = true, run2 = true;
    while (x != 0) {
        //x = o;
        while (x > 0 && run1) {
            x--;
            for (int n = 0; n < x; n++) {
                std::cout << " ";
            }
            std::cout << c << "\n";
        }
        if (x == 0) {
            run1 = false;
            run2 = true;
        }
        int Startx = -x;
        while (x < 0 && run2) {
            x++;
            for (int t = 0; t < x + Startx; t++) {
                std::cout << " ";
            }
            std::cout << c << "\n";
        }
        std::cin >> x >> c;
    }
    return 0;
}


Is this what you were wanting to do?
Yes!! Thankyou very much @eugenedakin. Actually your idea with the variable Startx is very nice.
My code wasn't working also when I implemented that.
The I have changed cin>>x>>c; to cin>>x;cin>>c; and it worked. Why did that happen?!
Now it works..
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
#include <iostream>

int main(){
    int x;
    char c;
    std::cin>>x;
    std::cin>>c;
    int o=x-1;
    bool run1=true,run2=false;
    while(x!=0 ){
        while(x>0 && run1){
            x--;
            for(int n=0;n<x;n++){
                std::cout<<" ";
            }
            std::cout<<c<<"\n";
        }
        if(x==0){
            run1=false;
            run2=true;
        }
        while(x<o && run2){
            x++;
            for(int t=0;t<x;t++){
                std::cout<<" ";
            }
            std::cout<<c<<"\n";
        }
        
        
        std::cin>>x>>c;
    }
    
    return 0;
}
Last edited on
The I have changed cin>>x>>c; to cin>>x;cin>>c; and it worked. Why did that happen?!


I am not sure why this change made a difference. I tried both of these inputs and they both worked the same on Visual Studio C++ 2019:

1
2
std::cin>>x;
std::cin>>c;


std::cin >> x >> c;

Glad to see that you have working code!
If someone is interested...
Just created a code that draws the full hexagon:
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
#include <iostream>

int main(){
    int x;
    char c;
    std::cin>>x;
    std::cin>>c;
   bool run1,run2;
    while(x!=0 ){
         int o=x-1;
          run1=true;
          run2=false;
               int s=0;
    int z=o-1;
        while(x>0 && run1){
            x--;
            for(int n=0;n<x;n++){
                std::cout<<" ";
            }
            for(int f=0;f<o+1+(2*s);f++){
                std::cout<<c;
            }
            s++;
            std::cout<<"\n";
        }
        if(x==0){
            run1=false;
            run2=true;
        }
        while(x<o && run2){
            x++;
            for(int t=0;t<x;t++){
                std::cout<<" ";
            }
            for(int p=0;p<o+1+(2*z);p++){
                std::cout<<c;
        }
        z--;
           std::cout<<"\n";
         
        }
        
        
        std::cin>>x;
        std::cin>>c;
    }
    
    return 0;
}
Last edited on
Thanks @yabi2943993,

Its appreciated to see the full running code. This will help other too.

Warm regards - Eugene
Topic archived. No new replies allowed.