meriley
meriley

Reputation: 1881

File Input and Errors on Compile

Hello Users of Stack Overflow!

I'm having an issue with a program I'm attempting to write in C++. Every time I compile my code I get the following Errors:

FCS2Phase.cpp: In function `int main(int, char**)':
FCS2Phase.cpp:47:15: error: request for member `open' in `userfile', which is of non-class type `std::ifstream()'
FCS2Phase.cpp:48:22: error: request for member `eof' in `userfile', which is of non-class type `std::ifstream()'
FCS2Phase.cpp:50:39: error: cannot convert `std::ifstream (*)()' to `char**' for argument `1' to `ssize_t getline(char**, size_t*, FILE*)'
FCS2Phase.cpp:52:49: error: cannot convert `std::ifstream (*)()' to `char**' for argument `1' to `ssize_t getline(char**, size_t*, FILE*)'
FCS2Phase.cpp:54:14: error: request for member `close' in `userfile', which is of non-class type `std::ifstream()'

Being so new to C++ I'm not too sure what this error is trying to tell me is incorrect with my use of ifstream. I'm sure this has to be something stupidly simple but i cannot figure it out :( Ive tried some desperate things like dereferencing userFile calling it as (userFile*).open and also userGFile->open. both result in the same errors.

The lines of code effected by this is as follows:

int count = 0;
userfile.open(fileName.c_str(), ios::in | ios::binary);
while (!userfile.eof()) {
    if (count < arrSize)
        getline(userfile, a[count]);
    else if (count - arrSize < arrSize)
        getline(userfile, b[count - arrSize]);
}
userfile.close();

The whole program is as follows:

/* 
 * File:   FCS2Phase.cpp
 * Author: Mark
 *
 * Created on October 17, 2011, 11:28 AM
 */

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <time.h>
#include <string>

using namespace std;

void randInput(int size, string fileName);

/*
 * 
 */
int main(int argc, char** argv) {

    string fileName;
    char randYN;
    int arrSize;
    cout << "(This program assumes file is in program root directory and arrays are equal size)";
    cout << "\nPlease insert the file name: ";
    getline(cin, fileName);

    ifstream userfile();

    cout << "How many numbers are stored in the arrays?: ";
    cin >> arrSize;

    double a[arrSize];
    double b[arrSize];

    cout << "Create Randomized File? (Y/N): ";
    cin >> randYN;

    if (toupper(randYN) == 'Y')
        randInput(arrSize * 2, fileName);



    int count = 0;
    userfile.open(fileName.c_str(), ios::in | ios::binary);
    while (!userfile.eof()) {
        if (count < arrSize)
            getline(userfile, a[count]);
        else if (count - arrSize < arrSize)
            getline(userfile, b[count - arrSize]);
    }
    userfile.close();



    cout << "The values inputted were: " << "\n\tArray #1: ";
    for (int i = 0; i < arrSize; i++) {
        cout << a[i] << "\t";
    }
    cout << "\n\tArray #2: ";
    for (int i = 0; i < arrSize; i++) {
        cout << b[i] << "\t";
    }

    return 0;
}

void randInput(int size, string fileName) {
    const double MIN = 0;
    const double MAX = 100;
    double num;
    ofstream file(fileName.c_str());
    if (file.is_open()) {
        for (int i = 0; i < size; i++) {
            srandom(time(NULL));
            num = (double) rand() / RAND_MAX;
            file << MIN + num * (MAX - MIN) << "\n";
        }
        file.close();
    }
}

Thank you for any help!

Upvotes: 0

Views: 1917

Answers (1)

tune2fs
tune2fs

Reputation: 7705

The following line should be

 ifstream userfile;

instead of

 ifstream userfile();

Upvotes: 5

Related Questions