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

[ تمت الإجابة ]مشكلة : استخدام كائن داخل حلقة while

بدأه Mohammad Mezo في 14 أكتوبر 2013 · 3 رد · 723 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم و رحمة الله و بركاته و كل عام و الجميع بألف خير 

 

اخواني ارجو منكم المساعدة في حل عطل في هذا البرنامج انا لم اشاهد اي عطل لكن عند اقتران الـ display لا يطبع المعلومات المرادة بل يطبع معلومات عشوائية غير مفهومة 

 

هل من احد يعرف لماذا

و شكرآ 

 

هذا كود البرنامج 

 

-----------------------------------------------------------------------------------

 

 

 

 

#include <iostream>#include<string>using namespace std;//-------------  int top=-1;  int size=2;int incree=-1;//---------------struct st{char name[30]; int id;float mark;};//end struct//--------------class mezo{st* in;public:mezo(){in=new st[2];}void Push(int ID,float Mark,char Name[10]){incree++;if(top>size-1){incre();}in[top].id=ID;in[top].mark=Mark;for (int i=0;i<=10;i++) {in[top].name=Name;}cout << in[top].name << " has been added \n\n";}//end push//-------------void Pop() { system("cls");if(top<0){  cout << "\n-->Stack Underflow\n\n" << endl;    }else{cout << in[top].name << " been deleted \n";top--;}cout << "\n\n--> press any key to return ";system("pause");system("cls");}//end pop//------------bool Empty(){return top <0;}//end empty//------------void incre(){cout << " done Jump ^_^ ";system("pause");size*=2;st* newin = new st [size];for (int i=0;i<=top;i++){newin.id=in.id;newin.mark=in.mark;for (int j=0;j<=10;j++){newin.name[j]=in.name[j];}}delete [] in ;in = newin;}//------------void display(){system("cls");if(Empty())cout <<"--> Stack is Empty \n\n\n" ;else{    cout << " name     mark     id"<<" \n\n ";cout <<"-----|------|----\n";for ( int i=top;i>=0;i-- ){cout<<" ( " << in.name << ", " << in.mark << ", "<<in.id<<" ) \n";}cout << "\n\n--> press any key to return ";system("pause");system("cls");}}//end display//----------void clear(){system("cls");top=-1;cout << "\nAll data has been cleared \n\n"; cout << "\n\n--> press any key to return ";system("pause");system("cls");}};//end class//---------------- void main() {while(1){cout << " Choose one of the choices above (1-5) \n prees 1 to Add student \n press 2 to remove the last student \n press 3 to clear the data \n press 4 to display all information \n press 5 to exit \n  ";int c;cin >>c;mezo s;int id;char name[10];float mark;switch(c){case 1:system("cls");top++;cout << "\n enter the name ["<<"]\n";cin>>name;cout << "\n enter the mark ["<<"]\n";cin >> mark;cout << "\n enter the id  ["<<"]\n\n";     cin >>id;system("cls");s.Push(id,mark,name);break;case 2:s.Pop();break;case 3:s.clear();break;case 4:s.display();break;case 5: exit(0);default:system("cls");main();}//end while}//end switchsystem("pause");} //end main

 

 

 

-------------------------------------------------------------------------------

1
#2

السلام عليكم أخي الكريم :)

ما شاء الله عليك ... أسلوب مرتّب في كتابة الكود ... وعمل متقن :) وفقك الله ...

خطأ البرنامج بسيط جداً ... أكثر مما تتوقع :D

 

1- أين يتم تخزين البيانات ؟ الجواب : داخل الكائن s من نوع mezo

2- أين يتم إنشاء هذا الكائن ؟ الجواب داخل main

3- أين يتم انشاء هذا الكائن داخل main ? الجواب : داخل حلقة ..

4- عندما تريد عرض البيانات ...فهذا سيحدث في إحدى دورات الحلقة أليس كذلك ؟ نعم

5- إذا ... فالكائن الذي تستعدي منه العملية display هو كائن مختلف عن الكائن الذي قمت فيه بإدخال البيانات :)  أليس كذلك ؟ فعلاً !

6- كيف حالك :D

لديك عدة خيارات منها :

1- عرّف الكائن s قبل بداية الحلقة ...

2- اكتب كلمة static قبل mezo وبذلك سيتم التعريف مرة واحدة فقط ..

بالنسبة لأطول سطر في برنامجك يمكنك تقسمه كما يلي :

cout << " Choose one of the choices above (1-5) \n"
"prees 1 to Add student \n"
"press 2 to remove the last student \n"
"press 3 to clear the data \n"
"press 4 to display all information \n"
" press 5 to exit \n  ";

 

بالتوفيق :)

#3

الف شكر على مرورك الراقي و الف شكر لك على هذه المساعدة ولا ادري كيف اشكرك على هذه المساعد شكرآ لك  :wub:

#4
 
 
CTRL + Q to Enable/Disable GoPhoto.it
−1

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