Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
#include<string.h>

struct kelompok{
    char idpegawai[5];
	char nama[10];
	int umur;
	
	kelompok(){}
	
	kelompok( const char * id, const char * n, int u )
	{
		strcpy( idpegawai, id );
		strcpy( nama, n );
		umur = u;
	}
	void print()
	{ 
	printf("%s\t  | %s    | %d\n", idpegawai, nama, umur );
	}
};

int main(void)
{
	
	struct kelompok k1( "P001", "Alvionina", 18 );
	struct kelompok k2( "P002", "Stevanny ", 18 );
	struct kelompok k3( "P003", "Joanita  ", 18 );
	struct kelompok k4( "P004", "Revan    ", 18 );
	
	printf("idpegawai | nama         | umur    \n");
	printf("================================\n");
	k1.print();
	k2.print();
	k3.print();
	k4.print();
	printf("================================\n");
	return 0;
}


What I have tried:

Which one i have change to make my program code inti array of struct?
Can anybody help me 😃I'm still confused i try already.
Posted
Updated 5-Nov-22 10:43am
v2

The program currently looks more like C than C++. With C++ it would make sense to include <iostream>, <string> and <vector>.
Instead of struct, class would be better and the variables declared as private.
String processing is much easier and safer
C++
// strcpy(idpegawai, id);
idpegawai =id;

and also the loop in the main program is much easier.
C++
int main(void)
{
	vector <class kelompok> kel;

	kel.push_back({ "P001", "Alvionina", 18 });
	kel.push_back({ "P002", "Stevanny", 18 });
	kel.push_back({ "P003", "Joanita", 18 });
	kel.push_back({ "P003", "Joanita", 18 });
	kel.push_back({ "P004", "Revan", 18 });

	cout << "idpegawai\t| nama\t| umur\n";
	cout << "================================\n";

	for (auto it=kel.begin();  it != kel.end(); ++it) 
	{
		it->print();
	}

	cout << "================================\n";
	return 0;
}
 
Share this answer
 
Comments
Richard MacCutchan 5-Nov-22 13:49pm    
It's fairly obviously complete C. The tags are not always a good indicator of the language type, especially with all the different versions.
merano99 6-Nov-22 3:03am    
For me the tags indicate what it should be or what solution is desired. If something completely different appears under "What I have tried:" I assume that the questioner has problems to implement it in the desired environment or is not even aware that his approach does not fit. With C and C++ there is however as well known a large intersection.
I am not sure if it would be acceptable to simply delete or ignore the possibly wrong tags. I would only do that if it is obviously wrong or incorrect.
Richard MacCutchan 6-Nov-22 3:17am    
I agree, but not everyone who opens a question checks the tags carefully.
Try this:
C++
int main()
{
    struct kelompok kel[] = {
        { "P001", "Alvionina", 18 },
        { "P002", "Stevanny ", 18 },
        { "P003", "Joanita ", 18 },
        { "P004", "Revan ", 18 },
        { "", "", -1 }
    };


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

    for (int index = 0; kel[index].umur != -1; ++index)
    {
        kel[index].print();
    }
    printf("================================\n");


[edit]
Added the actual array name.
[/edit]
 
Share this answer
 
v3
Comments
jonathan legends 5-Nov-22 9:53am    
Not work man 😟
Richard MacCutchan 5-Nov-22 10:15am    
Oops, I forgot to put a name on the array.

It would have helped if you had provided the actual error message, instead of just saying, "Not work man".
jonathan legends 5-Nov-22 10:20am    
Where i have input the code? After int main() or berore int main?
Richard MacCutchan 5-Nov-22 10:37am    
Just replace the four lines you have with the array I coded above. I have also added a final line to mark the end of the array. And added a simple loop to print any number of lines. Just ensure that the line
        { "", "", -1 }

is always the last one of the set.
jonathan legends 5-Nov-22 14:06pm    
Owhh that is a final line of the array? I just know that. Thank u so much man... Its done. God bless 😇🙏
One small thing, to continue with what Richard did :
C++
struct kelompok kel[] = {
    { "P001", "Alvionina", 18 },
    { "P002", "Stevanny",  18 },
    { "P003", "Joanita",   18 },
    { "P004", "Revan",     18 },
    { "", "", -1 }
};
and instead of padding the names with spaces, change the print method in kelompok to specify the width in the format string :
C++
void print()
{
    printf( "%s     | %9s | %d\n", idpegawai, nama, umur );
}
I prefer to avoid printing tabs because they can be problematic.
 
Share this answer
 

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