0

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");
    }
    }

}
Abdul Ahmad
  • 9,673
  • 16
  • 64
  • 127
  • 2
    Seriously? You aren't going to tell us what the error is? You expect us to guess? – John3136 Jan 17 '14 at 00:30
  • 2
    Why I see so many questions "this code gives an error" without the error specified? How do people think it is irrelevant the specific error to the question? – Jack Jan 17 '14 at 00:31
  • 1
    There is no reason to explicitly declare or define your constructor and destructor in this example. – Zak Jan 17 '14 at 00:31
  • ok guys sorry for not including the error log, no need to get nasty... I will include – Abdul Ahmad Jan 17 '14 at 00:47

2 Answers2

3

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>
David G
  • 94,763
  • 41
  • 167
  • 253
  • Who knows whether another header (including stdafx.h) fully includes it, but this is my guess. – chris Jan 17 '14 at 00:37
  • @chris Better safe than sorry! :) – David G Jan 17 '14 at 00:38
  • When you include (at least when you're using Visual C++, which OP is since there's stdafx.h) you get part of , IIRC it's enough to let you declare a string without errors, which can lead you to assume the problem isn't a missing header when you can't do something else later. I was bitten by this myself when some headers reorganized between VC++ releases. – Kate Gregory Jan 17 '14 at 00:46
  • @KateGregory, Definitely, I see quite a few questions now and then about being able to use most of it, but not the input and output specifically (I believe there are other free functions as well, but they don't pop up here). Good lesson for not relying on headers being included in others. – chris Jan 17 '14 at 00:52
0

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?

OnlineCop
  • 4,019
  • 23
  • 35
  • The first is for MSVC precompiled headers, which means the OP is probably on Windows, which still might not have a pause command even though it does by default. If it does, the command still might not behave as expected, so yeah, it's good to avoid. – chris Jan 17 '14 at 00:39