CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2002
    Location
    California
    Posts
    11

    Red face newbie....help please!!!

    Hi all!


    Suppose I have a input char* tmp = "A12" and it'll match one of 3 cases:

    if(the first char = A)
    then store the value after A ( which is 12) into a integer var
    else if( the first char = B)
    then store the value after B into a integer variable
    else if( the first char = C)
    then store the value after C into a integer variable
    else
    //do nothing

    Please help with the coding. Thanks in advance.

    Dxxv

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    If you're definitely going to be working with c-style strings, I'd
    do something like this:
    Code:
    char* temp = "A12";
    int aValue = 0;
    int bValue = 0;
    int cValue = 0;
    
    switch (temp[0])
    {
    case 'A':
       aValue = atoi(&temp[1]);
       break;
    case 'B':
       bValue = atoi(&temp[1]);
       break;
    case 'C':
       cValue = atoi(&temp[1]);
       break;
    default: /* do nothing */
       break;
    }
    You could even create a wrapper class [or struct] whose interface
    might look something like this:
    Code:
    class Conversion
    {
    public:
       Conversion();
    
       enum eConversionType {A, B, C};
    
       void setBuffer(const std::string& buf);
       int getValue() const;
       eConversionType getType() const;
    
    private:
       std::string m_buf;
    }
    You'd have to tinker with its interface to get what you want. You
    could even forget about the class and just go with a function to
    do your string conversion.

    --Paul

  3. #3
    Join Date
    Sep 2002
    Posts
    1,747

    pseudo code answer

    The basic steps then would be

    • Get first char (tmp[0]) and check if alphabetical (isalpha)
    • if so, convert rest of string to number (atoi(&(tmp[1]), ...) or stream conversion or whatever you are comfortable with)


    You did the basic outline in your original post, so I am not sure what you need help with. Is it the check (I showed isalpha, but there are other methods)? Or is it the conversion (I showed atoi, but again many ways...)? If you clarify, I can definitely help you out better.

    [EDIT:] Posted late. And notice now that you want to check only a-c. Previous post should help.

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    11
    Thanks all.
    I got this to work. However I've another complicated case I need your helps, please.
    If I have an input char*var = "A12B3C4". A,B,C can be in any order. How do I break this string and store the value like the previous msg?

    Thanks
    Dxxv

  5. #5
    Join Date
    Oct 2002
    Posts
    6
    Here is get the locations of where all the alpha characters are...


    PHP Code:
    #include <iostream.h> //for cout
    #include <iomanip.h> // for the endl function
    #include <string.h> //string manup stuff
    #include <ctype.h>  //for the isalpha, atoi, or atol, or atof functions

    void mainvoid )
    {

     
    char *var = "A12B3C4";
     const 
    int lenght strlen(var);
     
    int index 0;
     
    int counter 0;
     
    int letter[128];  //place to store addresses to the alpha letters.


     
    cout << "var is " << var << endl;
     
    cout << "length of var is " << strlen(var) << endl;

     for (
    int i=0;i<strlen(var);++i)   {
         if (
    isalpha(var[i])) {
           
    counter++;
           
    letter[index++] = i;
         }
     }
       
     
    cout << "address locations of all alpha letters" << endl;
      
     for (
    int j 0counter; ++j) {
         
    cout << "letter[" << << "] = " << letter[j] << endl
      }

     



    Once you get the locations, you can use the string.h function:


    char *strncpy(const char *string1,const char *string2, size_t n) -- Copy first n characters of string2 to stringl

    Use the alpha addresses locations and put it into the strncpy function, and extract the data and put it into a char *, or int variable.


    I left out the above part because it is an string.h excercise.
    You can then use PaulWends' suggestion and use mine to
    get what you want..

    I hope this helps.

  6. #6
    Join Date
    Oct 2002
    Posts
    6
    Here's an example of what you could to with strncpy:

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <iomanip.h>
    
    void main(void)
    {
      char *var = "A12B3C4";
      char s[128] = "";
      cout << "strncpy(s,var,3)=" << strncpy(s,var,3) << endl;
      cout << "strncpy(s,var[3],3)=" << strncpy(s,&var[3],2) << endl;
      cout << "strncpy(s,var[5],3)=" << strncpy(s,&var[5],2) << endl;
    }

  7. #7
    Join Date
    Oct 2002
    Posts
    6
    oops forgot to put the output:

    strncpy(s,var,3)=A12
    strncpy(s,var[3],3)=B32
    strncpy(s,var[5],3)=C42

  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I didn't bother looking at crazycliffy's suggestions, but I couldn't
    help but notice his usage of iostream.h and iomanip.h.
    These files are part of an older version of the C++ standard and
    this is not how they should be referenced nowadays. This usage
    is deprecated and isn't guaranteed to work on other compilers [or
    even on a future version of Microsoft's compiler].

    So, my suggestion is to kick the habit now:

    Code:
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <cstring> // for the string.h functions in C
    
    // uncomment the following line to make things easy
    // on yourself.
    // using namespace std;
    
    int main(int argc, char* argv[])
    {
       // you can choose to fully qualify the string
       // name from the std namespace this way instead:
       std::string temp;
    
       return 0;
    }
    This isn't an attempt to nitpick or belittle; I'm just trying to help.

    --Paul

  9. #9
    Join Date
    Oct 2002
    Location
    India
    Posts
    11
    Paul,
    i am also used to stuff like
    iostream.h and
    iomanip.h ....
    which u said is part of an older version of the C++ standard.

    Can u pls let me know some tutorials/links where i can get to know how they are referenced nowadays.

    Regards
    Debkumar

  10. #10
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I don't know of any links or anything; I can refer you to books,
    though. Everything in the C++ Standard Library is in the std
    namespace. Everything in the new header files [basically the
    same as the old ones, but without the .h] need to be fully
    qualified or you to use "using" in order to bring their name out.
    If you have The C++ Programming Language, check out
    chapter 16.1.2; Stroustrup put all of the header files in there.

    --Paul

  11. #11
    Join Date
    Oct 2002
    Posts
    6

    Thumbs up

    i apologize for the improper header usage.
    i wasn't really thinking of a c++ solution,
    but more of a quick suggestion, to the problem posted.

    next time i'll put review future code and make
    to Stroustrup standard.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured