Reversing the input of an array.

Oct 26, 2012 at 10:38pm
For this assignment, I have to reverse whatever the user has input.
So, if the user were to input:
123456789
I have to make a program that outputs:
987654321

This is my code so far, but it's got a few.. 'quirks'(?) in it.

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
27
28
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int i;
    char score[10];

    cout << "Enter anything you want reversed: " << endl;

    for (i = 0; i < 10; i++)
    {
        cin.get(score[i]);


        if (score[i] == '\n')
        {
            cout << "You have entered: " << endl;

            for (i = 9; i >= 0; i--)
            {
                cout.put(score[i]);
            }
        }
    }
}


First of all, I'm using get() and put() so that the program will read spaces the user has input. Also, the size of the array has to be 10.

However, if you compile this code, you'll notice that while it does reverse whatever I input, it'll sometimes give me some weird characters before showing me what I've input in reverse.

Also, once it's shown the input in reverse, the cursor will keep blinking instead of just ending the program.

Help, please?

These are the instructions:
1
2
You should limit the amount of data being stored in your array to 10 values. After
you read and store 10 or less values in your array from the terminal, ignore any other input.
Last edited on Oct 26, 2012 at 10:45pm
Oct 26, 2012 at 10:46pm
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int i;
    char score[10];

    cout << "Enter anything you want reversed: " << endl;

    for (i = 0; i < 10; i++)
    {
        cin.get(score[i]);
        if (score[i] == '\n')
            break;
    }
    cout << "You have entered: " << endl;
    for (i = 9; i >= 0; i--)
    {
         cout.put(score[i]);
    }
}
Oct 26, 2012 at 10:49pm
See, I tried that the very first time, too, but I kept getting this:
http://i.imgur.com/GP2eS.png

Notice the two weird characters after
You have entered:?
And it wouldn't go away after I changed it to the way I had the program in the original post.
Last edited on Oct 26, 2012 at 10:50pm
Oct 26, 2012 at 10:52pm
You need to keep track of the number of letters entered. Obviously, you don't want to print out the entire score array in reverse if the number of characters entered was only 3.
Oct 26, 2012 at 10:56pm
Try the following

1
2
3
4
5
6
7
8
9
10
    char c;

    for ( i = 0; i < 10 && ( c = cin.get() ) != '\n'; i++ )
    {
        score[i] = c;       
    }

    cout << "You have entered: " << endl;

    while ( i != 0 ) cout.put( score[--i] );
Oct 26, 2012 at 11:06pm
@vlad from moscow: I don't understand what these
1
2
3
4
5
( c = cin.get() )

score[i] = c

cout.put( score[--i] )

are doing.

@cire: how can I do that?
Oct 26, 2012 at 11:16pm
Switch on your brain. Is there any difference between?!

score[i] = 'A';

and

char c = 'A';
score[i] = c;
Oct 27, 2012 at 12:16am
It wasn't so much that as the cin.get inside loop parameters
Oct 27, 2012 at 1:13am
So I went with:
char score[10] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
I changed it to that and the 'special' characters disappeared. I also feel like there's a cleaner/better way of doing this, so let me know please.
Oct 27, 2012 at 1:17am
so let me know please.


I believe we have.
Oct 27, 2012 at 2:18am
Ended up with:
char score[10] = {'\0'};
Topic archived. No new replies allowed.