Thread: casting int to char

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    casting int to char

    i've been looking for this for a while, but unfortunatelly not too many descriptive insights.....

    well,.... correct me if i am wrong ......

    i need to cast from integer to character...

    i did............

    Code:
    int Selection;
    char Choice;
    Choice = (char) Selection;
    considering it is possible, so far so good... at least the compiler isn't complaining...

    the trick is, after converting that "int" to "char" i need to check whether that converted "Choice" is a numeric character......

    so i write:


    Code:
    #include <ctype.h>
    
    if(isdigit(Choice))
        cout << "is digit";

    .......all good
    after doing all of the above the compiler is all happy however the output is nowhere near... nothing works properly.....
    maybe i am doing this wrong ....



    any help would be appreciated
    Regards,
    mathe917

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i got this to compile, but i don't know if its what you want.

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    #define tab '\t'
    
    main()
    {
    	int a=113;
    	char b;
    	b=static_cast<char>(a);
    	cout<<b;
    	return(0);
    }

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    thanx for input.....
    however the casting is done fine...yours and mine.......

    the casting isn't the problem

    seams like the comparison isn't doing the job


    Regards,
    matheo917

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    You're passing a char to isdigit. The param for isdigit is int.

    From MSDN:

    //
    int isdigit( int c );

    Parameter

    c

    Integer to test

    //

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i guess i don't understand what you mean by "numeric character" or what your trying to do with the comparison. i'd like to help. . .

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Does selection have an integer value such as 5, or a character value '5'? If it's the former then the test will fail unless you force a character conversion, not simply casting. This works peachy.
    Code:
    #include <iostream>
    #include <cctype>
    
    int main ( void )
    {
      int selection = 5;
      char choice = selection + '0';
      if ( isdigit ( choice ) )
        std::cout<< choice <<"\nThis works\n";
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    think of a simple do ... while loop ...
    which keeps looping around if the input is a number smaller than 0 or grater than 5, as well as if you would enter a letter "w" in place of an integer the program would crash, kind of loop forever, the reason why i needed that isdigit is to determine if the input is a letter "w" or a simple so called number value "4"......

    maybe i was using a wrong function???

    what you think??

    Regards,
    matheo917

  8. #8
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    i played around with it from a different direction and still nothing...


    here's the code of this perticular loop:

    Code:
    int Selection;
      char Choice[2];
      do
       {
           cout << "1.  HELP MENU \n\n";
           cout << "2.  INVENTORY DIRECTORY \n\n";
           cout << "3.  DELIVERY INFORMATION \n\n";
           cout << "4.  ORDERING DIRECTORY \n\n";
           cout << "5.  WAITING LIST \n\n";
           cout << "0.  EXIT \n\n\n\n";
    
            cout << "Enter a number (#) corresponding to your selection - ";
    
            cin >> Selection;
    
            itoa(Selection, Choice, 2);
    
            clrscr();                               // clears the screen...
    
       }while((isalpha(Choice[0])) || (Selection < 0 || Selection > 5));
    .......
    for some reasong i can't get it to work.... maybe i am using a wrong algorithm in a while loop delimeter...... i am sure there's gotta be a way, i am sure people have run into this problem before when asked for int someone entered a char and the loop crashed...??

    ?? any ideas?

    Regards,
    matheo917

  9. #9
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    anyone care to give another perhaps a different solution??

  10. #10
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    hmmm..... no answers...

    anyone please....

    thank you..

    Regards,
    matheo917

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Is there any reason you can't do it like this?
    Code:
      char Choice;
      do
       {
           cout << "1.  HELP MENU \n\n";
           cout << "2.  INVENTORY DIRECTORY \n\n";
           cout << "3.  DELIVERY INFORMATION \n\n";
           cout << "4.  ORDERING DIRECTORY \n\n";
           cout << "5.  WAITING LIST \n\n";
           cout << "0.  EXIT \n\n\n\n";
    
            cout << "Enter a number (#) corresponding to your selection - ";
    
            cin >> Choice;
    
            clrscr();                               // clears the screen...
    
       }while(Choice < '0' || Choice > '5');

  12. #12
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    in this particular case the compiler will not detect whether a character is less than the other based on the less than or greater than principal.......at least that's what i think and compiler too i guess....

    i am working on something similar now, although i am making my loop to be infinite ....... here it is:

    Code:
    do
       {
           cout << "1.  HELP MENU \n\n";
           cout << "2.  INVENTORY DIRECTORY \n\n";
           cout << "3.  DELIVERY INFORMATION \n\n";
           cout << "4.  ORDERING DIRECTORY \n\n";
           cout << "5.  WAITING LIST \n\n";
           cout << "0.  EXIT \n\n\n\n";
    
            cout << "Enter a number (#) corresponding to your selection: ";
    
            cin >> Selection;
            Temp2 = Selection;
            Temp1 = (int)Temp2;
            Choice = Temp1;
    
            clrscr();                               // clears the screen...
    
       }while ((Selection < '0' || Selection > '5') || (Choice < 0 || Choice > 5));
    i am thinking of a ASCII representation of these number when it comes to characters i.e. char(32) maybe that will work......

    what do you think?

    Regards,
    matheo917

  13. #13
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    got it to work, however it's not too roboust and too innovative, but i kind of got tired of trying to do it the hard way and so here it goes......

    Code:
    string Selection;
    
      do
       {
           cout << "1.  HELP MENU \n\n";
           cout << "2.  INVENTORY DIRECTORY \n\n";
           cout << "3.  DELIVERY INFORMATION \n\n";
           cout << "4.  ORDERING DIRECTORY \n\n";
           cout << "5.  WAITING LIST \n\n";
           cout << "0.  EXIT \n\n\n\n";
    
            cout << "Enter a number (#) corresponding to your selection: ";
    
            cin >> Selection;
    
        clrscr();
    
       }while ((Selection != "0" && Selection != "1" &&
                Selection != "2" && Selection != "3" &&
                Selection != "4" && Selection != "5"));
    thanks for the effort...


    matheo917

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    in this particular case the compiler will not detect whether a character is less than the other based on the less than or greater than principal.......at least that's what i think and compiler too i guess....
    It does on my computer (Windows NT). But I guess if you're not comfortable with it, go with what feels right.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM