Jump to content
Sign in to follow this  
nhafizaj

Nak Guna Apa Selain Gets Dan Scanf?

Recommended Posts

#include <stdio.h>

typedef struct student_record {

char name[50];

char id[20];

char programme[30];

int year;

float cgpa;

};

void main(void)

{

struct student_record data[100];

char cont;

int i=0,trav;

do{

printf("Enter student's name: ");

gets(data.name);

printf("Enter matrics number: ");

scanf("%s",data.id);

printf("Enter student's programme name: ");

scanf("%s",data.programme);

printf("Enter student's year: ");

scanf("%d",&data.year);

printf("Enter student's CGPA: ");

scanf("%f",&data.cgpa);

printf("\nStill Want to Continue?(Y/N)");

scanf("%s",&cont);

i++;

}while(cont=='y'||cont=='Y');

printf("\n\nStudents' Records Are:\n");

for(trav=0;trav<i;trav++){

printf("\nname: %s\n",data[trav].name);

printf("matrics number: %s\n",data[trav].id);

printf("programme: %s\n",data[trav].programme);

printf("year: %d\n",data[trav].year);

printf("CGPA: %.2f\n",data[trav].cgpa);

}

}

ni program assignment aku. Bile guna gets,2nd loop tak boleh Enter student's name, terus jump kepada Enter matrics number. kalau guna scanf, between nama yg dimasukkan tak bleh ada space..

so nak buat cmner ek kalau aku nak masukkan nama yang boleh spacing ,contoh: mohd ali?

Share this post


Link to post
Share on other sites

jgn pakai scanf langsung (banyak problem 'jump to next line' kalau input tak sama dgn input setting) dan pastikan anda juga menggunakan latest modern c/c++ compiler

contoh:

printf("year: %d\n",data[trav].year);

kalau code di atas awak cuba masukkan string, dia akan jump

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...