Tim_Derp
Tim_Derp

Reputation: 11

Why does this basic cin prevent my program from compiling?

I've included and am using the standard namespace, and the program runs just fine when I just hard code the file name into in, but when I put in that cin VS gives me weird errors. I'm specifically talking about the cin >> sodokuFile line, for clarity.

cout << "Assignment 2\n\n";
ifstream ins;
cout << "Please enter the Sokoku file\n";
string sodokuFile;
cin >> sodokuFile;
ins.open(sodokuFile.c_str());

if(ins.is_open())
{
    int num;
    //counting numbers displayed horizontally
    int counth = 0;
    //counting numbers displayed vertically
    int countv = 0;
    while (ins >> num)
    {
        cout << num << "   ";
        counth++;
        //placing vertical lines
        if(counth %3 == 0)
        {
            cout << "| ";
        }
        //making line breaks for new rows
        if(counth == 9)
        {
            cout << "\n\n";
            counth = 0;
            countv++;
            //horizontal lines
            if(countv %3 == 0)
            {

                cout << "_________________________________________\n";
            }
        }
    }   
}
else
{
    cout << "File does not exist\n";
    return 0;
}

return 0 ;

Here is the only thing in the compiler errors that looks useful error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

Upvotes: 1

Views: 141

Answers (1)

Seth Carnegie
Seth Carnegie

Reputation: 75150

You need to put

#include <string>

At the top of your file because the string header declares operator>>(istream&, string&).

Upvotes: 6

Related Questions