الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة اكواد

بدأه NizarYamani في 15 ديسمبر 2012 · 5 رد · 654 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

ابي اعرف وين الخطأ بالتحديد ..

وجزاكم الله الخير ..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


	typedef struct mycandidate{
	    char name[20];
	    int votes;
	    float percentage;
	} candidate;

	void get_candidate(candidate list[]);
	float count_percentage(candidate list[],int);
	void print_result(candidate list[],int,int);
	int main(){
	    candidate list[50];
	    get_candidate(list);
	    return 0;
	}
	void get_candidate(candidate list[]){
	    int i,num,numCandidates=0;
	    printf("Enter the Number of Candidates: ");
	    scanf("%d",&num);
	    for (i=0;i<num;i++){
	        printf("Candidate %d\n",i+1);
	        printf("Enter the name: \n");
	        scanf("%s",list.name);      //remove the & from in front of "list.name"
	        if (strcmp("Done",list.name) == 0 || strcmp("Finish",list.name) == 0)
	            {break;}
	        printf("Enter the number of votes:\n");
	        scanf("%d",&list.votes);
	        printf("\n");
	    }
		numCandidates =list.votes; 
		count_percentage(list,numCandidates);
	}

	float count_percentage(candidate list[],int numCandidates){
		float total=0.0;
	    float temp=0.0;	   
	    for (int i=0;i<numCandidates;i++){
	        temp = temp + (list.votes);
	    }
	    for(int i=0;i<numCandidates;i++){
	        list.percentage = ((list.votes)/temp) * 100.0;
	    }
		  print_result(list,numCandidates,temp);
		return (0);
	}


	void print_result(candidate list[],int numCandidates,int temp,float total){
		int highest;
	    highest = 0;
	    for (int i=0;i<numCandidates;i++){
	        if (list.votes > highest){
	            highest = list.votes;
	        } 
		}
	    printf("\n%-12s %-12s %-12s\n", "Candidate", "Votes Received", "% of Total Votes");
	    for (int i=0;i<numCandidates;i++)
			{printf("%-12s %-12d %-12.2f\n",list.name,list.votes,list.percentage);}
	    printf("%-12s %-12d\n", "Total", temp);
	    printf("\nThe Winner of the Election is %s\n",list[highest].name);
	}
#2

هل تقصد أخطاء تحدث أثناء ترجمة الكود (Compilation errors) أم أخطاء وقت التشغيل (Runtime Errors) ؟

ثم مالهدف من الكود ؟

#3
اقتباس
هل تقصد أخطاء تحدث أثناء ترجمة الكود (Compilation errors) أم أخطاء وقت التشغيل (Runtime Errors) ؟

إنه يقصد خطأ أثناء الربط LINK :

اقتباس
error LNK2001: unresolved external symbol "void __cdecl print_result(struct candidate * const,int,int)" (?print_result@@YAXQAUcandidate@@HH@Z)

أخي الكريم .. هذا الخطأ يصدره المترجم أثناء عملية الربط عندما لا يتوفر جسم للتابع المستدعى ...

وسبب خطأك أنك استدعيت التابع print_result قبل أن تكتب جسمه .أو حتى prototype

خطؤك هنا :

    	float count_percentage(candidate list[],int numCandidates){
            	float total=0.0;
        	float temp=0.0;    	
        	for (int i=0;i<numCandidates;i++){
            	temp = temp + (list.votes);
        	}
        	for(int j=0;j<numCandidates;j++){
            	list[j].percentage = ((list[j].votes)/temp) * 100.0;
        	}
              	print_result(list,numCandidates,temp);
            	return (0);
    	}

قمت باستدعاء هذا التابع :

	print_result(list,numCandidates,temp);

فقام المترجم باستدعاء الprototype المكتوب أول الكود :

    	void print_result(candidate list[],int,int);

ثم حاول المترجم البحث عن جسم هذا التابع ..فلم يجد !!!

ستسألني : لماذا هذا هو التابع :

    	void print_result(candidate list[],int numCandidates,int temp,float total){
            	int highest;
        	highest = 0;
        	for (int i=0;i<numCandidates;i++){
            	if (list.votes > highest){
                	highest = list.votes;
            	} 
            	}
        	printf("\n%-12s %-12s %-12s\n", "Candidate", "Votes Received", "% of Total Votes");
        	for (int i0=0;i0<numCandidates;i0++)
                    	{printf("%-12s %-12d %-12.2f\n",list[i0].name,list[i0].votes,list[i0].percentage);}
        	printf("%-12s %-12d\n", "Total", temp);
        	printf("\nThe Winner of the Election is %s\n",list[highest].name);
    	}

