Hello Gerardo559,
I agree with
jonnin and
lastchance that a vector is a better choice over the string array, but if you can not use it this may help.
I a not saying it is the best way to do this, but it does work.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <iostream>
#include <limits>
#include <string>
//#include <cmath>
//#include <cstdlib>
//#include <ctgmath>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
constexpr size_t MAXSIZE{ 6 };
std::string* reverseArray(std::string TheArray[]);
int main()
{
std::string letters[MAXSIZE] // <--- Set up for testing. Remove the {}s and everything in between for normal run.
{
"This is string one.", // <--- Add or subtract lines as you want. Be sure to change "MAXSIZE" at the top.
"This is string two.",
"This is string three.",
"This is string four.",
"This is string five.",
"This is string six"
};
std::string* lettersPtr{};
//int size = sizeof letters / sizeof letters[0]; // <--- Not needed when using "MAXSIZE".
std::cout << "\n Before sort\n";
for (auto& lc : letters)
std::cout << " " << lc << '\n';
lettersPtr = reverseArray(letters);
std::cout << "\n\n Last Print in \"main\"";
for (int lc = 0; lc < MAXSIZE; lc++)
std::cout << "\n " << *(lettersPtr + lc);
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
std::string* reverseArray(std::string TheArray[])
{
size_t index{};
static std::string newArray[MAXSIZE];
for (int lc = MAXSIZE-1; lc >= 0; lc--)
{
newArray[index++] = TheArray[lc];
}
// <--- These next lines can be commented out or removed. The for loop in "main" replaces these lines.
// <--- Mostly used for testing.
//std::cout << "\n After sort\n";
//for (auto& lc : newArray)
// std::cout << " " << lc << '\n';
return newArray;
//std::cout << std::endl; // <--- Used in debugging.
}
|
I rearranged your header files. The second group are no needed. "cmath" and "cstdlib", for me are included through "iostream" in VS, and there is nothing from the "cmath" header file that you are using in the program.
From what little I saw from the "ctgmath" header file it looks like code and functions that you would not be using for awhile if ever or something that would be used outside of school.
Lines 9 and 10 the comment and link should explain that.
In "main" I defined "letters" in this way for testing since you have no way of putting this information into the array other to hard code it for now.
The comment on line 31 should explain it.
Lines 33 - 36 may not be necessary, but good for debugging and testing.
The last bit of code before "return" is a fair replacement for
system("pause");
. Best not to use "system" anything.
In the function I changes the return type and since you have "MAXSIZE" in the global scope of the file you do not need to pass the size or even figure the size in the function.
Line 56 it eventually occured to me to make this "static" because even though you are returning the address of "newArray" the memory for the array is lost when the function ends. This way the memory will stay available when the function ends.
I realize that this program can be improved on, but it should give you an idea of what you can do.
Hope that helps,
Andy