Secondly, I've been doing too much coding lately I guess, since I just typed this whole fuckin post, then instead of hitting submit, I hit ctrl+F5... "Page Has Expired" Back button no help => Epic Fail
Ok, now to my issue:
Just for practice and such, I'm building a basic program that takes an array of characters and reverses the letters in it. I broke this algorithm into two parts:
1) Build a function that uses a for loop to reverse the order of the characters within the array.
2) Build a function that essentially left justifies the letters.
I tested both functions by copying the code into the main function and they work fine. My problem comes when using that as actual funcitons. I can pass the array into the function easily enough, but returning an array is giving me a whole mess of issues. My best guess is that using "return aname[20];" is returning the contents of the 20th slot of the array (which would be garbage data in the memory) but this still begs the question "How to I pass the entire array?"
PS I tried this by passing pointers instead of arrays and got the same problems -_-
Code: Select all
#include <iostream>
using namespace std;
char backward(char aname[20]);
char ljustify(char aname[20]);
int main()
{
char name[20] = {'C','H','R','I','S'};
char name2[20] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','S','I','R','H','C'};
char test[20];
cout << name << endl;
cout << backward(name);
cout << ljustify(name2);
cout << endl;
cout << name;
test = backward(name);
}
char backward(char aname[20])
{
char bname[20];
for(int i = 0 ; i < 20 ; i++)
{
bname[i] = ' ';
}
for(int i = 0 ; i < 20 ; i++)
{
bname[i] = aname[(19-i)];
for (int j = 0 ; j < 20 ; j++)
{
cout << bname[j];
}
cout << endl;
}
return bname[20];
}
char ljustify(char aname[20])
{
while ( aname[0] == ' ' )
{
for ( int i = 0 ; i < 20 ; i++ )
{
if ( aname[0] == ' ' )
{
for ( int j = 0 ; j < 19 ; j++ )
{
aname[j] = aname[j+1];
}
aname[19] = ' ';
}
}
for (int k = 0 ; k < 20 ; k++)
{
cout << aname[k];
}
cout << endl;
}
for (int k = 0 ; k < 20 ; k++)
{
cout << aname[k];
}
return aname[20];
}