Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include<stdio.h>
#include<string.h>

struct kelompok{
    char idpegawai[5];
	char nama[10];
	int umur;
};
int main(void)
{
	struct kelompok k1;
	struct kelompok k2;
	struct kelompok k3;
	struct kelompok k4;
	
	strcpy(k1.idpegawai,"P001");
	strcpy(k1.nama,"revan");
	k1.umur =18;
	
	strcpy(k2.idpegawai,"P002");
	strcpy(k2.nama,"joo");
	k2.umur =18;
	
	strcpy(k3.idpegawai,"P003");
	strcpy(k3.nama,"rev");
	k3.umur =18;
	
strcpy(k4.idpegawai,"P004");
	strcpy(k4.nama,"van");
	k4.umur =19;
	
	printf("idpegawai | nama     | umur    ");
	printf("\n================================");
	printf("\n%s\t  |", k1.idpegawai, "\t %s", k1.nama, "\t %d", k1.umur);
	printf("\n%s\t  |", k2.idpegawai, "\t %s", k2.nama, "\t %d", k2.umur);
	printf("\n%s\t  |", k3.idpegawai, "\t %s", k3.nama, "\t %d", k3.umur);
	printf("\n%s\t  |", k4.idpegawai, "\t %s", k4.nama, "\t %d", k4.umur);
	printf("\n================================");
	return 0;
}
Is there any wrong in my program? 
When I run the program,"nama, and umur" are disappear .Confused about how to make the "nama, umur" not disappear, so that it is on the right side, like this  :

Id                |    Nama         | umur
=============================
P001          | employee      | 18
P002          | employee      | 18
P003          | employee      | 18
P004          | employee      | 18
=============================


What I have tried:

I tried all what i know, ,iam Newbie 🙂. Thanks btw if ur guys help me.
Posted
Updated 4-Nov-22 5:19am
Comments
jeron1 4-Nov-22 10:52am    
You should have your format string, then your variables when using the printf() function
try something like,

printf("\n%s\t |\t %s |\t %d", k1.idpegawai, k1.nama, k1.umur);
Member 15627495 4-Nov-22 11:06am    
for the printf , put all the vars at end of the print f ... just keep their type "%s"
jeron1 4-Nov-22 11:59am    
"just keep their type "%s" "
But variable 'umur' is declared as an int.
Member 15627495 4-Nov-22 16:20pm    
printf("\n%s\t  | \t %s \t %d",k3.idpegawai, k3.nama, k3.umur);
jeron1 4-Nov-22 16:44pm    
 put all the vars at end of the print f ... just keep their type "%s" 
So we are adding a %d to that as well.

1 solution

jeron1 gave you the important part - the printf calls need to be changed. Here is the documentation of the function : cplusplus.com : printf[^]. Printf needs to have one format string followed by the various arguments. For you that would look like this :
C++
printf("\n%s\t  |\t %s\t %d", k1.idpegawai, k1.nama, k1.umur );
One thing I recommend is that you move your new line characters (the '\n') to the end of the lines so the cursor is always left at the start of a line. That would make printf call look like this :
C++
printf("%s\t  |\t %s\t %d\n", k1.idpegawai, k1.nama, k1.umur );
It is a minor thing but it will make things a bit easier in general.

Also, you should take advantage of a feature in c++ whereby structs are actually classes with all members public by default. For your program, this means you could add a constructor to your class and cut down the amount of code considerably. Here's how that could look :
C++
struct kelompok
{
    // members

    char idpegawai[5];
	char nama[10];
	int umur;

	// methods

    kelompok() {}   // default constructor

    // parameterized constructor

    kelompok( const char * id, const char * n, int u )
    {
	     strcpy( idpegawai, id );
	     strcpy( nama, n );
	     umur = u;
    }
};
and then the code in your main function could look like this :
C++
int main(void)
{
	struct kelompok k1( "P001", "revan", 18 );
	struct kelompok k2( "P002", "joo", 18 );
	struct kelompok k3( "P003", "rev", 18 );
	struct kelompok k4( "P004", "van", 19 );

    // and so on...
One last suggestion - add a print method to your structure. Here is how its prototype would look in the class :
C++
struct kelompok
{
    // after previous code...

    void print()
    {
        printf("%s\t  |\t %s\t %d\n", idpegawai, nama, umur );
    }
};
here is how the revised main function could look with those changes :
C++
int main(void)
{
	struct kelompok k1( "P001", "revan", 18 );
	struct kelompok k2( "P002", "joo", 18 );
	struct kelompok k3( "P003", "rev", 18 );
	struct kelompok k4( "P004", "van", 19 );

	printf("idpegawai | nama     | umur    \n");
	printf("================================\n");

    k1.print();
    k2.print();
    k3.print();
    k4.print();

	printf("================================\n");
    return 0;
}
 
Share this answer
 
Comments
jonathan legends 4-Nov-22 11:54am    
Thank youuu so muchhhh 🤩😍😍 sho help full... Arghhh so happy man tytytyty!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900