Thread: C help :)

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Question C help :)

    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
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Provide custom keyboards that do not have letter keys.

    (That's the only way to prevent people from typing letters. If you want to know how to deal with people who type letters, you should search the boards and FAQ for "validating input" or some such topic.)

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Well, you can do it without buying a new keyboard, but it's not exactly easy. You'll first have to turn "echo" off. There's a function in the standard C library to set terminal features (look it up), or if you want to get fancy, download and use the "ncurses" library.

    So, you turn echo off, then use getc() to get characters one at a time. You test to see if they are valid (0-9, '.', 'e', 'E', etc): if they are, put them into a string and print them to the screen. If they aren't, discard them.

    Then, you can use sscanf() to convert your digits-only string to an int.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You should check bValue == 9999 before you enter your loop... because why start processing for the 1st digit if you intend to terminate?

    scanf returns a value you should check. If it's 1, then there has been a number. Else bValue has not changed and can be considered garbage.

Popular pages Recent additions subscribe to a feed