transform int to char

This is a discussion on transform int to char within the C++ Programming forums, part of the General Programming Boards category; hi , im making a program and i have a problem , anyway , here is the problem , my ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    3

    Question transform int to char

    hi , im making a program and i have a problem , anyway , here is the problem , my program ask a question which is supposed to be answered in numbers , but i want to make it so that if u answer it with x for example , it would quit , i tried transforming int into char by using the transformer method guess(char) , but that didnt work , any ideas how i can make it possible ?

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    use itoa()
    Code:
    int i;
    char c;
    cin>>i;
    c = itoa(i);
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >c = itoa(i);
    itoa (usually, but not certainly because it isn't standard) returns a pointer to char. Since c isn't a pointer to char, this won't work too well.

    >any ideas how i can make it possible ?
    Simple, just use cin's type checking to break the loop for you:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int num;
    
      cout<<"Enter a set of numbers (x to quit): ";
      while ( cin>> num )
        cout<<"You entered "<< num <<'\n';
      cout<<"Had enough, huh?"<<endl;
    }
    Because num is an integer, cin expects an integer. If it reads a character then it fails and the loop terminates. Of course, if you have any input from cin after the loop you will need to clear the error and discard the unread characters.
    My best code is written with the delete key.

  4. #4
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    >>itoa (usually, but not certainly because it isn't standard)
    Really? I thought itoa was standard, but atoi wasn't for some reason.
    I've always used it as returning an actual char on the cygwin gcc.
    Guess you (meaning I) learn something new every day.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

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, 09:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 10:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21