Hey i have not been programming long so this is probably simple to most of you but i need to make my numbers show as binary which i have already done wheyy however if the users input text it goes into an infanate loop. I need a way to only let the users input numers to my program any help would be great. here is my code so far
Code:
// Includes
#include "stdafx.h"
// Start Main function.
int main(int argc, char **argv)
{
//Initialise variables.
int mask = 2147483648; // Mask to use for size of 32 bits.
int bValue; // The value you want to show as binary
printf ("////////////////////////\n");
printf ("// //\n");
printf ("// Decimal to Binary //\n");
printf ("// //\n");
printf ("////////////////////////\n\n");
do
{
printf ("\n\nEnter a number (or 9999 to quit): "); // prints the text within the brackets asking the user for a value
scanf ("%d", &bValue); // convert there input as a decimal digit.
printf("\nThe Binary of (%d) is: ", bValue); //prints the binary view here
for (int i = 0; i < 32; i++) // start the for loop if i is less than 32 then increment i by 1
{
if ((bValue & mask) == 0) // AND the user input with the mask to check if its a zero
{
printf("0"); // if it is a zero print a zero
}
else
{
printf("1"); // if its not a zero print a 1
}
if (bValue == 9999 ) //check the user input if there value is 9999 if it is then end program
{
return(0);
}
bValue = bValue << 1; // move the input number to the left and check the next didit
}
}
while(1);// Test the Do While loop if it is true or false
}