首页 > 解决方案 > C ++如何引用用户输入?

问题描述

如何引用用户输入的整数以及结果。例如,我正在尝试打印此代码,二进制格式的数字“12”是“1100”

我的代码;

#include <iostream>

using namespace std;

void decimalToBinary();

int main()
{
    decimalToBinary();

    return 0;
} 

void decimalToBinary()
{
    int array[32];
    int number, index;

    cout << "Enter a number to convert to binary: ";    
    cin>>number;

    cout << endl;

    cout << "The number " << number << " in binary format is ";

    for(index=0; number>0; index++)    
    {    
    
        array[index]=number%2;
        number= number/2;  
    
    }

    for(index=index-1 ;index>=0 ;index--)    
    {    
    
        cout << array[index];
    
    } 

} 

标签: c++

解决方案


您需要放置一个 \(转义序列,与 \n 或 \t 使用的方法相同)以表明引号将被忽略:

cout << "The number \" " << number << " \" in binary format is ";

推荐阅读