Maybe A Cast Problem?

This is a discussion on Maybe A Cast Problem? within the C++ Programming forums, part of the General Programming Boards category; Ok I have this code, which I want it convert the string answer into all lowercase letters. I'm using strlwr ...

  1. #1
    Me
    Join Date
    Jul 2006
    Posts
    71

    Maybe A Cast Problem?

    Ok I have this code, which I want it convert the string answer into all lowercase letters. I'm using strlwr which you have to use a character array, but when I try to use a character array, it can't be a string(obviously) but when I check if it equals "roll" it doesn't work.

    Anyway, i'm not too sure about how to use a cast in this situation, and I would rather not have to, but if it is the only way to get around this, I guess it will have to do.

    Code:
    int main()
    {
    srand(time(NULL));
    string answer;
    
    do
    {
    cout<<"Enter A Command";
      cin>>answer;
      cin.ignore();
    
    strlwr(answer);
    
     if(answer=="roll")
      {
         roll_dice();
      }
    
    }
    while(1);
    
    cin.get();
    return 1;
    }

    Whoops. Forgot the error it gives me.

    Code:
    cannot convert `answer' from type `string' to type `char *'
    Last edited by relyt_123; 07-29-2006 at 06:22 PM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Make your own function:
    Code:
    #include <string>
    #include <algorithm>
    
    void char_to_lower(char& c)
    {
      // if c is an upper letter
      if(c >= 65 && c <= 90)
        c += 32; // shift to lower character
    }
    
    void string_to_lower(std::string& myStr)
    {
      std::for_each(myStr.begin(), myStr.end(), char_to_lower);
    }
    Haven't compiled, it should be something like this though.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by relyt_123
    Anyway, i'm not too sure about how to use a cast in this situation, and I would rather not have to, but if it is the only way to get around this, I guess it will have to do.
    It's good that you see casting as a last option, because it generally it is. Mostly because it is the incorrect choice most of the time -- like this.
    Quote Originally Posted by Desolation
    Code:
    void char_to_lower(char& c)
    {
      // if c is an upper letter
      if(c >= 65 && c <= 90)
        c += 32; // shift to lower character
    }
    Please try not to be system specific if it isn't necessary.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Me
    Join Date
    Jul 2006
    Posts
    71
    Hmm...never looked at it that way, but it does work, so thanks man.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Huh, I thought char defaulted to ASCII =/ So you're saying char could be something else than ASCII like UTF-8 or Unicode ? =/

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    just use what C++ already offers you. Why complicate?

    Code:
    #include <cctype>
    
    char x = 'A';
    x = tolower(x);
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Me
    Join Date
    Jul 2006
    Posts
    71
    Mario<<Will that work with strings? I'll try it, and go ahead and use that instead if it does, but for now, i'll just use desolations way.

    Anyway I'm back with another question:

    Code:
    int main()
    {
    srand(time(NULL));
    string answer;
    
    do
    {
    cout<<"Enter A Command: ";
      cin>>answer;
      cin.ignore();
    
    string_to_lower(answer);
    
     if(answer=="roll")
      {
      int numberhold;
       do
        {
         roll_dice();
    
          cout<<"How Many Dice Would You Like To Hold?";
          cin>>numberhold;
          cin.ignore();
    
          switch(numberhold)
           {
            case 1:cout<<"test";
            break;
           }
           }
    //Line 64
        }
    
    }
    while(1);
    
    cin.get();
    return 1;
    }
    It seems that there is an error in the switch statement, but I can't seem to find one.

    Error is:

    Code:
    parse error before `}' Line 64
    Edit: Nevermind, stupid mistake, I didn't put the while at the end...

    Edit2:Also I can't get the tolower function to work, it won't go through the if statement if I use it. Hmm, what do you put in the ()'s? Because whatever you put there it print's as an ascii value.
    Last edited by relyt_123; 07-29-2006 at 07:18 PM.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Reusing some code in this thread, to make a string lower case

    Code:
    void string_to_lower(std::string& myStr)
    {
      std::for_each(myStr.begin(), myStr.end(), tolower);
    }
    Watch for the problem which bubbled up in this thread.

    trouble with transform

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    That was meant as a reply to Desolation.

    If you want a less complicated way to change a string case you can use the transform algorithm.

    Code:
    #include <cctype>
    #include <string>
    #include <iterator>
    #include <algorithm>
    
    std::string str = "Hello World";
    std::transform( str.begin(), str.end(), str.begin(), toupper );  // all upper case
    std::transform( str.begin(), str.end(), str.begin(), tolower );  // all lower case
    EDIT: Tonto's option is even easier to read
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Me
    Join Date
    Jul 2006
    Posts
    71
    Ok, thanks it's working perfectly in the aspect of making it lowercase, but otherwise...who knows?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >std::for_each(myStr.begin(), myStr.end(), tolower);
    >std::transform( str.begin(), str.end(), str.begin(), tolower );
    Both of these contain a potential ambiguity that arises due to multiple overloads of tolower in the presence of locales. The chances of hitting the ambiguity are pretty good, even though many compilers will do the right thing. A better solution is to provide a wrapper around tolower that explicitly calls the correct overload:
    Code:
    struct lower_case {
      int operator() ( int ch )
      {
        return tolower ( ch );
      }
    };
    
    std::for_each(myStr.begin(), myStr.end(), lower_case());
    std::transform(str.begin(), str.end(), str.begin(), lower_case());
    My best code is written with the delete key.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    True.

    Or
    Code:
    std::transform( str.begin(), str.end(), str.begin(), (int(*)(int)) tolower );
    Last edited by Mario F.; 07-29-2006 at 08:01 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Or
    No, now you have a problem due to the C functions having "extern C" linkage and transform expecting C++ linkage. Yea, it's messed up, but that officially makes your casting solution non-portable.
    My best code is written with the delete key.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Well, thanks for that prelude. Didn't know about that. Will definitely go with the functor whenever I need this.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Me
    Join Date
    Jul 2006
    Posts
    71
    Ok, new question, how would you check if an int has a char in it in a switch statement? I have this so far.

    Code:
          switch(numberhold)
           {
            case 0:break;
    
            case 1:cout<<"int = 1";
            break;
    
            case 2:cout<<"int = 2";
            break;
    
            case 3:cout<<"int = 3";
            break;
    
            case 4:cout<<"int = 4";
            break;
    
            case 5:cout<<"int = 5";
            break;
    
            default:cout<<"int <0 or >5";
            break;
    
    
           }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  2. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 03:46 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. Microsoft Visual C++ compiler, cast problem?
    By jonnie75 in forum C Programming
    Replies: 5
    Last Post: 11-10-2001, 08:53 AM

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