Access Violation--Segmentation Fault

Hey you guys...I'm so lost!! I got my program to compile but it crashes as soon as it receives valid input from the user. Program is supposed to receive a positive integer, convert it to binary in the form of a string using concatenation. Any ideas? I'd really appreciate it...I've been staring at this for hours. Thanks!!

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


string intToBinaryString (int); //Function prototype

int main ()
{

int number; //number variable for user input
string binary; //string called binary

cout << "Enter a non-negative integer to convert to binary: " ;
cin >> number;

//Validate input
while (number < 1)
{
cout << "Invalid entry, please enter a non-negative integer: ";
cin >> number;
}


//Call to function

cout << "The binary equivalent of the integer that you entered is: " << intToBinaryString(number);

return 0;

}


//binary to string function
string intToBinaryString (int number)
{

string binary = " ";
string binaryTemp = " ";

while (number > 0)
{
if (number % 2 == 0)
{
binaryTemp = "0";
}
else
{
binaryTemp = "1";
}

binary = binaryTemp + binary;

number = number >> 1;

return binary;

}
}
1
2
#include <cstdlib>
#include <string> 

:)

Also, you return binary; prematurely (it should go after the loop).
Topic archived. No new replies allowed.