Flipping?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        system("cls");
    cout<< "Display Sting backwards"<<endl<<endl;
        cout << "string: ";
        getline( cin, userinput );

        char CharCount[userinput.length()];
        char Flipper[userinput.length()];
        for (int a=0;a<=userinput.length();a++)
        {
            CharCount[a]=userinput[a];
        }
        int counter2=0;
        for (int p=userinput.length();p<=0;p--)
        {
            Flipper[p]=CharCount[counter2];
            counter2++;
        }
        int counter;
        counter=0;
        while(counter<=userinput.length())
        {
            cout<<Flipper[counter];
            counter++;
        }


Result:
1
2
3
4
Display String Backwards

string: cat
'N(with a squigly above it)G 


So What am I doing wrong I gone over it and i dont see anything so either im being dumb or going about this completely wrong or both
char CharCount[userinput.length()]; This is illegal. The size of an array must be know in compilation time.
why are you mixing char[] with string?

Check your loops:
__line 8,20 should be < (from 0 to n-1)
__line 13 will never execute.

You can do a reverse of the word in-place.
Last edited on
char CharCount[userinput.length()]; Is not illegal because the size is know because the array is not declared until after I know what the size is so it works.
1
2
3
getline( cin, userinput );

        char CharCount[userinput.length()];


Thanks for your help I got it to work with the two loop problems you pointed.
thanks again
I said compilation time. In your example, you will know userinput.length() when the program is already running.

ISO C++ forbids variable length array. Your compiler may have an extension that allows you to do that, but is against the standard.
Last edited on
Topic archived. No new replies allowed.