irpg Community

Full Version: ถามด้านภาษา C การเขียนโปรแกรมแสดงคะแนน
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(){
char name[10];
int study,score[10],i,count=0;
printf("Enter Number of student:");
scanf("%d",&study);
for(i=1;i<=study;i++){
printf("No.%d\n",i);
printf("Enter name:");
scanf("%s",name);
printf("Enter score:");
scanf("%d",&score[i]);
}
system("cls");
printf("More than 50 is pass\n");
for(i=1;i<=study;i++){
if(score[i]>=50 && score[i]<=100){
count=count+1;
printf("No.%d %s score %d\n",i,name,score[i]);
}

}
printf("The student is pass the test is %d people",count);
getch();
}
อันนี้ผมเขียนเป็นโปรแกรมตรวจสอบนักเรียนที่ผ่านกี่คน
จากโค้ดก็ไม่มีอะไรผิดแหละครับ แต่เพียงแต่ว่า ผลลัพธ์สุดท้ายนี่สิครับ
หากเราให้แสดงชื่อ ของคนที่สอบผ่าน จะต้องใช้ฟังชั่นใดอะครับ เพราะผลลัพธ์ที่แสดงผล
มันขาดตรงชื่อ ข้อความเป๋็นสตริง ที่แสดงไม่ตรงกับข้อมูลที่เราป้อนไว้อะครับ

หากงงๆลองไปเทสโค้ดดูก่อนได้ครับ ผมติดตรงชื่อเนี่ยแหละครับแสดงชื่อไม่ตรงตามข้อมูลที่ป้อน

ขอบคุณล่วงหน้าครับ
ตัวแปร name เป็น array ต้องใส่ index ด้วยครับ name[i]
ผมกลัวว่าพอแสดงผลเป็นชื่อ มันจะปสดงเพียงตัวอักษรเดียวอะครับ เป็นอักขระแทน
งั้นเปลี่ยน char เป็น pointer array สิครับ
ทำยังไงอะครับไม่เคยใช้เลย
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main(){
char (*name[10])[10];
int study,score[10],i,count=0;

printf("Enter Number of student:");
scanf("%d",&study);
for(i=1;i<=study;i++){
printf("No.%d\n",i);
printf("Enter name:");
scanf("%s",&name[i]);
printf("Enter score:");
scanf("%d",&score[i]);
}
system("cls");
printf("More than 50 is pass\n");
for(i=1;i<=study;i++){
if(score[i]>=50 && score[i]<=100){
count=count+1;
printf("No.%d %s score %d\n",i,&name[i],score[i]);
}
}
printf("The student is pass the test is %d people",count);
getch();
}
จะลองเอาไปใช้ดูครับขอบคุณครับ