0

the code that I'm currently working with looks like this:

if ((text.at(text.size() -1 ) != '!' ) && (text.at(text.size() -1 ) != '?') && (text.at(text.size() -1 ) != ':' ) && (text.at(text.size() -1 ) != ',' ) && (text.at(text.size() -1 ) != ';' ) && (text.at(text.size() -1 ) != '=' ) )

The details of it aren't terribly important but I'm basically trying to make a kind of word processor that only uses the command prompt, and whenever the user presses enter with nothing in the input buffer, the program closes because it's being terminated in "an unusual way" which I'm taking to mean that this line of code is trying to reference a position in the string that doesn't exist, i.e. -1. So I need a new way to reference the last character in a given string variable that can still work with the line of code given. Thanks for your help!

asked Sep 18, 2013 at 9:34
2
  • 2
    Check for !text.empty() first. Commented Sep 18, 2013 at 9:39
  • 1
    string::at() also throws an out_of_range exception if you try to go beyond its bounds. Commented Sep 18, 2013 at 9:46

2 Answers 2

2

You should check text is not empty before access its element and use std::string::rbegin or std::string::back(needs C++11) to simplify your code.

you call it this way:

if (!text.empty())
{
 char c = *text.rbegin();
 if (c != '!' && 
 c != '?' && 
 c != ':' && 
 c != ',' && 
 c != ';' && 
 c != '=' )
 {
 }
}

To enhance code readability, you could introduce a local variable in this situation, it's cheap to make a copy of a char and you can shorten your if statement a lot.

answered Sep 18, 2013 at 9:37
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks a bunch but when I try to compile this code, i get the error "'rbegin' was not declared in this scope". Is there a library that I need to declare prior to using this function other than <string>?
you call lit this: *text.rbegin() != '!', right? It doesn't need other lib
Ohh I see. I'm new to this, I was trying to something like (text.at(rbegin()) != '!' ). Cheers for the help!
@BenjaminLindley Did I miss something?
Before, he was using at on an empty string, and getting an exception. You've simply modified it to dereference an invalid iterator, leading to undefined behavior. He probably thinks the problem is solved, because the program isn't crashing, because instead of an exception, he's getting undefined behavior.
|
2

Seems easy enough, just check for text.size() > 0 first

if (text.size() > 0 && 
 text.at(text.size() - 1) != '!' && 
 text.at(text.size() - 1) != '?' && 
 text.at(text.size() - 1) != ':' && 
 text.at(text.size() - 1) != ',' && 
 text.at(text.size() - 1) != ';' && 
 text.at(text.size() - 1) != '=')

or maybe you intended this logic (it's not really clear from your question)

if (text.size() == 0 || 
 (text.at(text.size() - 1) != '!' && 
 text.at(text.size() - 1) != '?' && 
 text.at(text.size() - 1) != ':' && 
 text.at(text.size() - 1) != ',' && 
 text.at(text.size() - 1) != ';' && 
 text.at(text.size() - 1) != '='))

You should also simplify that expression, it's way too complex.

answered Sep 18, 2013 at 9:41

2 Comments

Thanks a lot, this should work as well. I it's crude but I'm just reading a book on how to code and was getting bored with the examples they were giving so I wanted to try something a bit different. I'm sure it will get better with time. Thanks for your input :)
Actually this will really come in handy for translating new paragraphs, so you've answered a question that I probably just hadn't got around to answering yet :D

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.