1

FIXED: http://pastebin.com/71QxqGk5

first post/question.

So this is C++ and I am trying to print an array of words.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cctype>
#include <ctime>
using namespace std;

//structs
struct Input
{
    int size;
    string* word;
    bool is_palindrome[];
};

//prototypes
bool openInputFile(ifstream &ifs);
void File_to_Array(string* word, int &size);
void PrintArray(string* word, int size);

//main
int main()
{
    Input myInput = { 0, nullptr, false };
    File_to_Array(myInput.word, myInput.size);//copy arr and get size
    cout << myInput.word; //this outputs 00000000
    cout << *myInput.word; //this breaks and throws exception as commented below

    //Exception thrown at 0x0098BB6B in Project1.exe: 0xC0000005: Access violation reading location 0x00000014.

    PrintArray(myInput.word, myInput.size);//print array of strings

    system("PAUSE");
    return 0;
}

//functions
bool openInputFile(ifstream &ifs)
{
    string filename;

    cout << "Enter the input filename: " << endl;
    getline(cin, filename);
    ifs.open(filename.c_str());
    return ifs.is_open();
}

void File_to_Array(string* word, int &size)//copies file to dyn arr and assigns size from first elem
{
    ifstream myFile;
    while (!openInputFile(myFile))
        cout << "Could not open file" << endl;
    string tempstr = "";
    getline(myFile, tempstr);//first line is size of dyn arr
    size = stoi(tempstr);//now we have max size of dyn arr of strings
    word = new string [size];//now we have the array of strings, *word[index] = string1
    int i;
    for (i = 0; getline(myFile, word[i]) && i < size; ++i);//for each line
    //copy line of string from file to string arr within "bool" test, second param of for loop  //copying done
    size = i;
    myFile.close();//done with file, no need, close it
}

void PrintArray(string* word, int size)
{
    //for (int i = 0; i < size; ++i)
    //cout used to be here, but now its in main, for debugging
}

So I'm wondering if my problem is with passing a member of a struct, and if I should have instead passed the entire struct type "myInput" into the functions and use the -> operator to access the members of myInput.

below is an example of a text file

5
month
Runner
NEON
digit
ferret
nothing

the 5 would be the size of the dynamically allocated array, the rest are strings, as you can see there are 6 strings, so I have in the for loop a test for whether the file is still transferring strings to the array.

1
  • Input::is_palindrome is supposed to be an array but you initialize it with false. Commented Oct 21, 2015 at 7:10

1 Answer 1

1

This part of the File_to_Array is causing the problem:

word = new string [size];

You think that you are setting the pointer of myInput object to point to the string array, but you're not. When you pass the pointer to the function here:

File_to_Array(myInput.word, myInput.size)
              ^^^^^^^^^^^^

you are really passing a copy of the pointer. So inside the File_to_Array, this copy is re-pointed to the newly-created string array, but the real pointer inside myInput is not changed. You should pass a reference to the pointer instead:

void File_to_Array(string*& word, int &size)
                   \___________/
                         ^--reference to a pointer

I would also suggest you to use a vector[string] instead. Finally, your bool is_palindrome[]; member and it's initialization look very strange, but it's hard to comment further since they are never used in the code.

Sign up to request clarification or add additional context in comments.

2 Comments

oh, so I'm creating a copy of the pointer... okay, I understand now. So I have to pass a reference to a pointer to an array of strings into the functions. Thank you very much, I'll try it out right now and report back with success or failure. Hopefully the former. And we (my class) hasn't learnt vectors yet, and this is part of a hw assn which requires specific things be used, one of which is array, pointers, strings, all that kinda stuff, not vectors yet.
Thank you so much for the immediate help. I'm kinda new to this forum, but the first impression is great so far. I came back to tell you it worked, and I changed the bool[] to a bool*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.