For Loop
Dec 14, 2014 at 12:57pm UTC
why the output isn't same as the input? please correct the program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include<stdio.h>
struct mhs
{
char name[20];
int nim;
};
struct data
{
char address[20];
char country[20];
};
main()
{
struct mhs a;
struct data b;
int i;
for (i=0; i<5; i++)
{
printf("\nName: " );
scanf("%s" , &a.name);
printf("NIM: " );
scanf("%d" , &a.nim);
printf("Address: " );
scanf("%s" , &b.address);
printf("Country: " );
scanf("%s" , &b.country);
}
for (i=0; i<5; i++)
{
printf("\nName= %s" , a.name);
printf("\nNIM= %d" , a.nim);
printf("\nAdress= %s" , b.address);
printf("\nJCountry= %s" , b.country);
}
}
Dec 14, 2014 at 1:09pm UTC
An array's name is a pointer to the array itself: remove & when you read into an array and leave it only for the int.
Dec 14, 2014 at 1:10pm UTC
can you correct at the code please?
Dec 14, 2014 at 1:11pm UTC
I told you to remove the &s... It is not that complicated! :)
1 2 3 4 5 6 7 8
printf("\nName: " );
scanf("%s" , a.name);
printf("NIM: " );
scanf("%d" , &a.nim);
printf("Address: " );
scanf("%s" , b.address);
printf("Country: " );
scanf("%s" , b.country);
Dec 14, 2014 at 1:14pm UTC
the output still same. the output will be same as the output. it really confused me
Dec 14, 2014 at 1:17pm UTC
By the way... if you want more values then you need to use an array of struct.
Otherwise you overwrite the same value again and again...
Dec 14, 2014 at 1:30pm UTC
i dont know what you mean. i just want the output will be same as the input. 5 data's input will be same as the output. but the program doesn't. please help
Dec 14, 2014 at 1:32pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <stdio.h>
struct mhs {
char name[20];
int nim;
};
struct data {
char address[20];
char country[20];
};
int main() {
struct mhs a[5];
struct data b[5];
int i;
for (i = 0; i < 5; i++) {
printf("\nName: " );
scanf("%s" , a[i].name);
printf("NIM: " );
scanf("%d" , &a[i].nim);
printf("Address: " );
scanf("%s" , b[i].address);
printf("Country: " );
scanf("%s" , b[i].country);
}
for (i = 0; i < 5; i++) {
printf("\nName= %s" , a[i].name);
printf("\nNIM= %d" , a[i].nim);
printf("\nAdress= %s" , b[i].address);
printf("\nJCountry= %s" , b[i].country);
}
return 0;
}
Dec 14, 2014 at 1:59pm UTC
thankyouu
Dec 14, 2014 at 2:00pm UTC
Hope you understand what I did. Otherwise... just ask.
Topic archived. No new replies allowed.