ช่วยเช็คโค้ด ภาษาC++ให้หน่อยค่ะ

มีกรณีอยากถาม2กรณีค่ะ
1.ถ้าเกิดว่าต้องการนำส่วนที่มีอาเรย์2มิติเข้าฟังก์ชัน เช่น void-function / value-returning function ต้องทำอย่างไรหรอคะ
พอลองทำแล้ว compile ไม่ผ่านเลยค่ะ

2.มีการใช่ do-while เมื่อ while(ans =='y' || ans =='Y'); ซึ่งans คือ char ที่พิมพ์เข้าไปค่ะ แต่พอrun แล้วloopไม่วนค่ะ มันทำรอบเดียว

เป็นโปรแรมดึงไฟล์เข้ามาคำนวนหาเลขน่ะค่ะ




#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    char ans;
do{
    char fname[50];
    cout<<"Please enter the input file name: ";
    cin>>fname;
    ifstream ifile (fname);
    
    if(!ifile) cout<<"cannot open file"; //check file
    
    int sz;
    ifile>>sz;
    int a[sz][sz];
    for (int i=0; i<sz;i++)
        for(int j=0; j<sz;j++)
        ifile >>a[j];
        
    cout<<"your input is..."<<endl;
    
        for (int i=0; i<sz;i++){
        {
        for(int j=0; j<sz;j++)
        cout<<a[j]<<" ";
        }
        cout<<endl;    }


int sumrow[20], sumcol[20],sumrl[20],sumlr[20],sum1,sum2,sum3,sum4,mgsqr;
char ans;

for(int i=0; i<sz;i++)  // check row
{
    {sum1=0;
    for(int j=0;j<sz;j++)
    sum1=sum1+a[j];
    }        
    sumrow=sum1;
        
    int check=sumrow[0];
    if(sumrow!=check)
    mgsqr=0;
    else mgsqr=check;
    
    
    //cout<< sumrow <<endl;
}
    
if(mgsqr){
for(int i=0;i<sz;i++)  //check column
{
    {
    sum2=0;
    for(int j=0;j<sz;j++)
    
        sum2=sum2+a[j];
    sumcol=sum2;
    }    
    int check=sumcol[0];
    if(sumrow!=check)
    mgsqr=0;
    else mgsqr=mgsqr;
    
    //cout<< sumcol <<endl;
}
}
    
if(mgsqr){
//check from top left to bottom right
{
sum3=0;
for(int i=0;i<sz;i++)
    sum3=sum3+a;
}    
    int check=sum3;
    if(sum3!=check)
    mgsqr=0;
    else mgsqr=mgsqr;
    
//    cout<< sum3 <<endl;

}


if(mgsqr){
//check from top right to bottom left
    {    sum4=0;
        for(int i=0;i<sz;i++)
            sum4=sum4+a[sz-1-i];
    }
    
    int check=sumrl[0];
    if(sum4!=check)
    mgsqr=0;
    else mgsqr=mgsqr;
    
//    cout<< sum4 <<endl;
}




if (sum4!=0)
{
     cout<< "Congratulations!! Your square is a magic square!!"<<endl;
    cout<<"The magic constant of this square is "<<sum4<<endl;
}
else cout<< "Your square is not a magic square!!"<<endl;


cout<<"Try more <y/n>? ";
cin>>ans;
} while(ans=='Y' || ans=='y');

cout<<"Bye.";


return 0;
}

ตัวอย่างหน้าจอแสดงผลค่ะ
Please enter the input file name: myInputTable.txt <enter>
Your square is not a magic square!!
Try more <y/n>? y <enter>
Please enter the input file name: myTable2.txt <enter>
Congratulations!! Your square is a magic square.
The magic constant of this square is 15.
Try more <y/n>? Y <enter>
Please enter the input file name: theSquare.txt <enter>
Your square is not a magic square!!
Try more <y/n>? n <enter>
Bye.
<จบโปรแกรม>
แก้ไขข้อความเมื่อ
แสดงความคิดเห็น
อ่านกระทู้อื่นที่พูดคุยเกี่ยวกับ  C++ วิทยาศาสตร์คอมพิวเตอร์ มหาวิทยาลัย
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่