C++ String to Integer

Discussion in 'Programmer's Corner' started by TheFox, Jun 30, 2010.

  1. TheFox

    Thread Starter Active Member

    Apr 29, 2009
    66
    5
    What I am doing should seem very possible, however, it is not working for me. I am trying to get an input and get the number of characters in the string, and put that as an integer of some kind. every time I try a various string of characters, it gives the same negative number. It is also always the same number, -858993460.

    Code ( (Unknown Language)):
    1.  
    2. int encryption(string encry)
    3. {
    4.     int encry_size;
    5.     string encry2;
    6.     encry2=encry;
    7.     stringstream convert(encry.size());
    8.     convert >> encry_size;
    9.     cout<<encry_size<<"\n";
    10.     return encry_size;
    11. }
    12.  
    If you are to print the value of "encry2" it will give you a spade, like from a deck of cards.
     
  2. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    strlen should do the trick.

    Code ( (Unknown Language)):
    1. /* strlen example */
    2. #include <stdio.h>
    3. #include <string.h>
    4.  
    5. int main ()
    6. {
    7.   char szInput[256];
    8.   printf ("Enter a sentence: ");
    9.   gets (szInput);
    10.   printf ("The sentence entered is %u characters long.\n",strlen(szInput));
    11.   return 0;
    12. }
    This is quoted from:
    http://www.cplusplus.com/reference/clibrary/cstring/strlen/
     
Loading...