Compiling gives expected primary-expression before âfoutâ

Hey guys, brand new here, and brand new to C++. Trying to get through my homework and I keep getting a few errors when I compile:

1
2
3
4
5
6
7
8
hw9.cpp: In function âint main()â:
hw9.cpp:23: error: expected primary-expression before âfoutâ
hw9.cpp:23: error: expected primary-expression before âintâ
hw9.cpp:23: error: expected primary-expression before âfileNameâ
hw9.cpp:24: error: expected primary-expression before âfileNameâ
hw9.cpp: In function âvoid outputData(std::string)â:
hw9.cpp:53: error: âfnameâ was not declared in this scope
hw9.cpp:59: error: âinFileâ was not declared in this scope


I'm usually pretty good at puzzling these things out, and I'm sure that I'm missing something small and stupid, but I've been staring at this for too long and I've got 7 other classes to do homework for. Can anyone tell me where I might be off?

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

int main ()
{

    string fileName;
    int dataSetSize;
    ifstream fin;
    ofstream fout;
    void generateData(ofstream&, int, string);
    void outputData(string);

    srand(time(NULL));

    cout << "Please enter the number of numbers you would like to produce: ";
    cin >> dataSetSize;

    generateData(ofstream fout, int dataSetSize, string fileName);
    outputData(string fileName);


} // End int main ()


void generateData (ofstream & dataFile, int dataSet, string & fName)
{
    int counter = 0;
    int randomNum = 0;
    cout << "Please enter the name of the file you wish to create: ";
    getline (cin, fName); // Gets filename input from user
    dataFile.open (fName.c_str());
    while (counter <= dataSet)
    {
        randomNum = rand()%100 + 1;
        counter++;
        dataFile << randomNum;

    }
    dataFile.close ();
}


void outputData (string fName)
{
    int fileVal;
    ifstream fin;

    fin.open(fName.c_str());
    if (fin.fail())
    {
        cout << "Could not open file";
        exit(1);
    }
    while (! fin.eof()) // Check to see if data remains in file
    {
        fin >> fileVal;
        cout << fileVal << endl;
    } // End while

}


Thanks so much!

Jade
I just realized how stupid "number of numbers" sounds, haha. I'll have to change that.
Hi Jade,

Your error messages indicate there is a problem with lines 23 and 24:
1
2
    generateData(ofstream fout, int dataSetSize, string fileName);
    outputData(string fileName);


Here is your problem: when you call a function with arguments(the variables you are passing it) you should not include the type of each variable. Remove the references to ofstream, int, and string - the compiler can figure out the type itself, you are just confusing it.

1
2
    generateData(fout, dataSetSize, fileName);
    outputData(fileName);

*******************
When you try to compile it next, you'll get a strange error message that it didn't encounter before.

EDIT: Disregard this, as vlad kindly pointed out, you may put function prototypes in main. Your function prototypes (lines 15-16) are in the wrong place. Please read the section "Declaring Functions." at http://www.cplusplus.com/doc/tutorial/functions2/ to fix this.

I haven't tested your program to see if it works, but you should be okay now. Good luck!
Last edited on
ReedTompkins,

why you decided that his function ptototypes in a wrong places? And what strange error message he will get?

Any function has external linkage, so there is no "wrong place" and "an error message".

I think he meant they should have been above my variable declarations, so I moved them up there. I'm still getting a strange message that I'm trying to figure out, but I don't know that it has anything to do with the placement of the prototypes.

