0

I dont understand why i get this error when i compile my code. I believe that i am doing everything the same as i did previously but it is not working this time.

stack.cpp:30: error: cannot convert 'record*' to 'record**' for argument '2' to 'void filename(char*, record**)'

there are more errors when it is compiled but i will figure them out. (also i had to (add the 4 spaces per line that signify "code" by hand so if any one else could tell me to how to do that automatically! that would be great!

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <strings.h>

using namespace std;

struct record
{
    char first [20];
    char mid   [1];
    char last  [20];
    int  ssn;
};

void filename (char ifname [], struct record* student[]);
void structfill (fstream & infile, struct  record*  student []);

int main ()
{

    system ("clear");

    fstream infile;
    char ifname [256];
    struct record * student;
    filename (ifname, student);

    return 0;
}
/*******************************************************************/
void filename (char ifname [],record* student [])
{
    fstream infile;
    cout << "Enter name of file to read from: ";
    cin.getline (ifname, 256);
    cout << endl;
    infile.open (ifname);
    if (!infile.is_open ())
    {
        cerr << "FILELOOP!: Unable to open input file " << ifname
        << endl;
        exit (1);
    }
    structfill (infile, student);
}
/*******************************************************************/
void structfill (fstream & infile, record* student [])
{

    char buffer [81];
    char buffername [81];
    char bufferfirst [81];
    int n=0;
    int x=0;
    int f=0;

    infile.getline (buffer,81);
    while (!infile.eof ())
    {
        x++;
        cout << "-----------------------" << x;
        if (strncasecmp (buffer, "<student>",9)==0)
        {
            n++;
            cout << "jess sucks" << n;
            student = new *record;
            infile.getline (buffername, 81);
            if (strncasecmp (buffername, "<first>",7)==0)
            {
                f++;
                infile.getline (bufferfirst, 81);
                strcpy (student->first, bufferfirst);
                cout << endl << "######  " << f;
            }
        }
        infile.getline (buffer, 81);
        cout << *student[n]->first;
        cout << "endendendend" << endl;
    }
}
WhozCraig
  • 59,130
  • 9
  • 69
  • 128
user3304639
  • 31
  • 1
  • 9
  • You have things wrong with your code that go beyond your immediate issue of compilation. Your main() function uses an uninitialized pointer, student. – PaulMcKenzie Feb 13 '14 at 06:38
  • Unrelated: [This: `while (!infile.eof ())` is almost always wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – WhozCraig Feb 13 '14 at 06:38
  • The second parameter to filename() is an array of record*, which decays to record**. In main(), you're calling filename with a record* and not record** as is expected. Try using filename(ifname, &student), instead of student. EDIT:And as Paul pointed out there are other issues in the code as well. – Hari Mahadevan Feb 13 '14 at 06:38
  • I thought i was initializing student with: " struct record * student;" – user3304639 Feb 13 '14 at 06:41
  • Remove the C stuff. All you need is record* student. Now if you really mean to pass the pointer to your functions and assign a value to that pointer within your functions, then you pass the address of the pointer, as Hari Mahadevan pointed out. – PaulMcKenzie Feb 13 '14 at 06:44
  • Of course, you also have a memory leak due to not deleting the memory you allocated with "new". Maybe you should just declare an array of student in whatever way you need to in main() (statically or dynamically) and pass the array (see my answer). Or better yet, use a container such as std::vector instead of *new and getting things all fouled up. – PaulMcKenzie Feb 13 '14 at 06:47

2 Answers2

0

Arrays decay to a pointer to the first element when passed:

void structfill (fstream & infile, record*  student);

In addition, remove the C-isms. There is no need to have "struct" specified here.

PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
  • The code was compiling before i added the * to the: "void filename (char ifname [], struct record* student[]);" – user3304639 Feb 13 '14 at 06:40
  • So do you want to really pass the pointer and change the value of the pointer within the structfill() function? If so, then it should be void structfill(fstream& infile, record** student); – PaulMcKenzie Feb 13 '14 at 06:43
0

Here is the error free code.I compiled it successfully

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <stdio.h>
#include <cstring>
#include <strings.h>


using namespace std;

struct record
{
    char first [20];
    char mid   [1];
    char last  [20];
    int  ssn;
};

void filename (char ifname [], struct record* student);
void structfill (fstream & infile, struct  record*  student);

int main ()
{

    system ("clear");

    fstream infile;
    char ifname [256];
    struct record * student;
    filename (ifname, student);

    return 0;
}
/*******************************************************************/
void filename (char ifname [],record* student )
{
    fstream infile;
    cout << "Enter name of file to read from: ";
    cin.getline (ifname, 256);
    cout << endl;
    infile.open (ifname);
    if (!infile.is_open ())
    {
        cerr << "FILELOOP!: Unable to open input file " << ifname
        << endl;
        exit (1);
    }
    structfill (infile, student);
}
/*******************************************************************/
void structfill (fstream & infile, record* student )
{

    char buffer [81];
    char buffername [81];
    char bufferfirst [81];
    int n=0;
    int x=0;
    int f=0;

    infile.getline (buffer,81);
    while (!infile.eof ())
    {
        x++;
        cout << "-----------------------" << x;
        if (strncasecmp (buffer, "<student>",9)==0)
        {
            n++;
            cout << "jess sucks" << n;
            student = new struct record;
            infile.getline (buffername, 81);
            if (strncasecmp (buffername, "<first>",7)==0)
            {
                f++;
                infile.getline (bufferfirst, 81);
                strcpy (student->first, bufferfirst);
                cout << endl << "######  " << f;
            }
        }
        infile.getline (buffer, 81);
        cout << student[n].first;
        cout << "endendendend" << endl;
    }
}
  1. New operator returns a pointer not object.You should use struct record.
  2. student is capable of holding one array of struct records.
Able Johnson
  • 502
  • 7
  • 24
  • when the program runs it should be storing the name it reads into the new struct record that is created? I then try to print the first name out to see if it read correctly with line 82, "cout << student[n].first;" but it does not appear to be reading the line into memory? – user3304639 Feb 13 '14 at 07:58