-1

I am trying to use a integer vector to store an unknown number of positive integers from a text file. In general I have no problems pushing data into vectors, but in this case I am getting compile errors when I try to push an integer from the fstream to my vector. I could use a dynamic array or a stack, but I am adamant on learning to use vectors. My code is as follows:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

vector<size_t> nTxtToVec(string);

void main() {
    vector<size_t> allP = nTxtToVec("primes.txt");

    for (size_t i : allP)   // test all integers inside vector
        cout << i << ' ';

    cout << "\n\nPress any key to return...";
    cin.clear();
    cin.ignore(10000, '\n');
    return;
}

vector<size_t> nTxtToVec(string fin) {
    vector<size_t> v;
    size_t n = NULL;
    char c;

    fstream is;
    is.open(fin, ios::in);
    while (!is.eof()) {
        is.get(c);
        while (c >= '0' && c <= '9') {
            if (n == NULL)
                n = c - '0';
            else
                n = (n * 10) + (c - '0');
            is.get(c);
        }
        if (n != NULL) {
            v.push_back(n);
            n = NULL;
        }
    }
    is.close();
    return v;
}

With this code I get one error and one warning:

error C2440: 'initializing' : cannot convert from 'std::vector<int,std::allocator<_Ty>>' to 'int'

and

 warning C4018: '<' : signed/unsigned mismatch

I used "size_t" to replace all my "int" datatypes to try and avoid the signed/unsigned mismatch warning, but the compiler says the same thing even though the text file contains only positive integers and whitespace. I have also tried initializing the vector in main() and passing the vector to a void function rather than using a vector as the function return type. I am new to C++ so forgive me if didn't do something blatantly obvious or stupid.

  • You don't declare `allP` with a type. It should be `vector`. – 0x499602D2 Apr 13 '15 at 16:29
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Neil Kirk Apr 13 '15 at 16:30
  • I just added the datatype and re-ran the code. I still get the same errors.. – user4774782 Apr 13 '15 at 16:33
  • 1
    Does that error message perhaps come with a line number? Could you show which line the compiler is complaining about? – Useless Apr 13 '15 at 16:37
  • @Neil Kirk, for detecting the end of my file would it be appropriate to use while(is.get(c)), rather than while(is >> c)?? – user4774782 Apr 13 '15 at 16:40
  • @user4774782 read the link, it is almost NEVER ok to use as you do, since you end up "eating" the EOF. 99.99% in beginner code it's wrong. – vsoftco Apr 13 '15 at 16:41
  • @vsoftco well regardless of how i am detecting my end of file it is irrelevant at this point since i should still get some sort of output. I also changed the main function from void to int, but it also makes no difference. My issue is with initializing my vector, not detecting the end of file for my fstream... – user4774782 Apr 13 '15 at 16:46
  • Well in your question you say that you are getting a compile time error, not that your code compiles and run. Please edit and specify exactly what warnings you're getting. – vsoftco Apr 13 '15 at 16:48
  • @Useless the error C2440 is at line 13, and the warning is at line 49 (which doesn't make much sense since i only have 42 lines of code :P) – user4774782 Apr 13 '15 at 16:48
  • I would use `while (is >> my_string)` and then operate on the string. Much easier. – Neil Kirk Apr 13 '15 at 16:51
  • @vsoftco I specified correctly, you are simply not recognizing the problem. It will not compile and run based on the error and warning given in my question. The problem is beyond the solutions you are providing.. – user4774782 Apr 13 '15 at 16:51
  • @Neil Kirk thank you for the tip. i will try to reconfigure my code and give it a shot. – user4774782 Apr 13 '15 at 16:52
  • @user4774782 I sent you 2 links, and **both** compile your program. Copy paste the code and convince yourself. Compiling errors are one thing, logical errors another. You need first to be able to compile the code and then you can debug it. – vsoftco Apr 13 '15 at 16:53
  • OP - line 13 looks like whitespace in your question. I didn't ask what the line number of the error _is_, I asked you to _show which line_ it complains about. Presumably you can see this in your environment, so I don't know why you're forcing us to guess, or figure it out from first principles. – Useless Apr 14 '15 at 09:26

1 Answers1

0

Aside from other logical errors, such as while(!is.eof()), your code compiles if you replace void main() with int main() (as it should be, according to the C++ standard), and remove return; from the last line of main(). Unless you want to return something different from 0 (to signal to the OS an abnormal program error), there is no need for return 0; in main(), it automatically returns 0.

Live example on ideone (g++) or rextester.com (VC++)

vsoftco
  • 52,188
  • 7
  • 109
  • 221
  • Neither of your links compiled and I am using VC++ compiler. I did both of your suggested fixes and I am still getting the same errors. As far as I know, regardless of what the standard is, main() will execute regardless of the return type and is trivial with respect to my question. As for fixing my end of file loop, regardless of whether the end of file gives me unwanted junk or ends prematurely, it should still compile and run for me to see those errors. As far as I know the eof() error in the loop should not have anything to do with why my vector is not initializing... – user4774782 Apr 13 '15 at 17:20
  • @user4774782 ok, then it is probably a compiler issue. What version are you using? The online compiler I used compiles the code, as you can see. `C++ (vc++) and C - Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86` – vsoftco Apr 13 '15 at 18:11