1

I've been trying for 2 days now to get this code to work. It's just been error after error.

Can anyone point out what i'm doing wrong?

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{
    int h = 0;
    for(int a = 100; a<1000; a++)
        for(int b = 100; b<1000; b++)
            int c = a * b;
// Error: "c" is undefined
            if ((c == reverse(c)) && (c > h))
                h = c;
    cout << "The answer is: " << h << endl;
}

int reverse (int x)
{
// Error: "'itoa' : function does not take 1 arguments"
    string s = string(itoa(x));
    reverse(s.begin(), s.end());
  return (x);
}

Using std::to_string just gives me more errors as well.

Leinad177
  • 51
  • 1
  • 7
  • Listen to your compiler. It's telling you what the problem is. `...does not take 1 arguments...`, that is telling you one of two things: 1. You're sending one argument to a function that doesn't take any arguments(highly unlikely in this situation), or 2. You're sending one argument when the function takes at least two, but could be `n(Arg)`. If `n=100` and you sent `99` it'd say `...function does not take 99 arguments'. – ChiefTwoPencils Jul 29 '12 at 04:45
  • `..function does not take 99 arguments...`. The same goes when you exceed it, saying the same thing! It will compile correctly when you send it the correct amount of arguments. – ChiefTwoPencils Jul 29 '12 at 04:51
  • By looking at your code further, and your continued frustration in comments to other answers, I would suggest that you utilize braces even when they're not "needed"! Especially if you get simple errors continually. When you can bust out code while watching Seinfield, then omit them. I use 'em like it's required! – ChiefTwoPencils Jul 29 '12 at 05:16
  • Perhaps you should ask about how to address the problems you were having with [`std::to_string`](http://en.cppreference.com/w/cpp/string/basic_string/to_string), it's 14 times better than itoa, and standard. – Benjamin Lindley Jul 29 '12 at 05:35
  • Just FYI: In a situation where you have one or more programmer(you) defined overloaded functions which accept `n (+/-) x` arguments you wouldn't get that error message, but could possibly get unexpected results. – ChiefTwoPencils Jul 29 '12 at 05:36

3 Answers3

1

When your compiler explains something to you in an error message, you should believe it. itoa does, in fact, take more than one argument, as you can see at the following link:

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

Edit: Oh and this is achievable using standard, C++-style code by the way (fixed a bit of code as per suggestion in the comments):

int reverse(int x)
{
    std::stringstream ss;
    ss << x;

    std::string s = ss.str();
    std::reverse(s.begin(), s.end());

    ss.clear();
    ss.str(s.c_str());

    ss >> x;

    return x;
}

Here. Not sure it's the cleanest solution but it works on my compiler.

Edit: Found out how to use only one stringstream here : How to clear stringstream?

Community
  • 1
  • 1
ApplePie
  • 8,814
  • 5
  • 39
  • 60
  • "Error: no suitable user-defined conversion from "std::stringstream" to "std::string" exists" and a few more errors :/ – Leinad177 Jul 29 '12 at 04:48
  • @LuchianGrigore: I am not denying this though in fixing my own solution I did not want to copy yours. If people find yours better they can upvote it (it is indeed shorter). – ApplePie Jul 29 '12 at 04:56
  • I just tested your solution and it does not appear to be working (no reversing) while this one does. I wonder if it's possible to use only one stringstream though. I had no luck with clear() or flush() emptying it so I had to resort to using one for input and the other for output. – ApplePie Jul 29 '12 at 04:59
  • "'void std::reverse(_BidIt,_BidIt)' : expects 2 arguments - 1 provided", "'reverse' : local function definitions are illegal", "(7): this line contains a '{' which has not yet been matched", "end of file found before the left brace '{'". I think c++ hates me lol. – Leinad177 Jul 29 '12 at 05:03
  • It compiles fine and works on my computer. What compiler / IDE are you using ? Can you link us to your code via http://pastebin.com/ ? – ApplePie Jul 29 '12 at 05:06
  • 2
    Again listen to compiler! Search the line the error references and look to see what might match the error msg. `end of file found before left brace..` means exactly that. – ChiefTwoPencils Jul 29 '12 at 05:12
  • +1 You're missing a closing brace for the main function which you could have spotted very easily with better indentation. – ApplePie Jul 29 '12 at 05:13
  • Okay sorry. Now it's giving me "'<<' : operator has no effect; expected operator with side-effect" errors. and ss_in and ss_out has a "incomplete type is not allowed" error. – Leinad177 Jul 29 '12 at 05:19
  • Try using the latest code I have put there (sorry for the many edits, been trying to figure out the best way to do this). – ApplePie Jul 29 '12 at 05:21
  • Oh and you are not including sstream in which are stringstreams located. – ApplePie Jul 29 '12 at 05:21
  • @Leinad177: You were missing a couple of `#include` statements and a prototype for the `reverse` function. I fixed those and the code works correctly on [ideone.com](http://ideone.com/ZFjLT). – Blastfurnace Jul 29 '12 at 05:25
  • @Blastfurnace That works, thanks :) And thanks everyone else for helping. – Leinad177 Jul 29 '12 at 05:31
  • @Leinad177: I also tested using my `is_palindrome` function and it is significantly faster on [ideone.com](http://ideone.com/1k1l6). – Blastfurnace Jul 29 '12 at 05:33
1

Can I suggest a different solution? Instead of doing int<->string conversions you can test to see if a number is a palindrome this way:

bool is_palindrome(int number, int base = 10)
{
    int rebmun = 0;
    for (int temp = number; temp != 0; temp /= base) {
        rebmun = (rebmun * base) + (temp % base);
    }
    return number == rebmun;
}

Then your test becomes:

if (is_palindrome(c) && (c > h))
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Looks good, would you be able to explain it in more detail though? I don't really understand what it does, just that it works. – Leinad177 Jul 29 '12 at 05:45
  • @Leinad177: The key idea is using `temp % 10` to isolate the least siginificant digit of `temp`. Every step of the loop it multiplies `rebmun` by 10 to shift the value "left" and then adds the last digit from `temp`. Until `temp` becomes zero the digits are moved from `temp` to `rebmun` one at a time. Note the parameter `base` defaults to 10 for decimal numbers but you can pass 2 to test for binary (base 2) palindromes. This will come in hand for another Project Euler problem :) – Blastfurnace Jul 29 '12 at 06:01
0

For the first problem, correct indentation might make it clear:

int h = 0;
for(int a = 100; a<1000; a++)
    for(int b = 100; b<1000; b++)
        int c = a * b;

if ((c == reverse(c)) && (c > h))
    h = c;

With some extra brackets:

int h = 0;
for(int a = 100; a<1000; a++)
{
    for(int b = 100; b<1000; b++)
    {
        int c = a * b;
        if ((c == reverse(c)) && (c > h))
            h = c;
    }
}

As for the itoa issue, its signature is:

char *  itoa ( int value, char * str, int base );

so you can't just write itoa(x) and expect it to return a string.

There are however better way to convert an int to a string in C++

  • if you have C++11, there std::to_string
  • otherwise, a std::stringstream will do the job.

Like so:

#include <sstream>

int reverse (int x)
{
    std::stringstream ss;
    ss << x;
    string s(ss.str());
    reverse(s.begin(), s.end());
    return (x);
}

Note that this won't return the int reversed though.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625