Cicling problem-Very begginer

Hello world!! That's what i frst learned at programing. But now i have advanced a little and i have some problems so i come to you experienced programers hoping that you can help me. My program is to find every abcd(uplined) number which divided to an input n gives an integer result. the digits a b c d are diferent.My code looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n, a=0, b=0,c=0,d=0, num, i, g;
    cin>>n;
    for(num=0, num<10000, num++)
    {i=num;
     d=num%10;
     num=num/10;
     c=num%10;
     num=num/10;
     b=num%10;
     num=num/10;
     a=num%10;
     if(i%n=0&&a!=b&&a!=c&&!=d&&b!=c&&b!=d&&c!=d)
     {g=1000*a+100*b+10*c+d;
      cout<<g<<endl;};};
    system("PAUSE");
    return EXIT_SUCCESS;
}


But unfortunely i have some errors that don't let my program work. The errors are :
10 C:\... expected `;' before ')' token
23 C:\... expected primary-expression before "return"
23 C:\... expected `)' before "return"
If you think you can please give me a hand and solve these errors. Thabk you!
the for loop requires ; not ,:

for(num=0; num<10000; num++) // Note ;
instead of EXIT_SUCCESS I usually just close it with return 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n, a=0, b=0,c=0,d=0, num, i, g;
    cin>>n;
    for(num=0; num<10000; num++)
    {i=num;
     d=num%10;
     num=num/10;
     c=num%10;
     num=num/10;
     b=num%10;
     num=num/10;
     a=num%10;
     if(i%n=0&&a!=b&&a!=c&&!=d&&b!=c&&b!=d&&c!=d)
     {g=1000*a+100*b+10*c+d;
      cout<<g<<endl;}}//extra semicolons here were not needed
    system("PAUSE");
    return 0;
}

Last edited on
That was syntax.

Then there is logical error. Lines 13, 15, and 17 modify the num. A loop counter that is divided by 1000 on every iteration shall never reach 10000.

Semantic note following from the previous: If num is not modified, then num==i==g.

Optimization: Test division by n before calculating anything else.

Input sanity: The user will write "", "0", or "foo". You cannot use invalid values in modulo.

Last, do not call system(). There are better ways in the sticky.
keskiverto,

how good are you with java? I'm having some troubles creating a simon says game. i have an hour and a half to finish it, i'm running on nothing but monster drinks and no sleep, and this is the only thing keeping me from getting my associates degree in computer science. Can you help?

Thanks in advance
Thank you for all the help provided. It helped me solving some errors AND savig some space but i still have one error an it is:
19 C:\... non-lvalue in assignment
on the folowing code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n, a=0, b=0,c=0,d=0, num, i, g;
    cin>>n;
    for(num=0; num<10000; num++)
    {i=num;
     d=num%10;
     num=num/10;
     c=num%10;
     num=num/10;
     b=num%10;
     num=num/10;
     a=num%10;
     if(i%n=0&&a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
     {cout<<i<<endl;};};
    system("PAUSE");
    return 0;
}
And i forget to correct the logical error seen by keskiverto:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int n, a=0, b=0,c=0,d=0, num, i;
    cin>>n;
    for(num=0; num<10000; num++)
    {i=num;
     d=i%10;
     i=i/10;
     c=i%10;
     i=i/10;
     b=i%10;
     i=i/10;
     a=i%10;
     if(num%n=0&&a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d)
     {cout<<num<<endl;};};
    system("PAUSE");
    return 0;
}

I hope now is logically correct.
Last edited on
This num%n=0 needs to be num%n==0

Note:
= is assignment
== is comparison
That is correct. Post if you have any other questions, I'm trying to brush up on my C++ basics.
Thank you all for your help.. i am sorry if i bothered you with simple questions.
It's all good, ask as many questions as you can now because the basics are the most important when starting with C++. Most of the people here are very helpful.
Another note. Lets take n==10. Is num==10 a valid answer? Code says "no".
d==0
c==1
b==0
a==0

One can argue that there is no hundreds and thousands in 10, and non-existing 'a' is not equal to non-existing 'b' is not equal to 'd', which exists and is 0. The code does not handle the num<1000 as special cases.
num==10 is not a valid answer at least not in my problem.. i should have said that the number needs 4 digits... i am sorry about that.
No problem. Then you can naturally start the loop from 1234 1023 rather than 0.
Last edited on
A good idea
This problem can be broken down as follows:

for i from 1000 to 9999
if i has unique digits and is divisible by n
display i

and implemented as follows:

http://ideone.com/RaGvdU

Edit: @keskiverto, I suppose you mean 1023?
Last edited on
Topic archived. No new replies allowed.