Thread: String Problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question String Problem

    I have some code, that have error that I cannot repair by myself:
    Code:
    int main(){
       string a;
       int i;
       for(i=0;i<=a.size()-1;i++){
           if(a[i]!="b"){
              //do something
       }
    }
    at line : if(a[i]!="b" have error: ISOC++ forbids comparison between pointer and integer.
    So, who help me on this error, please.

    thanks
    Last edited by hqt; 09-19-2011 at 11:08 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the type of "a"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question

    I don't know why your ask like it: because my code isn't clear, or you want me to look more deeply my code, because it's question too stupid.
    Of course, type of a is string, and a[i] is a character(I think)

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    > a[i]!="b"

    Here a[i] is a char and "b" is possibly a const char *

    If you want the character 'b' ; say 'b' !


    Edit: Now I see... Laserlight didn't ask you the type of a , It was "a" !!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by hqt
    I don't know why your ask like it: because my code isn't clear, or you want me to look more deeply my code, because it's question too stupid.
    Of course, type of a is string, and a[i] is a character(I think)
    I made a typographical error: I intended to ask what is the type of "b".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just a heads up, but this
    >>for(i=0;i<=a.size()-1;i++)
    Is common written as
    for(int i = 0; i < a.size(); i++)

    You may do as you please, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    Just a heads up, but this
    >>for(i=0;i<=a.size()-1;i++)
    Is common written as
    for(int i = 0; i < a.size(); i++)
    And is uncommonly written as
    Code:
    for(auto i : a)
    No 'messy' operators !

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by hqt View Post
    I have some code, that have error that I cannot repair by myself:
    Code:
    int main(){
       string a;
       int i;
       for(i=0;i<=a.size()-1;i++){
           if(a[i]!="b"){
              //do something
       }
    }
    at line : if(a[i]!="b" have error: ISOC++ forbids comparison between pointer and integer.
    So, who help me on this error, please.

    thanks
    OK, I will ask. What are you trying to do? Big picture here.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Try this:
    Code:
    int main()
    {
        string a = "abc";
        int i;
        for(i = 0; i <= (int)a.size()-1; i++)
        {
            if(a[i] != 'b')
            {
                cout << "Not b, found " << a[i] << endl;
            }
            else
                cout << "Found b" << endl;
        }
    
        return 0;
    }
    Quote Originally Posted by hqt View Post
    I have some code, that have error that I cannot repair by myself:
    Code:
    int main(){
       string a;
       int i;
       for(i=0;i<=a.size()-1;i++){
           if(a[i]!="b"){
              //do something
       }
    }
    at line : if(a[i]!="b" have error: ISOC++ forbids comparison between pointer and integer.
    So, who help me on this error, please.

    thanks

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    There is no need for a cast, let alone a C style cast here.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But then again, i shouldn't be an int either. It should be a std::string::size_t or std::size_t. By removing the cast, you may get a warning (unless using correct type for i).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    For some reason my compiler is giving me this:

    "warning C4018: '<=' : signed/unsigned mismatch" when I don't cast. No errors when I do...any ideas why that is?

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by rmatze View Post
    For some reason my compiler is giving me this:

    "warning C4018: '<=' : signed/unsigned mismatch" when I don't cast. No errors when I do...any ideas why that is?
    Yes, I do:
    Quote Originally Posted by Elysia View Post
    But then again, i shouldn't be an int either. It should be a std::string::size_t or std::size_t. By removing the cast, you may get a warning (unless using correct type for i).
    My main point was the solution wasn't optimal depending on what the OP was exactly trying to do.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Ahhh yes, I read over your post to fast. Fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-10-2011, 05:25 AM
  2. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  3. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  4. Replies: 0
    Last Post: 04-05-2003, 09:33 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM