views:

114

answers:

4

I'm a student and just started learning C++ last week so this question is probably very low level but I can't figure it out.

I've searched around a bit but can't find any results, or maybe I'm looking for the wrong thing.

There are two cin parts. One taking in an int outside the loop, the other taking in a string inside the loop.

I'm getting a compile error saying ""Error no operator matches these commands" even though I just used them 5 lines ago.

Help?

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    // variable declaration
    const double payIncrease = 7.6;
    string employeeName;
    double initialSalary;
    double backPay;
    double employeeAnnualSalary;
    double employeeMonthlySalary;

    int numEmployees;

    // streams of information
    ofstream outStream;
    outStream.open("employeeRecords.txt");

    // console io's
    cout<<"Enter how many employees you have:";
    cin>>numEmployees;

    for(int i = 0; i <numEmployees;i++)
    {
            cout<<"What is Employee number: "<<i<<"'s name:";
            cin>>employeeName;

            cout<<"How much does that employee earn now: ";
            cin>>initialSalary;
    }

    outStream <<"annual salary was: " << numEmployees;
    outStream.close();

    return 0;

}

A: 
im getting a compile error saying ""Error no operator matches these commands" even though i just used them 5 lines ago. 

This sounds like a namespace issue.

Welcome to the wonderful world of programming. ;)

drachenstern
+1  A: 

Total fluke.

i just put

 #include<string>

at the top.

I wasn't aware that the console couldn't handle Strings

OVERTONE
Well, C++ can handle lots of things, you just have to include the appropriate headers ;-)
FredOverflow
This makes no sense whatsoever. Could you explain what exactly the problem was and how you fixed it?
David Thornley
I get the feeling half the programming community just done a facepalm at this.
OVERTONE
@David: He forgot to `#include <string>`
FredOverflow
@Overtone: I've been programming for a few years now, but every time I write a C++ program I forget at least one include...
delnan
@OVERTONE: "The console" is an OS feature and can handle strings just fine. You're probably referring to `std::cin`, which is the stream reading from the console. It can handle strings, but that's defined in `<string>` see my comment added to [this answer](http://stackoverflow.com/questions/3762371/error-no-operator-for-cin-i-cant-figure-out-what-im-doign-wrong-here/3762452#3762452) for why that's the case.
sbi
+4  A: 

Here is a version that actually compiles. You can figure out what you missed on your own ;-)

#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << "Enter how many employees you have:";
    int numEmployees = 0;
    cin >> numEmployees;

    for(int i = 0; i < numEmployees; ++i)
    {
        cout << "What is Employee number: " << i << "'s name:";
        string employeeName;
        cin >> employeeName;
    }
}
FredOverflow
know i figred it out before any answers but you probably deserve the answer more than me.. it was just the #include<string> at the top i forgot.
OVERTONE
A: 

im getting a compile error saying Error no operator matches these commands even though i just used them 5 lines ago.

If this refers to the snipped you posted, then you're wrong. As all other functions, operators can be overloaded in C++. This means there can be several functions using the same name, provided they take different arguments (or are either const or not member functions).

The variable name numEmployees looks to me as if it would refer to a number, while employeeName likely refers to a string. So this would call two different overloads of operator>>() for inputting these variables.

For reasons I'm omitting here, the operator>>() overload reading into a string is defined in the header <string>, while those for built-in types (int etc.) are in defined in <istream>, which you usually get by including <iostream>.

So, given what little information you gave us, this is a long shot, but I suppose you're missing an #include <string>.

sbi
i didnt overload any of the operators though. i just didnt know you had to include the package string to use it with cin.
OVERTONE
@OVERTONE: No, you didn't, but the standard library comes with dozens of overloads for `operator>>()`. In C++, IO isn't part of the language, but in the std lib. Therefor, the std lib needs to define an overload for `operator>>()` for every type you can input using `>>`. That makes a whole bunch of them. Another oddity of C++ is that strings aren't built into the language either. (That's due to its C heritage.) So strings are another thing the std lib provides. And the `operator>>()` that inputs into a string is declared in the `<string>` header, while most of the others are in `<istream>`.
sbi