Like Tree1Likes
  • 1 Post By Elysia

Confusion

This is a discussion on Confusion within the C++ Programming forums, part of the General Programming Boards category; I am somewhat baffled. I read a string literal from the keyboard and want to print it in reverse. Quite ...

  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 10:36 AM. Reason: Further observation

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    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]?
    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
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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;
    Imanuel 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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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 11:03 AM.

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-11-2006, 11:26 PM
  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, 05:10 AM
  5. C++ confusion
    By charlie in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2001, 05:46 AM

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