Like Tree1Likes
  • 1 Post By Elysia

String Problem

This is a discussion on String Problem within the C++ Programming forums, part of the General Programming Boards category; I have some code, that have error that I cannot repair by myself: Code: int main(){ string a; int i; ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    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 12:08 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    What is the type of "a"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    > 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" !!
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    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".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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 !
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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,866
    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
    Posts
    23,010
    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).
    AndrewHunter likes this.
    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,866
    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, 06:25 AM
  2. Replies: 22
    Last Post: 07-28-2011, 02: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, 11:17 AM

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