Thread: Confusion

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    Confusion

    I am somewhat baffled.

    I read a string literal from the keyboard and want to print it in reverse. Quite simple.

    Code:
    	string str;
    	getline(cin, str);
    	for(string::size_type x = str.size(); x >= 0; x--)
    		cout << str[x];
    	cout << endl;
    Ok and on my Mac in Xcode I get "Bad Exec" and on my PC using Ubuntu it doesn't even run.

    Anyone care to explain why this does not work?

    It is interesting to me that x != 0 results in a reversal of the string less the last index. x != -1 works fine but x >= 0 results in "EXEC_BAD_ACCESS"?
    Last edited by Imanuel; 01-13-2012 at 11:36 AM. Reason: Further observation

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose the user enters "Confusion" as the input. On the first iteration of the loop, x = str.size(). str.size() == 9. 9 >= 0, so the programs prints str[9]. Now, str[9] is... oh wait, what's str[9]?
    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
    May 2010
    Posts
    4,633
    You are trying to access the string outside it's bounds. In the following code:
    Code:
    for(string::size_type x = str.size(); x >= 0; x--)
    The operator>= is the problem you should just be using the operator>. When I compile your code I got this warning:
    main.cpp|13|warning: comparison of unsigned expression >= 0 is always true|
    std::string::size_type is an unsigned type.

    Jim

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by laserlight View Post
    Suppose the user enters "Confusion" as the input. On the first iteration of the loop, x = str.size(). str.size() == 9. 9 >= 0, so the programs prints str[9]. Now, str[9] is... oh wait, what's str[9]?
    x = str.size() - 1 yes I simply forgot that there. However, it still generates Bad access with x >= 0.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    See the reply above yours.
    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.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    You are trying to access the string outside it's bounds. In the following code:
    Code:
    for(string::size_type x = str.size(); x >= 0; x--)
    The operator>= is the problem you should just be using the operator>. When I compile your code I got this warning:

    std::string::size_type is an unsigned type.

    Jim
    But only using > results in all but index 0 having its contents printed. Something is very wrong with the way the interpretation of this code is being executed by the compiler. x is unsigned as is 0 unsigned. Nothing wrong with that. I am using unsigned values to reference the indicies of my string. thus, std::string::size_type x = s.size() - 1; x >= 0 is legitimate and valid as 0 is an unsigned constant.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nothing is wrong with the compiler. The problem is that when the count goes < 0, it wraps around because it's unsigned.
    One solution might be

    Code:
    string str;
    getline(cin, str);
    for(string::size_type x = str.size(); x > 0; x--)
        cout << str[x - 1];
    cout << endl;
    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.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    178
    Quote Originally Posted by Elysia View Post
    Nothing is wrong with the compiler. The problem is that when the count goes < 0, it wraps around because it's unsigned.
    One solution might be

    Code:
    string str;
    getline(cin, str);
    for(string::size_type x = str.size(); x > 0; x--)
        cout << str[x - 1];
    cout << endl;
    Why didn't I think of that in the first place. I feel worse than confused now. Thank you for your assistance.

    I feel as though i have been slapped in the face with a "HEY STUPID .. WAKE UP!!! UNSIGNED VALUES CAN NEVER BE LESS THAN ZERO"
    Last edited by Imanuel; 01-13-2012 at 12:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. g++ -r Confusion
    By skewray in forum Linux Programming
    Replies: 7
    Last Post: 09-30-2007, 06:00 PM
  2. If else confusion
    By metaTron in forum C Programming
    Replies: 6
    Last Post: 02-12-2006, 12:26 AM
  3. Confusion with * and &
    By Ganoosh in forum C++ Programming
    Replies: 32
    Last Post: 06-23-2005, 10:16 PM
  4. confusion
    By Penlan in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2003, 06:10 AM
  5. C++ confusion
    By charlie in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2001, 05:46 AM