expected primary-expression before ',' token

I'm getting this error, "expected primary-expression before ',' token" for the project I'm working on. For my project, I have to have the user input a text file using the text file's name as a string, where I will use ifstream to extract data from the textfile, and input the data into an array of structs. We are to use 3 text files, named one.txt, two.txt, and three.txt. I used a cin to input the file name, and the function c_str to convert the string to a cstring, but I am getting that error on line 40. I'm new to coding, so any pointers would be very helpful. Thanks!




#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>


using namespace std;

struct birds
{
string species;
int birdcount;
};

const int max_size = 200;

void getdata (ifstream& inFile, birds[], int& listSize);

string city;


int main()
{
birds list [max_size ];
ifstream datafile;

int number;
int x;

string filename;

cout << "Input the file you wish to open..." << endl;
cin >> filename;


cout << "Opening file " << filename << endl;

datafile.open(filename.c_str);
if (!datafile)
{
cout << "File not found..." << endl << endl;

return 1;
}

getdata(filename.c_str(), birds, max_size);

}


void getdata (ifstream& inFile, birds[], int& listSize)
{
char selection;
string species;
int birdCount;

listSize = 200;

inFile >> species;
inFile >> birdCount;

getline(inFile,city);
cout << city << endl;
cout << endl;

while(inFile)
{

inFile >> species;
inFile >> birdCount;

cout << species << setw(5) << right << birdCount << endl <<endl;

}
inFile.close ();
}
getdata(filename.c_str(), birds, max_size);
You cannot provide a const value since getdata(..., int& listSize) requires a non const value. create a variable for that.
In this statement

getdata(filename.c_str(), birds, max_size);

there are two errors. The first argument shall be of type ifstream& and instead of type specifier birds there shall be an object of type pointer to birds.
I created the variable, int number, to fill in for listSize in the function, but It's yielding the same error for me. Is there an issue with the way I've called my function? I'm a little shaky on functions so I'm not entirely sure I'm doing it right.
See how you have defined the function :

void getdata (ifstream& inFile, birds[], int& listSize)

you have the types and names of the first and last parameter in the function, but not the middle parameter, maybe:

void getdata (ifstream& inFile, birds flock[], int& listSize)

and then you need to work with the flock variable inside your function.
Ok I've changed some things up, but I have another problem now.

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
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstring>

using namespace std;
////structs/////
struct birdlist
{
    string species;
    int birdcount;
};
};
 birdlist birds;
 birdlist *birdies;

///////consts///////
const int max_size = 200;
/////prototypes//////
void getdata (ifstream& inFile,  birdlist[], int& listSize);
////variables///////
string city;

////////main///////////
int main()
{
birdies = &birds;
birdlist list [max_size ];
ifstream datafile;
int number;



string filename;

cout << "Input the file you wish to open..." << endl;
cin >> filename;


datafile.open(filename.c_str());
if (!datafile)
{
    cout << "File not found..." << endl << endl;

    return 1;
}

getdata(datafile , (*birdies), number);

}


void getdata (ifstream inFile, birdlist[], int& listSize)
   {
       char selection;
       string species;
       int birdCount;

       listSize = 200;

       inFile >> species;
       inFile >> birdCount;

       getline(inFile,city);
       cout << city << endl;
       cout << endl;

       while(inFile)
    {

        inFile >> species;
        inFile >> birdCount;

        cout << species << setw(5) << right << birdCount << endl <<endl;

    }
        inFile.close ();
    }


My error states:
 
|50|error: cannot convert 'birdlist' to 'birdlist*' for argument '2' to 'void getdata(std::ifstream&, birdlist*, int&)'|

Last edited on
Topic archived. No new replies allowed.