Error C2664

This is a discussion on Error C2664 within the C++ Programming forums, part of the General Programming Boards category; I am trying to convert the strings into inegers in this code: Code: string line; string oct1; string oct2; string ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    9

    Error C2664

    I am trying to convert the strings into inegers in this code:

    Code:
    string line;
    	string oct1;
    	string oct2;
    	string oct3;
    	string oct4;
    
    	int i = 0;
    
    	while ( ! in.eof())
    	{
    	  while(getline(in,line))
    	  {
    
    		while(line[i] != '.')
    		{	
    			oct1 = oct1 + line[i];
    			i++;
    		}
    
    		cout<<oct1<<"   ";
    		i++;
    
    		while(line[i] != '.')
    		{	
    			oct2 = oct2 + line[i];
    			i++;
    		}
    		cout<<oct2<<"   ";
    		i++;
    
    		while(line[i] != '.')
    		{	
    			oct3 = oct3 + line[i];
    			i++;
    		}
    		cout<<oct3<<"   ";
    
    		i++;
    	
    		while(i != line.length())
    		{	
    			oct4 = oct4 + line[i];
    			i++;
    		}
    		
    		cout<<oct4<<endl;
    	  }
    
    	}
    oct 1, 2, 3, and 4 I am trying to convert to an integer
    When I use the atoi or the sscanf commands this error comes up

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    atoi takes a c character array rather than a std::string object. Fortunately, we can get a character array from a std::string with the c_str() method.

    So instead of:
    Code:
    i =  atoi(oct4); /* This will cause an error. */
    we use
    Code:
    i = atoi(oct4.c_str()); /* This will work. */
    You may be interested in the inet_addr function.

    For future reference:
    • Please post the actual error text.
    • Please post the line that is causing the error.
    • Please post C questions in the C forum and C++ questions in the C++ forum.
    Last edited by anonytmouse; 12-11-2004 at 03:26 AM.

Popular pages Recent additions subscribe to a feed

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