When I want to print a space in C++, I get a number of 32 instead.

Asked 1 years ago, Updated 1 years ago, 35 views

It's incomplete, but I wrote the code

#include<iostream>
# include <cstdlib>

using namespace std;

int main (int argc, char**argv)
{
    intn;
    cin>>n;
    for(inti=0;i<n;i++){
        for(int j=0;j<2*(n-1)+1;j++){
            cout<<((j+1)<(i-1))?':(((n+i)>j)?':int(i+1))<endl;
        }
        cout<<endl;
    }
    return 0;
}

When you run it,

 3 // Value entered
32
32
32
1
1

32
32
32
32
2

32
32
32
32
32

Press Enter to exit the terminal...

In the plan, the number will be a triangle from top to bottom from 1 to the value entered, but for some reason, the number 32 will be displayed.I don't want to know the answer because it's a question, but does anyone know why the number 32 is displayed?

c++

2022-09-30 20:56

3 Answers

Perhaps you misunderstood the behavior of the trinomial operator A?B:C.

The trinomial operator A?B:C returns a value, but the value must always be of the same type.In other words, B and C must be the same.If not, the compiler checks if one type can be converted to the other type, performs type conversion if possible, and returns an error if not possible.

In this case, A?(char type value): (int type value), so char(') with few bits is converted to int.In other words,

  • char' (character code 32) is
  • Converted to int
  • int32 and
  • cout output

That's the flow.


2022-09-30 20:56

It's mainly because BLUEPIXY and Mossan are talking about it.
By the way, I don't understand the problem, but is it probably like this?

#include<iostream>
# include <sstream>
using namespace std;

namespace patch {
    // Converting integers to strings (C++11 can use to_string())
    US>string to_s(inti){
        ostringstreamos;
        os<<i;
        returnos.str();
    }
}

int main (int argc, char**argv)
{
    intn;
    cout<<"print number:";
    cin>>n;
    for(/*Delete as you don't want to know the answer*/){
        for(/*Delete as you don't want to know the answer*/){
            cout<<(
                (/*Delete as you don't want to know the answer*/)
                ? patch::to_s(i)
                // If you can use C++11 you can use to_string(i) (ex. Visual Studio 2012 or later)
                : (/*Delete as you don't want to know the answer*/)
                    ? " "
                    : patch::to_s(i)
            );
        }
        cout<<endl;
    }
    return 0;
}

If you don't explicitly convert it to a string yourself,
I deduce that it is int type in cout, so 32 is shown.
If you are using Visual Studio 2012 or later, you can use to_string()
You can convert a number to a string.

The results are as follows:

print number:8
1
22
3 3
4  4
5   5
6    6
7     7
88888888
Press any key to continue.


2022-09-30 20:56

The reason why 32 is displayed is as Hideki replied, so in this case, ' will be displayed as expected if statement instead of conditional expression.

#include<iostream>
# include <cstdlib>

using namespace std;

int main (int argc, char**argv)
{
    intn;
    cin>>n;
    for(inti=0;i<n;i++){
        for(int j=0;j<2*(n-1)+1;j++){
            if((j+1)<(i-1){
                cout<<';
            }
            else if((n+i)>j){
                cout<<';
            }
            else{
                cout<<(i+1);
            }
            cout<<endl;
        }
        cout<<endl;
    }
    return 0;
}

Run Results

 3 // Value entered



1
1





2







Press Enter to exit the terminal...


2022-09-30 20:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.