Question

I'm learning c++ and trying some things out... The following code is giving me a compile time error, can anyone explain to me why, I'm a little confused... I'm assuming it's the cin >> playagain statement. Thank you for the help. (also if I'm making any other general c++ mistakes, please let me know)

heres the error:

Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\abdo\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 45 1 ConsoleApplication1

anyways, including #include <string> fixed the problem, thank you 0x499602D2

#include "stdafx.h"
#include <iostream>

using namespace std;
class calculatorc1 {
public:
    calculatorc1();
    ~calculatorc1();
    int multnums(int a, int b);
protected:
    int result;
};

calculatorc1::calculatorc1() {
}
calculatorc1::~calculatorc1() {
}



int calculatorc1::multnums(int a, int b) {
    int result = a * b;
    return result;
}


int main()
{
    string playagain;
    bool calcing = true;
    while (calcing) {
    calculatorc1 c;
    int x;
    int y;
    cout << "first num\n";
    cin >> x;
    cout << "second\n";
    cin >> y;
    cout << c.multnums(x, y) << "\n";
    cout << "mul again? (y/n)\n";
    cin >> playagain;
    if (playagain == "n") {
        calcing = false;
        system("pause");
    }
    }

}
Was it helpful?

Solution

I believe the problem is that you're not including the <string> header which is why you're getting a compile-time error because string has not been defined in the program. You're going to need this line to fix that problem:

#include <string>

OTHER TIPS

I copied this file and ran it, and got two errors.

The first was a compiler error on #include "stdafx.h" (saying it could not find the file). Once I removed it, the program compiled fine.

The second error was at runtime, after I multiplied the numbers. It said 'pause' was not found (it works in some environments; it didn't in mine).

This works properly with pure integer types, but there is no error handling. 44.0 caused it to error out.

Could you describe what you are seeing, and what you are expecting?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top