Reading strings into an array and inverting the string

So what I did is create a program that will let the user input a file then it opens the file. What I need to do is take the input from that file and read a string into an array then invert the string and give the user a new file.
I keep trying to debug my program but I need help. Any advice would be greatly appreciated.
Here's what I have so far:
#include<iostream>
#include<string>
#include<fstream>
#include <iomanip>
void invertArray(char[], int);
using namespace std;

int main() {
const int ARRAY_SIZE = 1024; // Array size
int numbers[ARRAY_SIZE];
int count = 0;
ifstream inputFile;
ofstream outputFile;

string inFilename;
string outFilename;
string read;

int i = 1;

while (i) {

cout << "Enter the filename(or enter the filename with path)";
getline(cin, inFilename);
inputFile.open(inFilename);

while (count < ARRAY_SIZE && inputFile >> numbers[count]) {
count++;
}

if (inputFile.fail())
{
cout << "Invalid Input, try again\n";
}
else
{
i = 0;
}

}


i = 1;
while (i)
{
cout << "please enter the end location of the file( with path)";
getline(cin, outFilename);
outputFile.open(outFilename);
if (outputFile.fail())
{
cout << " Invalid input, try again\n";

}
else
{
i = 0;
//string read[1024];
for (int i = 0; i <= 1024; ++i)
{
read >> my Array[i];
invertArray(read, 1024);
cout << read << endl;
}


}
}
cout << "The numbers are: ";
for (count = 0; count < ARRAY_SIZE; count++) {
cout << numbers[count] << " ";
}
outputFile.close();

system("pause");
return 0;
}
void invertArray(char[], int count)
{
int temp;
for (int i = 0; i < count / 2; ++2)
{
temp = arr[i];
arr[i] = arr[count] - i - 1;
arr[count - i - 1] = temp;
}
}
Last edited on
Are you reading numbers or strings? You're reading an array of integers. Can you provide a short sample of the input and expected output?

If you just need to output the array in reverse order then go through it that way. There's no need to reverse the order of what's stored:
1
2
3
    for (int i = count-1; i >= 0; --i) {
        cout << numbers[i] << " ";
    }
So I'm reading text from a file and reading it into an array and inverting it.
So say it was reading my user name it would read Alyuleth then invert the text to be hteluylA.
I tried compiling it... what's wrong is that it's missing some pieces.

$ g++ alyuleth.cpp -o alyueth
alyuleth.cpp: In function ‘int main()’:
alyuleth.cpp:60:9: error: ‘myArray’ was not declared in this scope
read >> myArray[i];
^~~~~~~
alyuleth.cpp:61:23: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ for argument ‘1’ to ‘void invertArray(char*, int)’
invertArray(read, 1024);
^
alyuleth.cpp: In function ‘void invertArray(char*, int)’:
alyuleth.cpp:80:34: error: lvalue required as increment operand
for (int i = 0; i < count / 2; ++2)
^
alyuleth.cpp:82:8: error: ‘arr’ was not declared in this scope
temp = arr[i];
^~~

The reference to "my Array" only shows up that one time. Initially the compiler complained about "my" and I figured that was just a copy-paste error so I removed the space. It complained about myArray instead and that's when I noticed that it's not anywhere else.

In order to complete your assignment, I suggest turning on all the warnings in the compiler. They're really helpful to figure out what is wrong. Also, if you use the "find" function in your editor (or grep on the command line) you can quickly resolve questions like "Did I name the variable this anywhere else" and such.

I am currently using gcc and g++ on a Windows 10 laptop with a nice, free program called "cygwin" installed that lets it run the GNU software. Also, I remember this assignment from the C++ class I took at Arizona State ten years ago :-)

Try what I said and then post more code! I'll help.

Topic archived. No new replies allowed.