简体   繁体   中英

Why doesn't this c++ code template work? I'm having trouble

#include <stdio.h>
int main()
{
    int list[10];
    int a,b,c;
    int d=1;
    int e=0;
    printf("Enter a starting number to find 10 prime numbers: ");
    scanf("%d",&a);
    printf("Enter a ending number to find 10 prime numbers: ");
    scanf("%d",&b);
    for(c=0;c<10;c++)
    {
        while(a<b)
        {
            while(d<a)
            {
                if(a%d==0)
                {
                    e=1;
                }
                d++;
            }
            a++;
        }
        if(e==0)
        {
            list[c]=a;
            printf("%d is prime.",list[c]);
        }
    }
}

The code that will add 10 prime numbers in the given range to the list arr and print them. Why doesn't it work? Thanks. Please explain.

to flesh out @Craig answer

   for(c=0;c<10;)
    {
        while(a<b)
        {
            while(d<a)
            {
                if(a%d==0)
                {
                    e=1;
                }
                d++;
            }
            a++;
        }
        if(e==0)
        {
            list[c]=a;
            printf("%d is prime.",list[c]);
            c++;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM