You are here:

C++/string manipulation

Advertisement


Question
How do i write a c++ statement that assigns the 34 in the part variable to the code variable?
assuming: string part = "ABCD34G";
         int code    = 0;

Answer
string part = "ABCD34G";
  int code    = 0;
 
  int ilen = part.length();
  char * temp = new char[ilen];
  int itemp=0,icount =0;
  part.copy(temp,ilen);
  for(int i = 0 ;i<ilen;i++)
  {
         switch(temp[i])
         {
         case   48:
         case   49:
         case   50:
         case  51:
         case  52:
         case  53:
         case  54:
         case  56:
         case  57:
         case  58:
         {
         code =  atoi(&temp[i]);
         cout<<code;
         } break;
         default:
         break;
         }
 }  
I have written the code which has some error. Please, check the copying part of the string into number.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


rakshitha

Expertise

Difficult questions related to C .

Experience

C ,vc ,opc ,windows CE, ActiveX

Education/Credentials
Masters in electrical engineering

©2016 About.com. All rights reserved.