3

I wrote a program so the user inputs a number and the program outputs its binary representation.

I get this error:

No matching function for call to `getline(std::istream&, unsigned int&)'

How can I solve this?

Also, it outputs:

0
0
0
0

...when it should output the right value for the input.

#include <iostream>
using namespace std;

int main()
{
    int Number;
    cin >> Number;
    bool Binary[sizeof(int) * CHAR_BIT];
    for (unsigned int i = 0; i < sizeof(int) * CHAR_BIT; i++)
        Binary[(sizeof(int) * CHAR_BIT - 1) - i] = Number & (1 << i);

    for (unsigned int i = 0; i < sizeof(int); i++)
        std::cout << Binary[i] << std::endl;

    system("pause");
    return 0;
}
kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
Joriek
  • 39
  • 1
  • 1
  • 4

6 Answers6

11

The getline() function takes an istream and a string, not an integer. So:

string sNumber;
getline(cin, sNumber);
// now convert sNumber to an unsigned int
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

std::getline() gets a line into a std::string variable. You'll need to convert it to an unsigned int yourself. The usual way is to use a std::istringstream instance like so:

#include <sstream>
#include <iostream>
#include <iomanip>

int main ()
{
    std::string line;
    std::getline(std::cin, line);
    std::istringstream iss(line);

    unsigned int number = 0U;

    if (!(iss >> number))
    {
        // Handle error
    }
    else
    {   // Put your binary conversion logic in a function!
        std::cout << to_binary(number) << std::endl;
    }
}

Tip: Indentation can greatly improve the readability and maintainability of your code.

Putting your binary conversion logic into a function will make it easier to test (think "unit-test") and to reuse, like this:

// I've left this logic untouched other than to indent it and to introduce
// braces. It needs to be fixed. Declare before main().

std::string to_binary(unsigned int Number)
{
    std::ostringstream oss;

    bool Binary[sizeof(int) * CHAR_BIT];
    for (unsigned int i = 0; i < sizeof(int) * CHAR_BIT; i++)
    {
        Binary[(sizeof(int) * CHAR_BIT - 1) - i] = Number & (1 << i);
    }

    for(unsigned int i = 0; i < sizeof(int); i++)
    {
        oss << Binary[i] << std::endl;
    }

    return oss.str();
}

I've left correcting your binary conversion logic as an exercise as this looks like homework.

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • @Joriek: It (your compiler) would give you an error if you had not extracted the function `to_binary()`. I'be updated my answer to clarify what that may look like. – johnsyweb Mar 07 '12 at 17:28
1

getline can only be used to read in a complete line as a string. If you want to read in a number, use operator>>.

Example:

int number;
std::cin >> number;
swegi
  • 4,046
  • 1
  • 26
  • 45
1
unsigned int Number;
std::cin >> number;

and why you cant use getline(). Read this!!

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
0

The following are the valid signatures of getline:

istream& getline(char*, int, char = '\n');
istream& getline(signed char*, int, char = '\n');
istream& getline(unsigned char*, int, char = '\n');

You are not using any of them

Eduardo
  • 8,362
  • 6
  • 38
  • 72
-1

Change getline(cin, Number) to cin >> Number

Kien Truong
  • 11,179
  • 2
  • 30
  • 36