0

I'm having a problem and I'm not sure how to fix it.

Here is a search function that I made from which I'm receiving the error;

void guestSearch()
{
    &Reservation::getfirstName;
    &Reservation::getlastName;

    &Date::getday;
    &Date::getmonth;
    &Date::getyear;
    Hotelreservation reg;
    reg.duration;
    reg.occupants;
    &Contact::getareaCode;
    &Contact::getexchange;
    &Contact::getline;

    system("cls");
    cout<<"Enter Line Number for the Guest you are searching for:";
    cin>>line;
    string line2= to_string(line);
    line2.append(".txt");

    ifstream filemain(line2);
    while(firstName>>lastName>>day>>month>>year>>duration>>occupants>>areaCode>>exchange>>line)
    {
        int firstNameLength=firstName.size();
        int lastNameLength=lastName.size();
        int lengthTotal=firstName+lastName;

        string result;
        cout<< "Is this the correct Guest (y/n)"<<endl;
        cout<<"Name"<<firstName<<" "<<lastName<<end;
        cin>>result;

        if(result=="y")
        {
            system("cls");
            cout<<"Name";
            for(int y = 1; y<lengthTotal; y++)
            {
                cout<< " ";
            }
            cout<<"Reservation Date";
            for(int z=1; z<2; z++)
            {
                cout<< " ";
            }
            cout<<"Duration of Stay";
            for(int x=1; x<2; x++)
            {
                cout<< " ";
            }
            cout<<"Occupants";
            for(int u=1; u<2; u++)
            {
                cout<< " ";
            }
            cout<<"Contact Number";
            for(int v=1; v<2; v++)
            {
                cout<< " ";
            }
        }
        cout<<"Date Reservation was made:"<<day<<"/"<<month<<"/"<<year<<endl;
        cout<<"Duration Of Stay:"<<duration<<endl;
        cout<<"Occupants:"<<occupants<<endl;
        cout<<"Contact Number:"<<areaCode<<"-"<<exchange<<"-"<<line<<endl;
    }
}

I'm receiving an error at the while declaration after firstName ">>", I get this:

IntelliSense: no operator ">>" matches these operands operand types are: std::string>>std::string"

S.G. Harmonia
  • 297
  • 2
  • 18
AtlasB
  • 1
  • 3
  • 9
    What on earth is going on in that `while` condition? – Cory Kramer Nov 13 '14 at 14:35
  • 1
    IntelliSense is right... What did you expect that line to do? It's totally unclear what that line should be doing, that alone should be a sign of bad style. – leemes Nov 13 '14 at 14:39
  • I think he's trying to read user input into those variables, but doesn't understand the concept of an input stream. –  Nov 13 '14 at 14:41
  • 4
    I believe what you want is `while (filemain >> firstName >> ...)` – Anton Savin Nov 13 '14 at 14:42
  • @AntonSavin Why? `ifstream` has no return value, therefore there is no reason to put it into a `while()` loop –  Nov 13 '14 at 14:44
  • @Pickle Actually, it's recommended to [cast the returned `ifstream` (a reference to the original one) to bool](http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool) to check for errors. – leemes Nov 13 '14 at 14:45
  • 1
    @Pickle you are wrong, ifstream has `operator bool` inherited from `basic_ios` especially for such case – Slava Nov 13 '14 at 14:47
  • @leemes is that really considered "good" coding practice? –  Nov 13 '14 at 14:48
  • @Pickle At least it's considered much better than checking for `eof` because it breaks the loop also when an error other than "end of file" occures. – leemes Nov 13 '14 at 14:52
  • 1
    What exactly are you trying to accomplishe by the lines that look like `&Reservation::getfirstName;`? It's as useful as `1;`. – molbdnilo Nov 13 '14 at 14:53
  • @Pickle `>>` returns the stream, which can be implicitly converted to `bool`. The suggested while-loop is perfectly idiomatic C++. (It should not be cast, though.) – molbdnilo Nov 13 '14 at 14:55
  • (@molbdnilo Sorry, I of course actually meant *implicit conversion* instead of *cast*.) – leemes Nov 13 '14 at 14:55
  • Idiomatic or not, it doesn't "feel" right. What if you need to add a condition to the loop? It will start looking very messy. –  Nov 13 '14 at 14:56
  • @Pickle Idioms often don't "feel" right to a beginner, even in natural languages. – molbdnilo Nov 13 '14 at 14:58
  • I should mention, this a file management concept im trying to apply so anything past the "int lengthTotal=firstName + lastName" line hasn't been sorted yet so it most likely wont make sense – AtlasB Nov 13 '14 at 15:03
  • @molbdnilo OK so tell me what you would do if suddenly you were told that you had to check against one or more of the values of the inputs all of a sudden. Would you write `while((one< –  Nov 13 '14 at 15:22
  • Im a little short for time, so im searching for ways to find and edit files which is suppose to be the basic functionality of the program and then worry about exception handling a bit later – AtlasB Nov 13 '14 at 15:30
  • @Pickle that additional checks can be inside loop with `if(...) break;` not necessary to put them all there. – Slava Nov 13 '14 at 15:33
  • @Pickle You could start move that stuff to `parseLine(file)` and write `while(parseLine(file) && ...)`. Of course, `parseLine` would then return a `bool` implicitly from the provided `istream`. – leemes Nov 13 '14 at 15:34
  • OR, you could actually do it in a way that makes sense: `while(conditions) { streamin >> one >> two >> three ... }` –  Nov 13 '14 at 15:37
  • @Pickle But how would you then cancel if the format doesn't match your expectation? Conversion to bool does that for you, and breaking as soon as it turns out to be false avoids falsely doing stuff with the variables (which then might be uninitialized). – leemes Nov 13 '14 at 15:55

1 Answers1

1

You misunderstand the concept, though it is possible to chain multiple std::string instances trhough operator >>, first object should be of type inherited from std::istream such as ifstream fileman in your case:

while( fileman >> firstName >> lastName >> ... )

it works because you can treat that expression as:

while( fileman.operator>>( firstName ).operator>>( lastName ) ... )

and because std::istream::operator>> returns reference to std::istream again that chain invocation works.

There is no such operator in std::string

Slava
  • 43,454
  • 1
  • 47
  • 90
  • thanks for the eye opener, the function is now error free but when i run it, it only couts "Enter Line Number for the Guest you are searching for:" then the function ends... – AtlasB Nov 13 '14 at 18:23
  • @AtlasB If you have another issue - create another question. – Slava Nov 13 '14 at 18:27
  • are you sure because i would have to copy the whole function again and it might be something trivial i don't want to be making redundant trivial post.... – AtlasB Nov 13 '14 at 19:44