> Array and for loop question?

Array and for loop question?

Posted at: 2014-12-18 
I am stuck on looping out the array of the user input in the form of a single word, instead of displaying each letter individually from the array.

..............................

your code

// display array with for loop

for (int i = 0; i < 8; i++){

cout << "The word is " << letters [i] << " => " << letters[i] << endl;

}

prints this

the word is [letter1] => [letter1]

the word is [letter2] => [letter2]

....

i asume you want something like this

the word is [letter1 letter2 ... letter8 ] ...



to do that you want to print "the word is " outside the for loop

then inside

for (int i = 0; i < 8; i++){

cout << letters[i];

}

this will just print the letters after each one

finally print out the endln to move onto the next line

to print backwards it you have to start your i at 7, then make sure i is greater then zero while you decrement with i--

then print your letters with letters[i]

I am stuck on looping out the array of the user input in the form of a single word, instead of displaying each letter individually from the array. Also I am unsure on how to display the reverse of the word, help would be greatly appreciated!

I have to meet the following 4 steps:

1. Create an array to store 8 characters.

2. Using a for loop, get an 8-letter word from the user, one letter at a time.

3. Then use another for loop to print the complete word onto the screen one letter at a time.

4. Then use a third for loop to print out the word backwards.

Currently my code looks like :

#include

using namespace std;

int main()

{

// array

char letters[8];

char eletter[] = "Please enter a letter: ";

// fill array

for (int i = 0; i < 8; i++){

cout << eletter;

cin >> letters[i];

}

cout << endl;

// display array with for loop

for (int i = 0; i < 8; i++){

cout << "The word is " << letters [i] << " => " << letters[i] << endl;

}

cout << endl;

for (int i = 0; i < 8; i--){

cout << "The word backwards is " << i << endl;

}

system("PAUSE");

}