0

I get the error when using cout in main function at end

   #include "string"
#include "stdafx.h"
#include"iostream"

using namespace std;

string first[] = { "","one","two","three","four","five","six","seven","eight","nine","ten","eleven",
"twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen" };

string second[] = { "","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety" };

string lessThan100(int b)
{

    if (b < 20) { return first[b]; }//accounts for numbers that don't follow pattern ie 11 to 19

    return second[b / 10] + first[b % 10]; // dividing by 10 front number modding by 10 gives back number
}

string intToString(int a)
{
    string output = "";
    for (int x = log10(a); x >= 0; x = x - 3)
    {
        if (x >= 9)
        {
            int num = a / 1000000000;//dividing by a billion gives the # of billions
            if (num<99) { return "error number too large"; }

            output = output + lessThan100(num) + " billion ";

        }
        else if (x >= 6)
        {
            int num = a % 1000000000 / 1000000; //modding by a billion leaves the millions dividing by million gives # of millions
            output = output + lessThan100(num) + " million ";

        }
        else if (x >= 3)
        {
            string over100 = "";
            int num = a % 1000000 / 1000;//modding by a million leaves the thousands dividing by a thousand gives # of thousands
            if (num >= 100) { over100 = first[num / 100] + " hundred "; }//we can have more than 99 thousand so we account for that
            output = output + over100 + lessThan100(num) + " thousand ";

        }
        else if (x >= 0)
        {
            string over100 = "";
            int num = a % 1000;//moding by 1000 gives hundreds
            if (num >= 100) { over100 = first[num / 100] + " hundred "; }//accounts for number higher than 99
            output = output + over100 + lessThan100(num);
        }
    }
}

int main()
{
    int number = 19;
    string word = intToString(19);
    cout << word;
    return 0;
}

I get this error binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion). The program runs fine when I remove cout << word; I really just want to see if intToString() is working.

LiamKenny
  • 1
  • 1
  • 1
    Your posted is not compilable. Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – R Sahu Nov 16 '15 at 03:52
  • 1
    Use a debugger and step through `intToString(19)`. I'll guarantee you it isn't working like you think. Also, if you want help, please post all relevant code. See @RSahu's comment. – elixenide Nov 16 '15 at 03:52

2 Answers2

1

The headers you need are:

#include <string>
#include <iostream>

and they must come after the include of stdafx.h. The MS compiler ignores any lines that are before stdafx.h. The reasons why can be found in this answer (to a related question) but they basically boil down to the way MSVC does precompiled headres.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • You may want to provide the reason *why* this happens, the fact the stdafx is often precompiled and I believe it sets the current environment to a known state. At least I *think* that's how it works. – paxdiablo Nov 16 '15 at 04:01
  • Thank you very much this fixed it. I will remember this is the future – LiamKenny Nov 16 '15 at 04:05
  • @paxdiablo I've given all I know.. someone can edit if they are more familiar with MSVC :) – M.M Nov 16 '15 at 04:08
  • Okay, had a shot myself, hopefully doesn't result in downvotes for you if I'm horribly wrong. Still, it'll take five downvoters to reverse the current upvote (in terms of rep anyway) so we have a little leeway to get things fixed if that's the case :-) If anyone more au fait with MSVC feels it needs improving, please don't hesitate to do so. – paxdiablo Nov 16 '15 at 04:13
0

well, you should know that when you use " ", it tells the compiler to search for the head-file in local directory, while if you use <>, it tells the compiler to find head file in system directory. so, I suggest you to try use <> to #inlcude