Debugging issue

This is a discussion on Debugging issue within the C++ Programming forums, part of the General Programming Boards category; I'm trying to debug a fairly simple function that is suppose to take a string and convert any lowercase letters ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    Question Debugging issue

    I'm trying to debug a fairly simple function that is suppose to take a string and convert any lowercase letters to uppercase. Unfortanitly I am getting a large number of errors I do not know how to fix (using codeblocks)

    the code is as follows:
    Code:
    /*
    This function accepts a string and returns a string with any lowercase letters found in it to uppercase letters
    @param inputString:  This is the string to be converted
    @return: The upperCase string
    */
    
    string toUpper(string inputString)
    {
        for(int i=0; i<inputString.length(); i++)
        {
                if(inputString[i]=="a")
                inputString[i]="A";
                else if(inputString[i]=="b")
                inputString[i]="B";
                else if(inputString[i]=="c")
                inputString[i]="C";
                else if(inputString[i]=="d")
                inputString[i]="D";
                else if(inputString[i]=="e")
                inputString[i]="E";
                else if(inputString[i]=="f")
                inputString[i]="F";
                else if(inputString[i]=="g")
                inputString[i]="G";
                else if(inputString[i]=="h")
                inputString[i]="H";
                else if(inputString[i]=="i")
                inputString[i]="I";
                else if(inputString[i]=="j")
                inputString[i]="J";
                else if(inputString[i]=="k")
                inputString[i]="K";
                else if(inputString[i]=="l")
                inputString[i]="L";
                else if(inputString[i]=="m")
                inputString[i]="M";
                else if(inputString[i]=="n")
                inputString[i]="N";
                else if(inputString[i]=="o")
                inputString[i]="O";
                else if(inputString[i]=="p")
                inputString[i]="P";
                else if(inputString[i]=="q")
                inputString[i]="Q";
                else if(inputString[i]=="r")
                inputString[i]="R";
                else if(inputString[i]=="s")
                inputString[i]="S";
                else if(inputString[i]=="t")
                inputString[i]="T";
                else if(inputString[i]=="u")
                inputString[i]="U";
                else if(inputString[i]=="v")
                inputString[i]="V";
                else if(inputString[i]=="w")
                inputString[i]="W";
                else if(inputString[i]=="x")
                inputString[i]="X";
                else if(inputString[i]=="y")
                inputString[i]="Y";
                else if(inputString[i]=="z")
                inputString[i]="Z";
        }
    
    }
    The main errors I am getting are:
    /home/hewhosurvives/c++/order/item.h||In member function ‘bool item::load(std::ifstream&)’:|
    /home/hewhosurvives/c++/order/item.h|101|warning: comparison between signed and unsigned integer expressions|
    /home/hewhosurvives/c++/order/functions.h||In function ‘std::string toUpper(std::string)’:|
    /home/hewhosurvives/c++/order/functions.h|21|warning: comparison between signed and unsigned integer expressions|
    /home/hewhosurvives/c++/order/functions.h|23|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|23|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|24|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|25|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|25|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|26|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|27|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|27|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|28|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|29|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|29|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|30|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|31|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|31|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|32|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|33|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|33|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|34|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|35|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|35|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|36|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|37|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|37|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|38|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|39|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|39|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|40|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|41|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|41|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|42|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|43|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|43|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|44|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|45|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|45|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|46|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|47|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|47|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|48|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|49|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|49|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|50|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|51|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|51|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|52|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|53|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|53|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|54|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|55|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|55|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|56|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|57|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|57|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|58|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|59|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|59|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|60|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|61|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|61|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|62|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|63|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|63|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|64|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|65|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|65|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|66|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|67|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|67|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|68|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|69|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|69|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|70|error: invalid conversion from ‘const char*’ to ‘char’|
    /home/hewhosurvives/c++/order/functions.h|71|warning: comparison with string literal results in unspecified behaviour|
    /home/hewhosurvives/c++/order/functions.h|71|error: ISO C++ forbids comparison between pointer and integer|
    /home/hewhosurvives/c++/order/functions.h|72|error: invalid conversion from ‘const char*’ to ‘char’|
    ||More errors follow but not being shown.|
    ||Edit the max errors limit in compiler options...|
    ||=== Build finished: 50 errors, 27 warnings ===|

    Any ideas how I can fix it or what I am doing wrong?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    A string is composed of characters. For example, "hello" is

    Code:
    { 'h', 'e', 'l', 'l', 'o', 0 }
    so you would use the same syntax if you were assigning a part of it like
    s[3] = 'L';

    Now, do you know of any easier way to do this than a long if/else if/else tree?

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    hint: look into the standard library

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    Hint: look at documentation about C++ functions located in the standard library.
    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.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    What a nasty toUpper function. I think the errors are fairly straightforward there.

  6. #6
    Nor
    Nor is offline
    ‡ †hë Ö†hÈr sîĐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    there is a special string function in <string.c> i think.
    if you want to right your own try something like

    Code:
    string toUpper(string inputString)
    {
        for(int i=0; i<inputString.length(); i++)
        {
                if( inputString[i] < 'Z' )
                inputString[i]+= ('a' - 'A'); //  'a' - 'A' = i32
                    }
    
    }
    In the charter chart all lowercase letters are 32 bytes before their capital counterpart. or something like that.
    BUT your code is not very fast due to the string class.

    this would be faster
    Code:
    void toUpper(char* inputString)
    {
        //size_t and unsigned int(32/64) are one and the same
        size_t length = strlen(inputString);
    
        for(;length; length--, inputString++)
        {
                if( *inputString <= 'A' )
                    *inputString += ('a' - 'A'); //  'a' - 'A' = i32
        }
    
    }
    I may be wrong but i think
    Code:
    std::string Myname = "Nor";
    toUpper(Myname);
    std::cout << Myname;
    will work. not to sure but i belive std::string converts to char*
    Last edited by Nor; 11-03-2008 at 08:54 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  7. #7
    Nor
    Nor is offline
    ‡ †hë Ö†hÈr sîĐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    at a second look. if you want to do a major comparision like in your if/else block.
    try a switch()case 'a':break;case'b'break.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I can find at least a dozen things wrong in Nor's posts. All the replies above them are fine.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Quote Originally Posted by cyberfish View Post
    I can find at least a dozen things wrong in Nor's posts..
    I'm game:
    1. No capitals!
    2. It should be ctype.c, not string.c.
    3. It shouldn't even be ctype.c, but cctype.
    4. It shouldn't even be cctype, because we are using C++ strings.
    5. "right" should be "write"
    6. No comma between "own" and "try".
    Half way there in the first two lines and I haven't even touched the code!
    Whoops, there goes my attention span ...

  10. #10
    Nor
    Nor is offline
    ‡ †hë Ö†hÈr sîĐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    anything wrong with the stuff in the code blocks. besides the #include??????
    line 8: 'A' different
    Last edited by Nor; 11-03-2008 at 09:59 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    This is what happens during this moron thing I do called testing ...

    Before: pLease ... be kind, debug before POSTING!!
    After : pLease@NNN@be@kindL@debug@before@POSTINGAA

    Something seem wrong to you?
    Take the hint.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    there is a special string function in <string.c> i think.
    It is in <cctype>, and it doesn't work on strings, but work on chars.

    Code:
    
    string toUpper(string inputString)
    {
        for(int i=0; i<inputString.length(); i++)
        {
                if( inputString[i] < 'Z' )
                inputString[i]+= ('a' - 'A'); //  'a' - 'A' = i32
                    }
    
    }
    1. Indentation!
    2. the function should return a string
    3. "if( inputString[i] < 'Z' )" what if it's not a letter?
    4. it will only work if the computer uses ASCII. some don't

    Code:
    
    void toUpper(char* inputString)
    {
        //size_t and unsigned int(32/64) are one and the same
        size_t length = strlen(inputString);
    
        for(;length; length--, inputString++)
        {
                if( *inputString <= 'A' )
                    *inputString += ('a' - 'A'); //  'a' - 'A' = i32
        }
    
    }
    This is known as premature optimization. Worse still, in this case, the code is probably not any faster if not slower, and definitely a lot less readable. It is pointless.

    Code:
    std::string Myname = "Nor";
    toUpper(Myname);
    std::cout << Myname;
    Yes, you are indeed wrong.

    1. the string is passed by value (copied, the original string is not modified)
    2. A std::string cannot be converted to a char* like that. That's what the c_str() member function of the std::string class is for. Even that will only give you a "const char *", so you cannot modify it inside the function.

    It may sound a little hypocritical, but when I said I can find a dozen things wrong in your post, I wasn't referring to the English, but the code. It's an C++ forum afterall, and not everyone's native language is English (mine isn't either).

    Anyways, I would've implemented it like this -
    Code:
    #include <cctype>
    ...
    void str_to_upper(std::string &str) { //pass by reference
        for (size_t i = 0; i < str.size(); i++) {
            str[i] = (char) toupper(str[i]); //toupper returns the input if the input has no uppercase equiv.
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. debugging - step into issue
    By barneygumble742 in forum C++ Programming
    Replies: 7
    Last Post: 03-15-2009, 01:30 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  3. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  4. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  5. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 08:35 PM

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