Char to Int

This is a discussion on Char to Int within the C++ Programming forums, part of the General Programming Boards category; I'm just trying to make this simple little thing that will decode ASCII into readable characters. The only problem is, ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Char to Int

    I'm just trying to make this simple little thing that will decode ASCII into readable characters. The only problem is, the code I've got so far voids everything after a space. IE '39 20', meaning 20 would be completely ignored...

    I've tried a lot of stuff, but I can't think of anything that makes it work.


    Code:
    /*
        BUGS:
    	Voids any ASCII after a space.
    */
    
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int main()
    { 
    	char str[1024];
    	int i;
    
    	cout << "Enter some some ASCII:" << endl;
    		cin.getline ( str, 1024 );
    
    	i = atoi(str);
    	cout << char(i);
    
    	getch();
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a FAQ or two on the correct way to get a line of input. You may want to take a gander at'm.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Oh alright. Thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You need to convert each number entered separately. Use a loop and cin >> to read the numbers directly into the integer i, then cout each one the way you've done. That way you won't need atoi, and each number entered will be converted.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    You mean, like a prompt everytime they want to add a new number?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you just used cin >> then you might have to prompt for each one (or you could use a sentinal value to know when to stop - like 0 or -1).

    Another option if you want them to be able to input multiple characters per line and convert each one is to use getline like you are doing, but then put the line into a stringstream and use the stringstream to extract the integers. This is easier to find the end of input because when the line is done the stream will return false.

    One more option is to parse the string yourself and separate each number. This isn't a very good option IMO, but it works for some people who prefer C or are afraid to use C++.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Daved
    One more option is to parse the string yourself and separate each number. This isn't a very good option IMO, but it works for some people who prefer C or are afraid to use C++.
    That's one of the stupidest statements I've seen all day. String parsing isn't done in C++ any more? :boggle:


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    I'll look into the stringstream way. Thanks for all your help.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    > That's one of the stupidest statements I've seen all day. String parsing isn't done in C++ any more? :boggle:

    Perhaps I should have added the word "style", i.e. C style/C++ style?

    And no, manually parsing a string to look for spaces and separate integers is generally not done in C++, except perhaps as a learning exercise or in the instances I mentioned.
    Last edited by Daved; 07-19-2005 at 10:29 PM.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    And no, manually parsing a string to look for spaces and separate integers is generally not done in C++, except perhaps as a learning exercise or in the instances I mentioned.

    I have yet to find a language style that doesn't include string parsing...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    > I have yet to find a language style that doesn't include string parsing...

    I'm not sure you understood the context of the statement you quoted. I said "manually parsing", meaning writing your own code instead of using built-in or existing tools.

  12. #12
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    well i got an idea, cast the char to an int and youll get a number, and youll have to do something to that number to get the actuall number, figure it out yourself...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  2. 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
  3. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  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