0

My function has something wrong:

#include<iostream>

using namespace std ;
int max=1;
int lcm(int i){
    if(max%i==0){
    }
    else 
        for (int b=2; b<=i ; b++){
            max*=b;
            max/=(b-1);
            if(max%i==0){
                break;
            }
        }
    }
int main(){
int n;
cin>>n;
int i[n];
for(int j =0 ; j<n; j++){
    cin>>i[j];
    lcm(i[j]);
}
cout<<max+1;
}

This program gives me the lcm of several numbers.

I use global in python when I have this problem in my code whats function I should use in c++?

Or I should make my code better?

anastaciu
  • 23,467
  • 7
  • 28
  • 53
md-yz
  • 11
  • 4
  • 1
    Unrelated: turn up your compiler warnings! your lcm function promises to return an int but never returns anything. That is a good way to crash your program. – Botje Aug 14 '20 at 14:32

2 Answers2

3

That's what happens when you use using namespace std, your variable max is ambiguous because it conflicts with std::max library function.

Don't use using namespace std, and use std::cin, std::cout, etc., or at the very least use specific using expressions, like using std::cin;.

You could also change the name of the variable, but the previous considerations about good coding practices should still be followed.

One other thing, about int i[n];, variable length arrays are not part of C++ standard, though this is allowed by some compilers, you may want to use std::vector instead.

Finally, as @Botje pointed out, using a function with non-void return type with missing return statement leads to undefined behavior.

cigien
  • 57,834
  • 11
  • 73
  • 112
anastaciu
  • 23,467
  • 7
  • 28
  • 53
2

This is one of the (many) reasons you should never do using namespace std;. If you do that it brings in all the names from std, which includes the function std::max. This conflicts with the variable int max giving you an ambiguity error.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • That's cargo cult. `using namespace std` is fine, just learn how to use it wisely. - Not in header files (because it propagates to other header files) - Not along `using namespace foo` if you're not sure what's inside `foo` and whether it may conflict with std. Just don't declare your variables/functions/classes directly in the global namespace in the first place... Use your own namespace instead. And use namespace aliases like `using fs = std::filesystem` whenever possible. – m88 Aug 14 '20 at 15:29
  • 3
    @m88 -- so, how would you fix the problem in the question? Post a real solution rather than insulting ("cargo cult") various experts who you, apparently, disagree with. – Pete Becker Aug 14 '20 at 17:23
  • @PeteBecker Have you read my comment after the 1st sentence? It doesn't seem so. – m88 Aug 15 '20 at 19:00
  • @m88 -- I read your entire comment several times, trying to figure out what its technical content was supposed to be. The comment has various hand-waving rants, but it does not even hint at an answer. Clearly you don't approve of this answer, even though it solves the problem. What do you suggest to solve the problem? – Pete Becker Aug 15 '20 at 19:06
  • @PeteBecker Here's what I suggested: https://codeshare.io/5MQBjJ (many other problems in this code, but let's ignore it). Since foo::max would still conflict with std::max, you can't use both `using namespace` at the same time. So you either rename foo::max to prevent conflicts with std, or you keep the foo prefix in your main. Honestly I'd chose the 1st one. – m88 Aug 15 '20 at 20:20
  • @m88 — so your solution is to remove `using namespace std;`. – Pete Becker Aug 15 '20 at 20:42
  • @PeteBecker I'm sorry to ask that again but, did you read the code I linked? Or did you miss the "using namespace std;" in it? – m88 Aug 15 '20 at 21:08
  • @m88 — sigh. As you surely know, you **moved** the using declaration to **after** the use of `max`. Pretending that that’s different, in this context, from removing it is simply dishonest. – Pete Becker Aug 16 '20 at 02:15
  • @PeteBecker Because lines 3 to 21 are just an inline equivalent to `#include "mycode/foo.hpp"` so it's only logical that I use the `using namespace std` after. That's a corollary to the 3rd sentence in my original comment, so it's not something I'm pulling out of my hat... – m88 Aug 16 '20 at 09:59
  • @PeteBecker And by the way, "experts" (I don't know who you're refering to) don't disagree with me on this topic. Have a look at SF6 and SF7 in particular: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#sf6-use-using-namespace-directives-for-transition-for-foundation-libraries-such-as-std-or-within-a-local-scope-only – m88 Aug 16 '20 at 10:07