INT to STRING

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • PentiumPunk

    INT to STRING

    Hey, I am trying to get a character, check its ascii value, if the ascii is
    less than 100 then I append a 0 on the front of it, so it will have 3
    digits. That is my aim to have a 3 digit integer to add to my hash table
    that corresponds to the ascii of a letter.

    i tried:

    char ch;
    int i, j;
    string s, s2;

    fin>>str; //input string from file
    for(i=0;i<str.s ize();i++)
    {
    ch=str[i];
    if(ch<100)
    { j=ch; // assigns ascii number to j
    s=j;
    s2="0"+s;
    cout<<s2;
    }
    }
    it just outputs 0b ("bird" is the word that is input. and since b's ascii is
    98, its supposed to output 098 ). how can i fix this? Thanks in advance!
    ~PentiumPunk~


  • John Carson

    #2
    Re: INT to STRING

    "PentiumPun k" <broox@triad.rr .com> wrote in message
    news:PNObb.6639 $vq1.4529@twist er.southeast.rr .com[color=blue]
    > Hey, I am trying to get a character, check its ascii value, if the
    > ascii is less than 100 then I append a 0 on the front of it, so it
    > will have 3 digits. That is my aim to have a 3 digit integer to add
    > to my hash table that corresponds to the ascii of a letter.
    >
    > i tried:
    >
    > char ch;
    > int i, j;
    > string s, s2;[/color]

    str not declared.
    [color=blue]
    >
    > fin>>str; //input string from file
    > for(i=0;i<str.s ize();i++)
    > {
    > ch=str[i];
    > if(ch<100)
    > { j=ch; // assigns ascii number to j
    > s=j;[/color]

    s now stores the character b since s = j does not convert b to a text string
    of its ascii number.
    [color=blue]
    > s2="0"+s;[/color]

    now you concatenate 0 and b to give you 0b.
    [color=blue]
    > cout<<s2;
    > }
    > }
    > it just outputs 0b ("bird" is the word that is input. and since b's
    > ascii is 98, its supposed to output 098 ). how can i fix this? Thanks
    > in advance! ~PentiumPunk~[/color]

    Replace

    s = j;
    s2="0"+s;

    with

    ostringstream oss;
    oss << j;
    s=oss.str();
    s2="0"+s;

    Better yet, replace it with

    ostringstream oss;
    oss << "0" << j;
    s2 =oss.str();

    You may want to move the declaration of oss outside the loop.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)

    Comment

    • Kevin Goodsell

      #3
      Re: INT to STRING

      PentiumPunk wrote:
      [color=blue]
      > Hey, I am trying to get a character, check its ascii value, if the ascii is
      > less than 100 then I append a 0 on the front of it, so it will have 3
      > digits. That is my aim to have a 3 digit integer to add to my hash table
      > that corresponds to the ascii of a letter.[/color]

      The fact that you must use ASCII complicates matters - you'll have to
      have some kind of table of ASCII codes in your program to look up the
      correct value (since you can't count on the implementation character set
      being ASCII).

      Given a std::map<char, int> ascii_codes that maps characters to their
      ASCII code (building the map is left as an exercise to the reader), you
      can do this (assuming appropriate declarations):

      fin >> str;
      char f = cout.fill('0');
      for (i=0; i<str.size(); ++i)
      {
      ch = str[i];
      cout << setw(3) << ascii_codes[ch] << ' ';
      }
      cout.fill(f);

      -Kevin
      --
      My email address is valid, but changes periodically.
      To contact me please use the address from a recent posting.

      Comment

      Working...