Help I need help writing my string out put

Feb 10, 2017 at 1:38am
I am trying to have this code print out No! I've been able to get it to print out Yes! but not no. Here is the problem.
Keep asking the user for characters until the user enters #. The characters may be stored in a vector of chars or in a string. Do NOT include # in your list. If the series of characters is a palindrome, print out "Yes!". If the series of characters is NOT a palindrome, printout "No!" A palindrome is a word that can be read forwards and backwards the same. And example of a palindrome is "racecar".

Example:
Input:
a
b
c
c
b
a
#

Output:
Yes!

And here is my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <iostream>
#include <string>

using namespace std;

int main()
{
    char input = ' ';
    string str;
    while (input != '#')
    {
        cin >> input;
        if (input != '#')
            str += input;
        
        //else (input != '#')
        //str -= input;
    }   



cout << "Yes!";
//cout << "No!";    
    system("pause");
    return 0;
}.
Feb 10, 2017 at 2:02am
Where is the code that checks if a string is a palindrome? How can you just print out "Yes!" without any evidence?

cout << "Yes!";
Feb 10, 2017 at 2:09am
You can check the string char by char or reverse the string and compare the two.
Feb 10, 2017 at 2:15am
string str;
initialize this upon declaration to be certain there's no garbage value in the initial variable;
a do-while loop would be more suited for your program as you'd have to accept at least one char;
finally, you don't have a palindrom checker anywhere in the code you've posted:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

int main()
{
    std::string str{};
    char input{};
    do
    {
        std::cout << "Enter character: \n";
        std::cin >> input;
        if(input != '#')
        {
            str += input;
        }
    } while (input != '#');
    if(str == std::string(str.rbegin(), str.rend()))
    //compare str to str reversed using string ctor with iterators
    {
        std::cout << "Yes \n";
    }
    else
    {
        std::cout << "No \n";
    }
}

Feb 10, 2017 at 2:23am
Oh sure lets do their homework for them and see how much they learn....
Your not doing them a favor if you post the code until they at least attempt it...
Feb 10, 2017 at 2:44am
OP posted 26 lines of code as their attempt but thanks for reporting this anyway
Feb 10, 2017 at 3:03am
I honestly have been trying to figure out this code by myself now for three hours and have been having a very hard time moving forward with it. I was posting it out of frustration after trying to find help on google and asking ta's and not getting very far. Thank you for your help so far.
Topic archived. No new replies allowed.