وسأقول لك : لا يا عزيزي ...التابع الذي كتبته يستقبل 4 وسطاء ... أما التابع المطلوب يستقبل 3 وسطاء ..

وبذلك لم يتعرف المتجم على تابعك ...

ولحل المشاكلة .. اكتب التابع ب3 وسطاء فقط ...

ولا تنسنا من الدعاء :)

يبدو أن سبب حدوث ذلك .. هو أنك قمت بكتابة الكود على عدة مراحل ...فنسيت إزالة آخر وسيط ...

قمت بإزالته لك .. وهذا هو التابع بعد إزالة آخر وسيط :

    	void print_result(candidate list[],int numCandidates,int temp){
            	int highest;
        	highest = 0;
        	for (int i=0;i<numCandidates;i++){
            	if (list.votes > highest){
                	highest = list.votes;
            	} 
            	}
        	printf("\n%-12s %-12s %-12s\n", "Candidate", "Votes Received", "% of Total Votes");
        	for (int i0=0;i0<numCandidates;i0++)
                    	{printf("%-12s %-12d %-12.2f\n",list[i0].name,list[i0].votes,list[i0].percentage);}
        	printf("%-12s %-12d\n", "Total", temp);
        	printf("\nThe Winner of the Election is %s\n",list[highest].name);
    	}

وهذا كودك .. بعد التصحيح:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


    	typedef struct mycandidate{
        	char name[20];
        	int votes;
        	float percentage;
    	} candidate;

    	void get_candidate(candidate list[]);
    	float count_percentage(candidate list[],int);
    	void print_result(candidate list[],int,int);
    	int main(){
        	candidate list[50];
        	get_candidate(list);
        	return 0;
    	}
    	void get_candidate(candidate list[]){
        	int i,num,numCandidates=0;
        	printf("Enter the Number of Candidates: ");
        	scanf("%d",&num);
        	for (i=0;i<num;i++){
            	printf("Candidate %d\n",i+1);
            	printf("Enter the name: \n");
            	scanf("%s",list.name);  	//remove the & from in front of "list.name"
            	if (strcmp("Done",list.name) == 0 || strcmp("Finish",list.name) == 0)
                	{break;}
            	printf("Enter the number of votes:\n");
            	scanf("%d",&list.votes);
            	printf("\n");
        	}
            	numCandidates =list.votes; 
            	count_percentage(list,numCandidates);
    	}

    	float count_percentage(candidate list[],int numCandidates){
            	float total=0.0;
        	float temp=0.0;    	
        	for (int i=0;i<numCandidates;i++){
            	temp = temp + (list.votes);
        	}
        	for(int i=0;i<numCandidates;i++){
            	list.percentage = ((list.votes)/temp) * 100.0;
        	}
              	print_result(list,numCandidates,temp);
            	return (0);
    	}


    	void print_result(candidate list[],int numCandidates,int temp){
            	int highest;
        	highest = 0;
        	for (int i=0;i<numCandidates;i++){
            	if (list.votes > highest){
                	highest = list.votes;
            	} 
            	}
        	printf("\n%-12s %-12s %-12s\n", "Candidate", "Votes Received", "% of Total Votes");
        	for (int i=0;i<numCandidates;i++)
                    	{printf("%-12s %-12d %-12.2f\n",list.name,list.votes,list.percentage);}
        	printf("%-12s %-12d\n", "Total", temp);
        	printf("\nThe Winner of the Election is %s\n",list[highest].name);
    	}

بالتوفيق

والسلام عليكم ورحمة الله وبركاته

#4

@مصطفى

أنا سألته عن نوع الخطأ لأنني لاحظتُ أن الكود يحتوي على أخطاء تحدث أثناء الترجمة و أخرى تحدث أثناء التنفيذ :)

#5

يا اخي انا عدلته من اول بس المشكله ما قدرت ادخل المنتدى مرره ثانيه مع ان عندي يوزر من قبل اليوزر الاساسي NeeZaaR ولما جيت اسوي استعادة ماجاني شي ..

