-3

I just recently figured I'd try to learn how to code. I'm at the basics and found some exercises to do online, but I've sat at this for ~40 mins and can't figure it out. So even though this might seem basic to most of you I'll ask for some help :)

So here's the task:

The specified integers are in the range [m, n]. Write a program to find the number with the greatest divisors.

Input / output :

Initial data |   Result

10 40        |        36

Code :

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    int m, n, s, sd = 0, ats;
    double a = 0;
    ifstream fd("Duomenys.txt");
    fd >> m >> n;
    for(int i = m; i<=n; i++){
        s = 0;
        for(int j = 1; j<=i; ++j){
            a = 0;
            a = i % j;
            if(a = 0) s= s + 1; 
        }
    if(s > sd) {
        i = ats;
        s = sd;

        }
    }
    cout << ats;
        system("PAUSE");
    return EXIT_SUCCESS;
}

Now I'm only using cout << ats to test it for the moment, but if I run it it gets a 0 as an answer.

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Kotton
  • 3
  • 2

1 Answers1

3

If you want to test whether a is equal to 0 or not, you have to use the Equal to operator, which is ==.

With if (a = 0), you're actually assigning 0 to a and then testing the result of this operation, which will always be 0. That's why the instructions of the if, here s = s + 1; will never be executed.

Other problems in your code:

  • You use the variable ats but never initialize it, so its value is undetermined
  • I don't see any reason why a is a double instead of an int

I suggest you to turn on all your compiler warnings and to start with a good book or tutorial about C++

Community
  • 1
  • 1
BlackDwarf
  • 2,070
  • 1
  • 17
  • 22
  • Thanks a lot man! Figured it all out now! And it was a double because i was trying something difrent at the start :P Anyways thanks alot for taking the time to help me out! – Kotton Sep 25 '15 at 15:14