1
2
3
4
5
/tmp/cclZvs8q.o: In function `main':
hw9.cpp:(.text+0x2e6): undefined reference to `generateData(std::basic_ofstream<char, std::char_traits<char> >&, 
int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status


And thank you so much for your help! I think I knew those weren't supposed to be there, but my brain is on overload.
Last edited on
Please show your updated code.
oops, sorry!

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;




int main ()
{

    void generateData(ofstream&, int, string);
    void outputData(string);

    string fileName;
    int dataSetSize;
    ifstream fin;
    ofstream fout;

    srand(time(NULL));

    cout << "Please enter the number of numbers you would like to produce: ";
    cin >> dataSetSize;

    generateData(fout, dataSetSize, fileName);
    outputData(fileName);


} // End int main ()


void generateData (ofstream & dataFile, int dataSet, string & fName)
{
    int counter = 0;
    int randomNum = 0;
    cout << "Please enter the name of the file you wish to create: ";
    getline (cin, fName); // Gets filename input from user
    dataFile.open (fName.c_str());
    while (counter <= dataSet)
    {
        randomNum = rand()%100 + 1;
        counter++;
        dataFile << randomNum;

    }
    dataFile.close ();
}


void outputData (string fName)
{
    int fileVal;
    ifstream fin;

    fin.open(fName.c_str());
    if (fin.fail())
    {
        cout << "Could not open file";
        exit(1);
    }
    while (! fin.eof()) // Check to see if data remains in file
    {
        fin >> fileVal;
        cout << fileVal << endl;
    } // End while

}
Sorry, vlad, I wasn't clear - in my opinion, it is very strange to put function prototypes in main. You are correct that it is possible and will not error (which was news to me!)

Jade,

Your function prototype for generateData lists its arguments as (ofstream &, int, string). However, generateData actually accepts (ofstream &, int, string &) (note the bold character). If you correct your prototype, it will fix your error.

Your error is basically saying "I don't see a generateData that accepts (ofstream&, int, string).
Last edited on
Thank you SO much. It's running now, and while I've still got errors to fix my headache is already starting to go away. :)

Jade
It is even better to declare ( and define ) your function as

void generateData (ofstream & dataFile, int dataSet, const string & fName)
I hate to sound totally helpless....but that's kinda how I'm feeling at the moment, so what the hell.... Any idea why the program wouldn't be stopping at line 41 to create the file that the user inputs the name of? For me, it asks how many numbers to produce, then the filename prompt pops up, but doesn't stop to get the input, just goes straight to "Could not open file" from the outputData function.

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;




int main ()
{

    void generateData(ofstream&, int, string&);
    void outputData(string);

    string fileName;
    int dataSetSize;
    ifstream fin;
    ofstream fout;

    srand(time(NULL));

    cout << "How many numbers you would like to produce? ";
    cin >> dataSetSize;

    generateData(fout, dataSetSize, fileName);
    outputData(fileName);


} // End int main ()


void generateData (ofstream & dataFile, int dataSet, string & fName)
{
    int counter = 0;
    int randomNum = 0;
    cout << "Please enter the name of the file you wish to create: ";
    getline (cin, fName); // Gets filename input from user
    cout << fName;
    dataFile.open (fName.c_str());
    while (counter <= dataSet)
    {
        randomNum = rand()%100 + 1;
        counter++;
        dataFile << randomNum;

    }
    dataFile.close ();
}


void outputData (string fName)
{
    int fileVal;
    ifstream fin;

    fin.open(fName.c_str());
    if (fin.fail())
    {
        cout << endl << "Could not open file" << endl;
        exit(1);
    }
    while (! fin.eof()) // Check to see if data remains in file
    {
        fin >> fileVal;
        cout << fileVal << endl;
    } // End while

}


Hi Jade,

Your problem is because you are using cin >> (line 25) and getline (line 39). Problems can occur if you mix these two.

Basically, after you use a cin >>, a newline character exists in the input stream (hitting enter generates a newline, but cin >> doesn't save the newline with your variable, so it remains in the input stream).

Then, getline reads until it sees a newline character, but you already have the "old" newline character sitting on the input stream, so it stops reading immediately.

(I hope that made sense).

Solutions: Use cin.ignore(); after you cin >> to clear the input stream. Alternatively, try not to mix cin and getline.
Topic archived. No new replies allowed.