1

I am working on flipping a string of numbers. I get no errors or warnings when compiling, but after I input numbers, there popped out an error window where I can understand none of the words. Can someone help me?

my purpose is:to see, in an interval of numbers, how many numbers have is the same when turned 180 degrees? (e.g.8888, 6699, 90088006)

Enviroment: OS: Windows 10, 64 bit

IDE: Visual studio community 2022

Error window: https://i.stack.imgur.com/VfjyP.png

code:

#include <iostream>
#include <string>

using namespace std;

int total = 0;

void numf(int n, string c) {
    if (!(c.find('3') == 0 and c.find('4') == 0 and c.find('7') == 0 and
          c.find('2') == 0 and c.find('5') == 0)) {

        // reverse
        string c2;

        for (int i = 0; i < sizeof(c) / sizeof(c[0]); i++) {
            c2[i] = c[sizeof(c) / sizeof(c[0]) - i - 1];
        }
        for (int i = 0; i < sizeof(c) / sizeof(c[0]); i++) {
            if (c2[i] == '6') {
                c2[i] = '9';
            } else if (c2[i] == '9') {
                c2[i] = '6';
            }
        }
        if (c2 == c) {
            total++;
        }
    }
    return;
}

int main() {
    int num, num2;

    cin >> num >> num2;

    for (int i = num; i <= num2; i++) {
        string newnum = to_string(i);
        numf(i, newnum);
    }
    cout << total;
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Eddie
  • 33
  • 3
  • 1
    what do you expect `find` to return? `c.find('3') == 0 and c.find('4') == 0 and c.find('7') == 0` makes no sense. It is not possible that the first character is `'3'` and `'4'` and `'7'` at the same time. Maybe you made a mistake while negating the expression – 463035818_is_not_an_ai Apr 20 '22 at 19:53
  • 1
    Please post text as text, not an image of text. – Scott Hunter Apr 20 '22 at 19:53
  • 3
    `sizeof(c) / sizeof(c[0])` is also not doing what you think it does – 463035818_is_not_an_ai Apr 20 '22 at 19:54
  • *I get no errors or warnings when compiling* -- That doesn't mean your code has no bugs. All compiling successfully means is that your code has no syntax errors. The next step is for you to [debug your code](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems), instead of just running the program, seeing it doesn't work, and then report that it doesn't work. – PaulMcKenzie Apr 20 '22 at 19:54
  • @Eddie `sizeof(c) / sizeof(c[0])` -- Debugging, or just merely printing out this value, would have revealed a hint as to why things are not working. Then a better post for SO would be "why is this not giving the right results?" (which of course there would be duplicate questions, but better than just posting code without any debugging effort). – PaulMcKenzie Apr 20 '22 at 19:58
  • 2
    *IDE: Visual studio community 2022* -- Visual Studio has one of the best debuggers ever created for C++. Not using it, sorry to tell you, maybe worthy for a downvote (I didn't downvote). You should learn how to use this very powerful tool. – PaulMcKenzie Apr 20 '22 at 20:00
  • @463035818_is_not_a_number then what does it do – Eddie Apr 20 '22 at 20:03
  • it is the same as `sizeof(std::string)` (because `sizeof(char)=1`) but I think you want `std::string::size`. The first is the size of a `std::string` object, the latter is the number of characters in the string – 463035818_is_not_an_ai Apr 20 '22 at 20:05
  • @PaulMcKenzie it gave me error, not the wrong results – Eddie Apr 20 '22 at 20:06
  • even if `c` was an array of `char`, `sizeof(c) / sizeof(c[0])` would not work to get its size once it decayed to a pointer when passed to a function – 463035818_is_not_an_ai Apr 20 '22 at 20:07
  • @Eddie And the way you solve the error is to use the debugger. Read the assertion dialog presented to you. It says "Press Retry to debug the application". What do you think you will see when you do this? Yes, you see the debugger, complete with call stack to show you what series of calls led to the error, the value of the variables, etc, etc, – PaulMcKenzie Apr 20 '22 at 20:07
  • @PaulMcKenzie it lead me to the xstring code line 3839, and I can read none of what it says – Eddie Apr 20 '22 at 20:12
  • @Eddie I mentioned the call stack. Did you see it? That shows you the functions *you* wrote that led to the error. I think you need a tutorial on how to debug programs, because obviously you weren't ready to debug your code (and as the link states, it is a necessary skill for any programmer to know how to use their interactive debugging environment). – PaulMcKenzie Apr 20 '22 at 20:13
  • @PaulMcKenzie well, before there was no error, I saw a green line below: c2[i] = '9'; – Eddie Apr 20 '22 at 20:15
  • @Eddie Just in case if you're curious about other ways of solving your interesting task, [here is my solution](https://stackoverflow.com/a/71945769/941531), please put a look. – Arty Apr 20 '22 at 21:02

2 Answers2

1

For starters this if statement

if (!(c.find('3') == 0 and c.find('4') == 0 and c.find('7') == 0 and
      c.find('2') == 0 and c.find('5') == 0)) {

does not make a sense.

It seems you are trying to exclude numbers that contain one of the listed digits.

In this case you should write

if ( c.find_first_of( "34725" ) == std::string::npos )
{
    //...

The class std::string is not an array. So the expression sizeof(c) / sizeof(c[0]) in the loops like below also does not make a sense.

    for (int i = 0; i < sizeof(c) / sizeof(c[0]); i++) {
        c2[i] = c[sizeof(c) / sizeof(c[0]) - i - 1];
    }

Moreover the object c2 is empty. So you may not use the subscript operator like c2[i].

You could just write instead of the loop

std::string c2( c.rbegin(), c.rend() );

This incorrect for loop

    for (int i = 0; i < sizeof(c) / sizeof(c[0]); i++) {
        if (c2[i] == '6') {
            c2[i] = '9';
        } else if (c2[i] == '9') {
            c2[i] = '6';
        }
    }

can be changed for the following range-based for loop

for ( auto &ch : c2 )
{
    if ( ch == '6') 
    {
        ch = '9';
    }
    else if ( ch == '9' ) 
    {
        ch = '6';
    }
}

Pay attention to that the first function parameter is not used within the function.

Also it is a bad idea to use the global variable total within the function. It will be much better if the function had the return type bool and returned true in case when a number satisfies the requirement.

If I have understood the assignment correctly then I would write the program something like the following.

#include <iostream>
#include <string>
#include <utility>
#include <algorithm>

bool numf( const std::string &s )
{
    if ( s.find_first_of( "34725" ) == std::string::npos )
    {
        std::string s2( s.rbegin(), s.rend() );

        for ( auto &c :s2 )
        {
            if (c == '9')
            {
                c = '6';
            }
            else if (c == '6')
            {
                c = '9';
            }
        }

        return s == s2;
    }
    else
    {
        return false;
    }
}

int main()
{
    unsigned int num1 = 0, num2 = 0;

    std::cin >> num1 >> num2;

    std::tie( num1, num2 ) = std::minmax( { num1, num2 } );

    unsigned int total = 0;

    for (; num1 <= num2; ++num1)
    {
        total += numf( std::to_string( num1 ) );
    }

    std::cout << "total = " << total << '\n';
}

The program output might look like

60 99
total = 3
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Interesting task you have. Just in case if you're curious about other ways of solving your task, not only about how to fix your code, for that I encoded my own solution for you.

My solution is using small conversion table "01xxxx9x86" for figuring out what will be the 180 degree rotation of a digit, x here signifies that digit is not rotatable.

As you can see I included 1 as rotatable, same as you did in your code, but in some types of font 1 rotated 180 degrees doesn't look same. You may excluded it from rotatables by marking it with x symbol in conversion table of my code.

Try it online!

#include <cstdint>
#include <string>
#include <iostream>

uint64_t RotateCount(uint64_t begin, uint64_t end) {
    uint64_t cnt = 0;
    char constexpr conv[] = "01xxxx9x86";
    for (uint64_t n = begin; n < end; ++n) {
        std::string const s = std::to_string(n);
        bool matched = true;
        for (size_t i = 0; i < (s.size() + 1) / 2; ++i)
            if (conv[s[i] - '0'] != s[s.size() - 1 - i]) {
                matched = false;
                break;
            }
        if (matched)
            ++cnt;
    }
    return cnt;
}

int main() {
    uint64_t begin = 1'000'000, end = 9'000'000;
    std::cout << "Count: " << RotateCount(begin, end)
        << std::endl;
}

Input range:

1'000'000 ... 9'000'000

Output:

Count: 225

Arty
  • 14,883
  • 6
  • 36
  • 69