المشكلة يا الحبيب انه يعطيني النتيجه صفر رغم اني ادخل اسماء المرشحين فما يطلع لي اي مرشح ولا نسبة ولا مجموع الاصوات ..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


	typedef struct mycandidate{
	    char name[20];
	    int votes;
	    float percentage;
	} candidate;

	void get_candidate(candidate list[]);
	float count_percentage(candidate list[],int);
	void print_result(candidate list[],int,int);
	int main(){
	    candidate list[50];
	    get_candidate(list);
	    return 0;
	}
	void get_candidate(candidate list[]){
	    int i=0,num,numCandidates=0;

	    for (i=0;i<10;i++){
	        printf("Candidate %d\n",i+1);
	        printf("Enter the name: \n");
	        scanf("%s",list.name);      //remove the & from in front of "list.name"
	        if (strcmp("Done",list.name) == 0 || strcmp("Finish",list.name) == 0)
	            {break;}
	        printf("Enter the number of votes:\n");
	        scanf("%d",&list.votes);
	        printf("\n");
	    }
		numCandidates =list.votes; 
		count_percentage(list,numCandidates);
	}

	float count_percentage(candidate list[],int numCandidates){
	    float temp=0.0;
		int i=0;
	    for (i=0;i<numCandidates;i++)
		{temp = temp + (list.votes);}

	    for(i=0;i<numCandidates;i++)
		{list.percentage = ((list.votes)/temp) * 100.0;}
		  print_result(list,numCandidates,temp);
		return (0);
	}


	void print_result(candidate list[],int numCandidates,int temp){
		int highest,iplus=0,i=0;
		highest = list[0].votes;
	    for (i=0;i<numCandidates;i++)
		{
			if (list.votes > highest)
				{
					highest = list.votes;
					iplus = i;
				}

		}

	    printf("\n%-12s %-12s %-12s\n", "Candidate", "Votes Received", "% of Total Votes");
	    for (i=0;i<numCandidates;i++)

		{printf("%-12s %-12d %-12.2f\n",list.name,list.votes,list.percentage);}
	    printf("%-12s %-12d\n", "Total", temp);
	    printf("\nThe Winner of the Election is %s\n",list[iplus].name);
	}

NizarYamani1 كتب:

يا اخي انا عدلته من اول بس المشكله ما قدرت ادخل المنتدى مرره ثانيه مع ان عندي يوزر من قبل اليوزر الاساسي NeeZaaR ولما جيت اسوي استعادة ماجاني شي ..

المشكلة يا الحبيب انه يعطيني النتيجه صفر رغم اني ادخل اسماء المرشحين فما يطلع لي اي مرشح ولا نسبة ولا مجموع الاصوات ..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


	typedef struct mycandidate{
	    char name[20];
	    int votes;
	    float percentage;
	} candidate;

	void get_candidate(candidate list[]);
	float count_percentage(candidate list[],int);
	void print_result(candidate list[],int,int);
	int main(){
	    candidate list[50];
	    get_candidate(list);
	    return 0;
	}
	void get_candidate(candidate list[]){
	    int i=0,num,numCandidates=0;

	    for (i=0;i<10;i++){
	        printf("Candidate %d\n",i+1);
	        printf("Enter the name: \n");
	        scanf("%s",list.name);      //remove the & from in front of "list.name"
	        if (strcmp("Done",list.name) == 0 || strcmp("Finish",list.name) == 0)
	            {break;}
	        printf("Enter the number of votes:\n");
	        scanf("%d",&list.votes);
	        printf("\n");
	    }
		numCandidates =list.votes; 
		count_percentage(list,numCandidates);
	}

	float count_percentage(candidate list[],int numCandidates){
	    float temp=0.0;
		int i=0;
	    for (i=0;i<numCandidates;i++)
		{temp = temp + (list.votes);}

	    for(i=0;i<numCandidates;i++)
		{list.percentage = ((list.votes)/temp) * 100.0;}
		  print_result(list,numCandidates,temp);
		return (0);
	}


	void print_result(candidate list[],int numCandidates,int temp){
		int highest,iplus=0,i=0;
		highest = list[0].votes;
	    for (i=0;i<numCandidates;i++)
		{
			if (list.votes > highest)
				{
					highest = list.votes;
					iplus = i;
				}

		}

	    printf("\n%-12s %-12s %-12s\n", "Candidate", "Votes Received", "% of Total Votes");
	    for (i=0;i<numCandidates;i++)

		{printf("%-12s %-12d %-12.2f\n",list.name,list.votes,list.percentage);}
	    printf("%-12s %-12d\n", "Total", temp);
	    printf("\nThe Winner of the Election is %s\n",list[iplus].name);
	}

كانت فيه مشكلة بسبب البرنامج كان فيه تعارض ..

#6

نعم حلت المشكلة ولكن المشكلة الاساسية لم تحل ..

المفروض the ouput will be as this

Candidate Votes Received % of Total Votes

Najib 5000 29.91

Mahathir 4000 20.73

Hadi 6000 31.09

Aziz 2500 12.95

Lim 1800 9.33

Total 19300

The Winner of the Election is Hadi.

بس المشكلة ما يعطيني اي اسم ادخلته ولا المجموع ولا كمية الاصوات لكل واحد

مواضيع مشابهة