Invalid operands of types 'void' and 'bool' to binary 'operator=='

Mình đã sai ở đâu vậy ? :zipper_mouth_face:

  #include <iostream>

    using namespace std;

    void NhapMang(int a[], int n)
    {
        for (int i=0;i<n;i++)
        {
            cout << "Phan tu thu " <<i +1 << " ";
            cin >> a[i];
        }
    }
    void XuatMang(int a[],int n)
    {
        for (int i=0;i<n;i++)
        {

            cout << "Phan tu mang a[" <<i << "] " << a[i]<<endl;
        }
    }
    void KiemTraSNT(int n)
    {
        bool KT = true;

        if(n<2)
        {
        KT = false ;
        }
          else
          {
            if (n > 2 && n%2==0)
             {
                KT = false;
            }
            else
             {
                for (int j=3; j<n;j=j+2)
                {
                    if (n%j==0)
                    {
                        KT = false;
                        break;
                    }
                 }
             }
           }
        //return KT;
    }
    void XuatSoNguyenTo(int a[],int n)
    {
        for (int i =0; i<n;i++)
        {

           if (KiemTraSNT(a[i]) == true)
           {

           cout << a[i] << " ";
           }

         }
    }
    void XuatSoSieuNguyenTo(int a[], int n)
    {
        for (int i =0; i<n;i++)
        {
            int tam = a[i];
            bool KT =true;
            while (tam != 0)
         {
            KT =true;
           if (KiemTraSNT(tam) == true)
           {
           // for (int i )
           tam = tam/10;
           // cout << a[i] << " ";
           }
           else
           {
               KT = false;
               break;
           }

          }
          if (KT == true)
          {
              cout << a[i] <<" ";
          }

         }
    }
    int main()
    {
        int a[100];
        int n;
        cout <<"Nhap so phan tu cua mang: ";
        cin >> n;
        NhapMang(a,n);

        cout << "XUAT MANG";
        XuatMang(a,n);
        cout << "\nSo nguyen to";
    XuatSoNguyenTo(a,n);
        cout << "\nSO sieu nguyen to";
        XuatSoSieuNguyenTo(a,n);
    return 0;
    }
void KiemTraSNT(int n)
if (KiemTraSNT(a[i]) == true)

Hàm KiemTraSNT() trả về void mà lại so sánh với bool

Đoạn cuối hàm:

//return KT;

Rõ là đi đúng rồi mà lại không đi tiếp.
Sửa kiểu trả về là bool.

4 Likes

thank bác, vật vã cả buổi chiều vì cái lỗi này

A post was split to a new topic: Thắc mắc về DNH

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?