views:

465

answers:

5

I'm working on a small little thing here for school. After hours of researching, and a ton of errors and logic reworking I've almost completed my little program here.

I'm trying to take user input, store it into the string, get a character array from the string ( dont ask why, I just have to put this into a character array ), then get the reversed order of the phrase that the user entered. Here is my code:

#include "stdafx.h"
#include <iostream>
#include <String>
#include <cstring>

using namespace std;
using namespace System;
#pragma hdrstop

char* getCharArray(string);

string reversePhrase( int, char* );

void main(void)
{
    string sPhrase = "";
    int sSize = 0;
    string sReversed = "";
    char* cPhrase = NULL;

    cout << "Welcome to the You Type It & We'll Reverse it! [Version 1.0] " << endl;
    cout << "This program will reverse your phrase, and count how many characters are in it!" << endl;
    cout << "To begin just enter a phrase." << endl;
    cout << "Enter a phrase: ";

    getline( cin, sPhrase);

    sSize = sPhrase.length();

    cout << endl;

    cPhrase = getCharArray(sPhrase);

    sReversed = reversePhrase( sSize, cPhrase );

    cout << sReversed;

    system("pause");


}


string reversePhrase(int size , char* cPhrase)
{
    string sReversed = "";
    int place = size;

    for ( int i = 0; i < size ; i ++ )
    {
     sReversed.append(1, cPhrase[place]);
     cout << "Current string: " << sReversed << endl;
     cout << "Current character: " << cPhrase[place] << endl;
     place--;
    }

    return sReversed;
}

char* getCharArray(string sPhrase)
{
    int size = 1;
    size = sPhrase.length();

    char* cArray = NULL;
    cArray = new char[size];

    for (int i = 0 ; i < size ; i++)
    {
     cArray[size] = sPhrase.at(i);
    }

    return cArray;
}

When I type in "ownage" into the program, this is what I get returned:

http://img26.imageshack.us/my.php?image=erroro.jpg

It is almost like my Character Array is getting garbage collected before it can use all of the characters. This is probably an easy fix but I just don't see how I can get around this one. Thanks!

+1  A: 

The bug is in this line:

cArray[size] = sPhrase.at(i);

That size should be your loop index.

You should probably look at using std::string more, and not poke around with character arrays when there's no need to.

unwind
Thanks! I really appreciate the help!
OneShot
+1  A: 

Why use a char array at all? It's not only useless – it complicates the code substantially (the usage of your function is more difficult, and you've forgotten to free the memory allocated by new!). Why not just have the following function:

string reverse(string const& input);

(Passing the argument by const reference instead of by value saves you a copy!)

In fact, implementing the function only takes a single line using the features of the string class (one of its constructors takes two iterators):

string reverse(string const& input) {
    return string(input.rbegin(), input.rend());
}
Konrad Rudolph
Thanks, appreciate the help!
OneShot
+3  A: 

Try rewriting getCharArray like this

    char* getCharArray(string sPhrase)
    {
        int size = 1;
        size = sPhrase.length();

        char* cArray = NULL;
        cArray = new char[size+1]; // NOTE

        for (int i = 0 ; i < size ; i++)
        {
            cArray[i] = sPhrase.at(i); // NOTE
        }

    }

    cArray[size]=0;  // NOTE

   return cArray;
  }

Note that the assignment in the loop now uses the index variable. Also, you need to allocate one extra char in the array to set the null terminator for the string and then you need to set it at the end.

You'll also need to think about deallocating the array at some point

Sean
Yes, cArray[size] = sPhrase.at(i) is never going to correctly copy the string into the character array.
AndyUK
Thank you I can't believe I missed that...
OneShot
+1  A: 

reversePhrase is also not correct. Try something like this:

string reversePhrase(int size , char* cPhrase)
{
    string sReversed = "";
    sReversed.resize(size);
    int place = size - 1;

    for ( int i = 0; i < size ; i ++ )
    {
        sReversed [i] = cPhrase[place];
        cout << "Current string: " << sReversed << endl;
        cout << "Current character: " << cPhrase[place] << endl;
        place--;
    }

    return sReversed;
}
kgiannakakis
Thank you. This also fixed the next step to my problem.
OneShot
A: 

first start array with -1 after that use for loop with -1 and increment in loop.after that u can get ur first element of the array.