char and int input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maher5
    New Member
    • Oct 2007
    • 7

    #1

    char and int input

    hi

    i wrote a program to convert int to binarys, it works just fine until the input is a char. how can i make it know the input is a char and not an int and therfore display and error msg?


    it might be an easy q? but im only new to programming.

    thanks a lot.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Are you using C or C++?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      It appears you have it working with an int. Yes?

      If so, it should work with a char. Just move your chaer to an int either directly or use the char in the call to your function with an int argument.

      I guess I'm not seeing the problem. A char is just another integer.

      Comment

      • maher5
        New Member
        • Oct 2007
        • 7

        #4
        it is C

        and my problem is when ever a char is entered the porgram would crash
        thats my codings
        note: it is not an assignment so i figured i can insert the codes


        #include "stdafx.h"
        int _tma
        in(int argc, _TCHAR* argv[])
        {
        int input;
        printf("NOTE: To Exit Enter 9999\n");
        do {
        printf ("Enter Int:\n");
        scanf("%d", &input);
        unsigned int mask =2147483;
        int i=0;
        for (i=0;i<32;i++) {
        int itemp=0;
        itemp = (input&mask);
        if (itemp != 0) {
        printf ("1");
        }else {
        printf ("0");
        }
        mask = mask/2;
        }
        printf("\n");
        }while (input!=9999);

        return 0;

        }



        thanks a lot

        Comment

        • maher5
          New Member
          • Oct 2007
          • 7

          #5
          all i want is to be able to print an error msg whenever a char is entered and not crash.. thanks again

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by maher5
            int input; <<<<<<
            printf("NOTE: To Exit Enter 9999\n");
            do {
            printf ("Enter Int:\n");
            scanf("%d", &input);
            You bug is where the <<< are.

            A char, like A is not an in integer value. scanf() expects integer values like 19 or -98. You would need to enter 65 instead to get the A.

            If you change your input to a char it will work for characters but will now crash in integers.

            scanf() assumes you know the format of the data before it is entered. If you can't tell whether a user will enter digits or characters then you can't use
            scanf().

            You have have to get a byte at a time and analyze the result.

            Comment

            Working...