Help with Assignment!

I have been working on this question for i dont know how many hours now and I'm just about to give up!

It seems like a lot of code but basically everything is main() is given to me and I just have to write the three functions: (findSize, printNames and copyNames) I have bolded them.

FindSize: is supposed to find how many names are in a .txt file.

printNames: prints the names in that .txt file on screen.

copyNames: take a name of a file, an array of strings (an array of char arrays!) and the size of the array. It will then allocate new memory for each element in that array. It will then open the file, and read in all the names from the file and store them in each element of the array.

I got the functions to work on their own but when i add them to the program i get a bunch of errors. Any help would be appreciated! Thanks!!

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
 #include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>

#include "util.h"

using namespace std;
int findSize(char numOfNames[])
{
    ifstream names(numOfNames);



    char name[255];
    int count;

    while(names.getline(name,255))
    {
    count++;
    }
    return count;
}


//Add printNames
void printNames(char **name,int size)
{

    ifstream names(*name);
    char arr[255];

    while(names.getline(arr,255))
    {
    cout << name << endl;
    }

        return;
    }


//Add copyNames
bool copyNames(char *nameOfFile, char **arrOfNames, int size)
{

    ifstream file;
    file.open(nameOfFile, ios::in);


    //read the file into the arrays
    for (int i = 0; i < 255; i++)
    {
    file >> arrOfNames[i];
    }

return true ;
}


char **createArray(int size)
{
    if(size < 0 || size > 1000)
        return NULL;
    char **array = new char*[size];
    return array;
}

void deleteArray(char **array, int size)
{
    for(int i=0; i<size; i++)
        delete[](array[i]);
    delete[](array);
    return;
}

const int MAX_STRING_LENGTH = 255;

int main(int argc, char* argv[])
{
    int size=0;
    char **names=NULL;
    char *fileName = new char[MAX_STRING_LENGTH];

    //check if a filename is given
    if(argc > 1)
        strcpy(fileName, argv[1]);
    else
        strcpy(fileName, "input.txt"); //default filename

    //Determine how big to make the array
    //This is a fancy way of checking the return value
    //      at the same time when the function is called
    if( (size=findSize(fileName))==-1 )
    {
        cout << "An error occured...exiting\n" << endl;
        return EXIT_FAILURE;
    }

    //Create the array - allocating memory
    names = createArray(size);

    //Copy the names into the array from the file
    if(!copyNames(fileName, names, size))
        return EXIT_FAILURE;
    char array[255];

    // define file class
    ifstream file;
    file.open("input.txt", ios::in); //file.txt is the file you wanna open, ios::in means input file

    //now read the file into the arrays
    for (int i = 0; i < 255; i++) // a simple loop which counts to 3
    {
    file >> array[i];
    }




    cout << "The names in the file are:\n------------" << endl;

    cout << names[0] << endl;

    //Print the names in the array
    printNames(names, size);

    //Delete the array - freeing the memory
    deleteArray(names, size);

    cout << "------------\nGoodbye!" << endl;

    return EXIT_SUCCESS;
}
Last edited on
closed account (Dy7SLyTq)
could you post the errors
../src/A1cmpt115.cpp: In function 'void printNames(char**, int)':
../src/A1cmpt115.cpp:31: error: invalid conversion from 'char' to 'char*'
../src/A1cmpt115.cpp:31: error: initializing argument 1 of 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'
../src/A1cmpt115.cpp: In function 'bool copyNames(char*, char**, int)':
../src/A1cmpt115.cpp:47: error: declaration of 'char arrOfNames []' shadows a parameter
../src/A1cmpt115.cpp:47: error: initializer fails to determine size of 'arrOfNames'
make: *** [src/A1cmpt115.o] Error 1
closed account (Dy7SLyTq)
in copyNames you declared arrOfNames twice.
in print names try removing the ** on line 32
ok i get no errors now but when i compile i get "segmentation fault: 11"
and ive edited the my first post with my corrected functions.

Last edited on
closed account (Dy7SLyTq)
itll be the **'s that were removed. i dont know how to help you there.
Topic archived. No new replies allowed.