0

VS Code (wrong output) - Online IDE (right output)

The VS Code (wrong output) - Firstly, I made the code in different IDE but then I want to try the VS Code editor just to test the water but for some reason the code ran perfectly in the different IDE and Online IDE but got wrong output in VS Code - sorry for my bad english I'm still a first year student.

[Red is VS Code & Yellow is Online IDE - Picture]

#include <iostream>
using namespace std;

int main (){
    int num;
    bool ans;
    int prime_num(int m);
    
    cout << "Enter a number: ";
    cin >> num;
    
    ans = prime_num(num);
    if (ans == true)
        cout << num << " is a prime number" << endl;
    else
        cout << num << " isn't a prime number" << endl ;
    
    system("pause");
    return 0;
}

int prime_num(int m){
    bool result;
    if (m == 0 || m == 1){
        result = false;
    }
    for (int i = 2; i <= m/2; ++i){
        if (m % i == 0){
            result = false;
            break;
        }
        else 
            result = true;
    }
    return result;
}

VS Code (right output) - Online IDE (wrong output)

*\So, I've tried changing the bool value but then it got inverted for the answer for both environment.*

[Red is VS Code & Yellow is Online IDE - Picture]

#include <iostream>
using namespace std;

int main (){
    int num;
    bool ans;
    int prime_num(int m);
    
    cout << "Enter a number: ";
    cin >> num;
    
    ans = prime_num(num);
    if (ans == false)
        cout << num << " is a prime number" << endl;
    else
        cout << num << " isn't a prime number" << endl;
    
    system("pause");
    return 0;
}

int prime_num(int m){
    bool result;
    if (m == 0 || m == 1){
        result = true;
    }
    for (int i = 2; i <= m/2; ++i){
        if (m % i == 0){
            result = true;
            break;
        }
        else 
            result = false;
    }
    return result;
}
Sanjeev
  • 348
  • 2
  • 9
  • Afraid I'm not all that good with colour. Could you mark the stuff that's red and yellow in the text version? – user4581301 Dec 01 '22 at 02:07
  • VS Code doesn't compile C++ by itself. Are you using an extension? Also, which online IDE is this? – qrsngky Dec 01 '22 at 02:11
  • Recommendation: remove the user input and hard-code the value you're using for testing. Makes testing easier, and if you find that changes the behaviour of the program you've learned something interesting about the problem. – user4581301 Dec 01 '22 at 02:12
  • 1
    What is half of 3 in integer math? – user4581301 Dec 01 '22 at 02:13
  • 1
    Try adding `cout << "Result is assigned false";` etc. in your program, and you'll notice that the value is never assigned for an input of `3`. And the value before assignment ("garbage value") depends on the compiler. (And there are other problems with your code) – qrsngky Dec 01 '22 at 02:17
  • While the linked topics (closure remarks) do not answer on how to fix the other problems (other than the uninitialized variable), there are plenty of other threads about prime numbers in C++ https://stackoverflow.com/search?q=prime+number+C%2B%2B One example: https://stackoverflow.com/questions/36797150/prime-number-function – qrsngky Dec 01 '22 at 03:20
  • @qrsngky for online ide, i used programiz.com, as for windows ide, i used embarcadero dev-c++ previously because that is what my school recommended. – Azmin Izzuddin Dec 01 '22 at 05:39
  • @user4581301 i had to put the user input because that is required, so cannot be removed. – Azmin Izzuddin Dec 01 '22 at 05:40
  • You can, and should, do anything you want while developing the program. Add in the simple, low-risk stuff like user input at the end of the job so you have more time to focus on the hard stuff. Once you know three works test 5, 7, 11, 14, whatever. But if you know 3 doesn't work, why waste time typing 3 over and over and over? – user4581301 Dec 01 '22 at 06:04
  • One of the big mistakes we see people making here is, "My teacher said not to use `string`!" so they didn't even look at it to see how it could make their lives easy. Screw that. Use `string` to get the program working, then replace `string` with your version of `string`. Solve one problem at a time, and if some one else has already solved a problem, use their solution to make sure the rest of your code is working before tackling that problem. – user4581301 Dec 01 '22 at 06:05
  • I solved it, I just put ' bool result = true; ' Thanks for helping me – Azmin Izzuddin Dec 01 '22 at 08:11
  • @AzminIzzuddin adding that only fixes the inconsistency between different compilers. There are other problems with the function. – qrsngky Dec 02 '22 at 02:54

0 Answers0