-1

I'd like to preface this by saying I am new to programming but I am working on a little project and have everything figured out except this one little part, I can't find it online and can't think it through for whatever reason. I need to display a string backwards but the way I have coded it, it displays each character on a new line. I want to display it on the same line. If I add an endl to the end it has the "Press any key to continue..." message which I do not want.

if (sel == 4)
        {
            for (a = sent.length() - 1; a >= 0; a--)
            {
                cout << sent.at(a) << endl;
            }
            break;
        }

So essentially, the user inputs a string, I store it as "sent", then they are asked to enter a selection which is stored as "sel" 1 through 4, each does something different to the string (uppercase, lowercase, etc) but 4 will display it backwards, it works now, but when it displays the string, the message "Press any key to continue..." appears (as it should) I just can't figure out a way around it.

zhodges10
  • 5
  • 3
  • 1
    You are aware that `endl` stands for "end of line", right? You explicitly emit it after every character, and are then surprised that every character is on a new line. – Igor Tandetnik Oct 16 '17 at 02:46
  • I do, as I said in the original post, ""Press any key to continue..." appears (as it should) I just can't figure out a way around it." – zhodges10 Oct 16 '17 at 02:56
  • Have you considered *not* asking for newlines? – Cheers and hth. - Alf Oct 16 '17 at 02:57
  • What does "Press any key to continue..." have to do with anything? Make it `cout << sent.at(a);` - stop emitting a newline after every character, and you won't have every character on a new line. – Igor Tandetnik Oct 16 '17 at 02:57
  • If you're running this within Visual Studio, there is no way around "press any key to continue": https://stackoverflow.com/questions/17897736/avoiding-press-any-key-to-continue-when-running-console-application-from-visua – mnistic Oct 16 '17 at 02:58
  • When I do not have the endl there, it adds "Press any key to continue..." at the end, I understand why it does this, I would just like to know to have it display "olleH" instead of "olleHPress any key to continue...", assuming the input is "Hello" – zhodges10 Oct 16 '17 at 03:02
  • 1
    Have you tried running the exe on the command line, outside of VS? – mnistic Oct 16 '17 at 03:03

1 Answers1

0

I think I know what you mean, it is pretty simple. Just do this:

        if (sel == 4) 
        {
            for (a = sent.length() - 1; a >= 0; a--)
            {
                cout << sent.at(a);
            }
            cout<<endl;   
            break;
        }
Sahil Shah
  • 97
  • 1
  • 6