I'm doing a code in which a user inputs a word and then I have to output the word backwards, but for some reason, the black screen disappears when I enter the word...here's my code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    string word,backwards;
    int numchars=0;
    
    cout << "Enter word : " ;
    getline(cin,word);
    
    numchars = int(word.length());
    
    for(int i=numchars;i>=0;i--)
    {
       backwards = word.at(i);
    }
    
    cout << "The word backwards is : " << backwards << endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

What am I doing wrong?

Recommended Answers

All 11 Replies

All you are doing is moving 1 character at a time from word into backwards -- but you overwrite backwards each time.

Check the starting index on line 17, what's the last index of an array of length numchars?

You're on the right track with the loop but run through it by hand, you're always replacing the last entry of backwards with the next. See what you can come up with.

Worse yet, you start by (attempting to) copy a character that's one past the end of the source word. Depending on your compiler's implementation of the string, this could be a bad thing (it is in VC++2008)

I noticed the backwards thing after I posted the code, so yeah, thanks for pointing out. I edited the code a bit but now I input the word and the program does nothing...

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    string word;
    int numchars=0;
    
    cout << "Enter word : " ;
    getline(cin,word);
    
    numchars = int(word.length());
    string backwards[numchars];
    
    for(int i=0;i<numchars;i++)
    {
       backwards[i] = word.at(i);
    }
    
    for(int j=numchars-1; j<=0; j--)
    {
       cout<<backwards[j];
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Annotations in your code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    string word;
    int numchars=0;
    
    cout << "Enter word : " ;
    getline(cin,word);
    
    numchars = int(word.length());
    string backwards[numchars]; //makes an array of strings, not  
                                               //what you want
    
    for(int i=0;i<numchars;i++)
    {
       backwards[i] = word.at(i); //again, plot this one out on paper
//go through the loop by hand, you're copying one character to each of the arrays in the same order
    }
    
    for(int j=numchars-1; j<=0; j--) //see above
    {  //if your requirements were as such you could just use a loop to [I]display[/I] it backwards but I don't think that's what you want
       cout<<backwards[j];
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Now you created a string array called backwords and loaded each character from word into each string array.

The thing you're missing is 'backwards'. Copy the last character in word to the first location in backwards. Then the next to last to the second. Etc.

Remember, if backwards starts empty, all you need to do is add the character from word with +

Remember, if backwards starts empty, all you need to do is add the character from word with +

That was going to be my big finish :) If you want to do it by index of backwards set backwards to word initially so you have the right number of spaces in the array.

commented: Sorry for blowing you big surprise :P +11

Ok...thanks for the help guys. I managed to do it with the "run it by hand method"...very useful.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    
    string word;
    int numchars=0;
    
    cout << "Enter word : " ;
    getline(cin,word);
    
    numchars = int(word.length());
    char backwards[numchars];
    
    for(int i=1;i<=numchars;i++)
    {
       backwards[numchars-i] = word.at(i-1);
    }
    
    for(int j=0;j<numchars;j++)
    {
       cout<<backwards[j];
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Just output the string backwards. It's a string. You don't have to print it char by char.

Yeah I know, but I HAD to do it that way because the exercise wanted me to do it with the .at() function of string.

No, no, no. Note the italics. "Just output the string named backwards. You don't have to print the string backwards char by char." In other words, get rid of your last loop. where you used .at is fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.