string::assign for int type?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kenneth6
    New Member
    • Mar 2007
    • 79

    #1

    string::assign for int type?

    name & line are string type;
    age is int type.

    Code:
    size_t found_2=line.find("\s", found_1);	
    name.assign(line,found_1,found_2);
    			
    size_t found_3=line.find("\s", found_2);
    age.assign(line,found_2,found_3);
    when i debug, msvc told me assign is a function for string only. how can i do the same job for int type?
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    Originally posted by kenneth6
    name & line are string type;
    age is int type.

    Code:
    size_t found_2=line.find("\s", found_1);	
    name.assign(line,found_1,found_2);
    			
    size_t found_3=line.find("\s", found_2);
    age.assign(line,found_2,found_3);
    when i debug, msvc told me assign is a function for string only. how can i do the same job for int type?
    I'm not sure what your intent is here. Are you extracting a number (as text) from the string 'line' and you want to store the value in the int 'age'?

    If so you'll have to convert the substring to int first. If this is not what your intending then I need more clarification.

    Comment

    • kenneth6
      New Member
      • Mar 2007
      • 79

      #3
      Originally posted by mac11
      I'm not sure what your intent is here. Are you extracting a number (as text) from the string 'line' and you want to store the value in the int 'age'?

      If so you'll have to convert the substring to int first. If this is not what your intending then I need more clarification.
      Code:
      #include <iostream>
      #include <stdlib.h>
      #include <string>
      using namespace std;
      
      
      int main()
      {
      	string str1[3];
      	string aray[3];
      	string str1[0]={123450};
      	string str1[1]={123451};
      	string str1[2]={123452};
      	
      	for (int i=0; i<2; i++)
      	{
      		cout << str1[i] << endl;
      	}
      
      	for(int j=0; j<100; j++)
      	{
      		int aray[j] = atoi(str1[j].c_str());
      	}
      
      	for (int i=0; i<2; i++)
      	{
      		cout << aray[i] << endl;
      	}
      }
      Yes, i want to do the job u said. but what you mean by convert the substring to int first ?? is that what i did in the above?
      What's wrong with that code? Many errors

      Comment

      • kenneth6
        New Member
        • Mar 2007
        • 79

        #4
        i solved the problem on my own !! thx u all

        some definition problem in the codees...

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          I hope you found out that string only uses the char data type.

          wstring uses wchar_t and these are the only teo types u you can use in basic_string<>.

          Comment

          Working...