0

I keep getting the correct output answers from the input I get on test cases for my problem on my homework but under my output in CodeRunner, it will show a 0 or crazy decimal under the correct output making the test case wrong. Any Suggestions?

double calcSimScore(string, string);

int main()
{
    calcSimScore("CATTATGCATTATATACAT", "CATCACCACCCTCCTCCTC");
}

double calcSimScore(string str1, string str2)
{
    int i;
    int count = 0;

    if (str1.length() == str2.length())
    {
        for (i = 0; i < str1.length(); i++)
        {
            string s1 = str1.substr(i, 1);
            string s2 = str2.substr(i, 1);
            if (s1 != s2)
            {
                count++;
            }
        }

        if (str1.length() - count == 0)
        {
            cout << 1 << endl;
        }
        else
        {
            float str = str1.length() - count;
            float len = str1.length();
            float simScore = str / len;
            cout << simScore << endl;
        }
    }
    else
    {
        cout << 0;
    }
}

the output I get on CodeRunner is:

0.315789 3.22526e-319

user4581301
  • 33,082
  • 7
  • 33
  • 54
Nic Bowles
  • 19
  • 1
  • 3
  • Probably not the bug you're interested in, but `calcSimScore` promises to return a `double` but does not. Much wailing and gnashing of teeth could result. Since the return value is not used, you probably will not notice any effects, but ... – user4581301 Sep 27 '19 at 18:31
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – R Sahu Sep 27 '19 at 18:31
  • Are you trying to read the string from a file or standard input in CodeRunner? – fukanchik Sep 27 '19 at 18:32
  • 2
    Wait a sec. This `CodeRunner` thing is probably trying to use that missing return value. In that case, don't print the output in your function. Compute and return the value. – user4581301 Sep 27 '19 at 18:32
  • from a file @fukanchik – Nic Bowles Sep 27 '19 at 18:34
  • Changing from printing to returning and printing from the main, fixed the issue. thanks @user4581301 – Nic Bowles Sep 27 '19 at 18:43
  • Does it or your compiler issue any warnings? –  Sep 27 '19 at 18:58
  • @Chipster raises a good point. Most modern compilers will detect this and issue a warning. Warnings mean the source code is syntactically correct and can be converted into executable code, but the logic may be wrong and the program likely won't do exactly what you want. Don't ignore warnings messages. In fact, I recommend that you crank them up loud and see if you can turn them into error messages to force you to deal with them. If you do, watch out for the nanny compiler that tries to force a particular style on you with warnings. Their nagging as errors kinda really sucks. – user4581301 Sep 27 '19 at 19:16

1 Answers1

1

user4581301 is right. What you are doing by not returning a value from your non-void function is actually undefined behavior:

Flowing off the end of a function [...] results in undefined behavior in a value-returning function.

Undefined behavior, if you haven't run into it yet literally permits anything to happen, including appearing to work or printing some weird result.