Dragon Wolf
Dragon Wolf

Reputation: 61

Pointers and Char arrays issue

Ok so this code does compile, and it does run, but it does NOT give me the correct output for EITHER the original or reverse array, just gibberish. I spent the last 4 hours trying to see where I went wrong (it wasn't working at all before and now that I got it to at least output what SHOULD be a C-string, i get gibberish). Someone please help me see where I went wrong.

#include <iostream>
#include <cstring>
using namespace std;


char* getInput();
char* getReverse(char[], int);
void displayResults(char *, char *);

const int MAX_SIZE = 21;

int main()
{
    //program to create a c-string and then output the reverse order
    char *original;
    char *reverse;
    original = getInput();
    int originalSize = strlen(original);
    reverse = getReverse(original, originalSize);
    displayResults(original, reverse);
    return 0;
}

/***************************************************
Definition of function- getInput:                  *
prompts the user to enter in a line of text to a   *
max amount of characters. Returns a pointer to the *
C-string array.                                    *
****************************************************/
char* getInput()
{
    char *originalptr;
    char original[MAX_SIZE];
    cout << "Enter a word or line of text up to a max of 20 characters\nand will     have it output in reverse!" << endl;
    cin.getline(original, MAX_SIZE);
    originalptr = original;
    return originalptr;
}

char* getReverse(char *reverseThis, int size)
{

    char* reverseOutput;
    char reverse[MAX_SIZE];
    int counter = 0;
    while(*reverseThis != '\0')
    {
        reverse[counter] = *(reverseThis + (size - counter));
        reverseThis++;
        counter++;
    }
    reverseOutput = reverse;
    return reverseOutput;

}

void displayResults(char *original, char *reverse)
{   
    cout << "\nYou originally entered: " << original << endl;
    cout << "In reverse order: " << reverse << endl;
}

Upvotes: 0

Views: 109

Answers (3)

Chinmay
Chinmay

Reputation: 1

You are wasting too much char* s and data. Why not try this : (I have not tested the code, but it must work ,probably with minor fixes,if any.)

    #define MAXSIZE 20
    void getinput(char *in)
{    
   cin.getline(in,MAXSIZE);
return;

}
    void reverse(char *in);     
    {
        int len=strlen(in);
char *store=in;        
while(int i=0;i<len/2;i++)
{
char temp;
temp=*in;
*in=*(store+len);
in++;len--;
}
return;
    }
    int main()
    {
char data[MAXSIZE];
    getinput(data);
cout<<"Original :"<<data;
reverse(data);
cout<<"reverse"<<data;
    }

Upvotes: 0

john
john

Reputation: 88017

You are returning a pointer to a local array. The array original is destroyed after the function getInput exits. So original in main is pointing at something which no longer exists, garbage in other words. getReverse has exactly the same problem.

One way to solve this is to declare the arrays in main, and pass pointers to those arrays to the getInput and getReverse functions, for instance.

int main()
{
    //program to create a c-string and then output the reverse order
    char original[MAX_SIZE];
    char reverse[MAX_SIZE];
    getInput(original);
    int originalSize = strlen(original);
    getReverse(original, originalSize, reverse);
    displayResults(original, reverse);
    return 0;
}

void getInput(char* original)
{
    cout << "Enter a word or line of text up to a max of 20 characters\nand will     have it output in reverse!" << endl;
    cin.getline(original, MAX_SIZE);
}

// etc

Upvotes: 2

Gavin Haynes
Gavin Haynes

Reputation: 2002

In 'getReverse' you are allocating the variable 'reverse' on the STACK meaning the data will be GONE WHEN THE FUNCTION RETURNS, no matter how many pointers reference that data.

I would declare 'reverse' in main with 'char reverse[MAX_SIZE];', then have the function take a parameter 'char reverse[]', which the function would then modify.

Upvotes: 2

Related Questions