חייב עזרה ב c++

עידן31

New member
חייב עזרה ב c++

אני עובר קורס ב c++ ועד פה הכל היה בסדר. הגענו לחלק של המחלקות והתבניות ואני פשוט לא מצליח להריץ את זה בסטודיו של c++. המטלה הייתה: לממש תוכנית הבאה: לבנק מזרחי 34 סעיפים ברחבי הארץ. לכל בנק נשמרים הנתונים הבאים: עיר,רחוב,רישמה של שמות העובדים,רשימה של הלקוחות. לכל עובד ישמרו הנתונים: שם,גיל לכל לקוח ישמרו: מס' תעודת זהות, יתרה, שם אם מישהו יכול בבקשה לעשות את זה בקובץ ולהשים פה כדי שאוכל לראות מה הטעות שלי. דבר נוסף- המרצה שלנו אמר שבפרק הבא נלמד כיצד להפוך את התבניות למסדי נתונים כלומר שנוכל להזין נתונים ולהשתמש בהם גם אחר חודש או חודשיים וכל יום להוסיף נתונים. סתם מתוך סקרנות איך עושים את זה?
 

אלדד27

New member
בוא נעשה הפוך,

אתה תעלה לכאן את מה שעשית ואנחנו נספר לך מה לא בסדר. הולך?
 

עידן31

New member
הנה התוכנית בבקשה תעזרו

# include <iostream> #include <list> #include <string> using namespace std; struct worker { string name; float age; } struct client { string id; float left; string name; } struct bank { string street; string city; list<client> costumers; } mizrahi=bank[34]; int main() { mizrahi b; int i; for (i=0,i<34,i++) { cout<<"please insert bank's street"; cin>>b.street; cout<<"please insert bank's city"; cin>>b.city; //we are not goinig to initizalize the list for now becasue i just want to check if it works// } for (i=0,i<34,i++) { cout<<b.street; cout<<b.city; } return 0; }
 

עידן31

New member
עכשיו תבינו יותר טוב

# include <iostream> #include <list> #include <string> using namespace std; struct worker { string name; float age; } struct client { string id; float left; string name; } struct bank { string street; string city; list<client> costumers; } mizrahi=bank[34]; int main() { mizrahi b; int i; for (i=0,i<34,i++) { cout<<"please insert bank's street"; cin>>b.street; cout<<"please insert bank's city"; cin>>b.city; //we are not goinig to initizalize the list for now becasue i just want to check if it works// } for (i=0,i<34,i++) { cout<<b.street; cout<<b.city; } return 0; }
 

ברנדל

New member
אז ככה..

דבר ראשון תלמד: לפני שאתה רושם קוד תעבור שורה, תלחץ על "תחילת קוד" ובגמר הקוד תרשום "סיום קוד". ועכשיו נעבור לקוד עצמו: איזו מן הגדרה זו mizrahi b להזכירך mizrahi זה לא אחד מהמבנים שקיימים אצלך ולכן אין לך שום type בשם mizrahi. לעומת זה, כן הגדרת מבנה בשם bank. ולכן יש להגדיר: bank b
 

ברנדל

New member
יתרה מזאת

מה זה בכלל mizrahi, אתה לא יכול להחליט שסתם שם שאין לו שום type יהיה שווה למשהו אחר. אתה חייב קודם להגדיר את ה type של mizrahi. חוץ מזה שאין לך בכלל צורך בכך. לא ב mizrahi ולא ב b. וזה הזמן להציג את הקוד שרשמת בלי שגיאות תחביר:
# include <iostream> #include <list> #include <string> using namespace std; struct worker { string name; float age; } struct client { string id; float left; string name; } struct bank { string street; string city; list<client> costumers; } //mizrahi=bank[34]; error there is no type to the name mizrahi bank mizrahi[34]; //the correct way to asign array of 34 elements int main() { //mizrahi b; error there is no type name mizrahi for (int i=0,i != 34, ++i) { cout<<"please insert bank's street"; //cin>>b.street; error cin>>mizrahi.street cout<<"please insert bank's city"; cin>>mizrahi.city; //we are not goinig to initizalize the list for now becasue i just want to check if it works// } for (i=0,i!=34,i++) { cout<<mizrahi.street; cout<<mizrahi.city; } return 0; }​
 

ברנדל

New member
אה כן..

גם את לולאת ה for רשמת לא נכון (עכשיו כשקראתי את התגובה שלי שמתי לב) התחביר של for משתשמש ב ; ולא ב , כלומר יש לרשום
for (int i = 0 ; i != 34 ; i++) and not for (int i = 0 , i != 34 , i++)​
 

יוסי311

New member
הוא רושם שיש לך טעויות

c:\windows\b.cpp(9) : error C2236: unexpected 'struct' 'client' c:\windows\b.cpp(9) : error C2143: syntax error : missing ';' before '{' c:\windows\b.cpp(9) : error C2447: missing function header (old-style formal list?) c:\windows\b.cpp(17) : error C2065: 'client' : undeclared identifier c:\windows\b.cpp(17) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' c:\windows\b.cpp(17) : fatal error C1903: unable to recover from previous error(s); stopping compilation Error executing cl.exe.
 

ברנדל

New member
בסדר

לא עקבתי לחלוטין אחרי הכל וגם לא הרצתי את זה. לדוגמא יש טעות בכך שיש צורך ב ; אחרי כל struct. המטרה שלי היתה להראות לו דברים בסיסיים שהוא לא עושה נכון בשפה.
 

עידן31

New member
עדיין לא פועל לי

אם תוכל בבקשה (ברנדל) לרשום את כל התוכנית באופן תקין אודה לך מאוד.
 

ברנדל

New member
בכיף שלנו

שוכתב וקומפל
# include <iostream> #include <list> #include <string> using namespace std; struct worker { string name; float age; }; struct client { string id; float left; string name; }; struct bank { string street; string city; list<client> costumers; }; bank mizrahi[34]; int main() { for (int i=0;i != 34; ++i) { cout<<"please insert bank "<<i+1<<" street"<<endl; cin>>mizrahi.street; cout<<"please insert bank's city"<<endl; cin>>mizrahi.city; } for (i=0;i!=34;i++) { cout<<mizrahi.street<<endl; cout<<mizrahi.city<<endl<<endl; } return 0; }​
 

matliv

New member
אני תופס טרמפ על השאלה

כתבתי את זה ככה ואני אשמח לשמוע הערות (דרך אגב, ברנדל, חסר לך int ב-for האחרון). כמו כן, האם יש דרך לממש את שני ה-struct מחוץ ל-class, או שאולי עדיף לשים אותם בכלל מחוץ ל-class?
#include <iostream> using namespace std; class mizrahi { private: char city[50]; char street[50]; struct ovdim { char name[50]; int age; }oved[3]; struct lakohot { char name[50]; float left; char id[11]; }lakoah[5]; public: void set_city(); void set_street(); char* get_city(); char* get_street(); void set_ovdim(); void get_ovdim(); void set_lakohot(); void get_lakohot(); }; void mizrahi::set_city() { cout<<"\nPlease insert the city of the branch\n"; cin.getline(city,50); } void mizrahi::set_street() { cout<<"\nPlease insert the street\n"; cin.getline(street,50); } char* mizrahi::get_city() { cout<<"\n\n\t\tBranch: "; return(city); } char* mizrahi::get_street() { cout<<"\nThe street of the branch is "; return(street); } void mizrahi::set_ovdim() { int onum=3; char buffer[10]; for(int i=0; i<onum; i++) { cout<<"Please insert the name of worker #"<<(i+1)<<"\n"; cin.getline(oved.name,50); cout<<"\nPlease insert the age of the worker\n"; cin.getline(buffer,10); oved.age = atoi(buffer); } } void mizrahi::get_ovdim() { int onum=3; for(int i=0; i<onum; i++) { cout<<"\nThe name of worker "<<(i+1)<<" is "<<oved.name; cout<<"\nhis age is "<<oved.age; } } void mizrahi::set_lakohot() { int lnum=5; char buffer[10]; for(int i=0; i<lnum; i++) { cout<<"Please insert the name of client #"<<(i+1)<<"\n"; cin.getline(lakoah.name,50); cout<<"\nPlease insert the amount of money he has left\n"; cin.getline(buffer,20); lakoah.left = atof(buffer); cout<<"\nPlease insert the client's id\n"; cin.getline(lakoah.id,11); } } void mizrahi::get_lakohot() { int lnum=5; for(int i=0; i<lnum; i++) { cout<<"\nThe name of client "<<(i+1)<<" is "<<oved.name; cout<<"\nmoney left in his account: "<<lakoah.left; cout<<"\nhis id is "<<lakoah.id; } } int main() { mizrahi branch[2]; int m=2; for(int n=0; n<m; n++) { branch[n].set_city(); branch[n].set_street(); branch[n].set_ovdim(); branch[n].set_lakohot(); } for(int n=0; n<m; n++) { cout<<branch[n].get_city(); cout<<endl<<branch[n].get_street(); cout<<"\n\tList of workers in this branch:\n"; branch[n].get_ovdim(); cout<<"\n\tList of clients in this branch:\n"; branch[n].get_lakohot(); cout<<endl; } return(0); }​

ותודה למי שקרא עד לפה!
 

ברנדל

New member
אתה מתחצף

לא חסר שום int. הואיל וה i כבר הוגדר ב for לפני אז הוא כבר מוכר ב for אחרי ולא צריך להגדיר אותו שוב כ int.
 

יוסי311

New member
תודה רבה, יש לי עוד שאלה קטנה

ל maltiv התוכנית שלך לא תואמת את השאלה ביקשו 34 סניפים ואני לא רואה איפה ייצגת את זה. דרך אגב אולי בבקשה תסבירו לי על מחלקות תבניות והורשות אודה לכם מאוד
 

matliv

New member
קבל עדכון

שמתי 2 סניפים כדי שההרצה לא תיקח שעה אבל הנה עם 34 סניפים:
#include <iostream> using namespace std; class mizrahi { private: char city[50]; char street[50]; struct ovdim { char name[50]; int age; }oved[3]; struct lakohot { char name[50]; float left; char id[11]; }lakoah[5]; public: void set_city(); void set_street(); char* get_city(); char* get_street(); void set_ovdim(); void get_ovdim(); void set_lakohot(); void get_lakohot(); }; void mizrahi::set_city() { cout<<"\nPlease insert the city of the branch\n"; cin.getline(city,50); } void mizrahi::set_street() { cout<<"\nPlease insert the street\n"; cin.getline(street,50); } char* mizrahi::get_city() { cout<<"\n\n\t\tBranch: "; return(city); } char* mizrahi::get_street() { cout<<"\nThe street of the branch is "; return(street); } void mizrahi::set_ovdim() { int onum=3; char buffer[10]; for(int i=0; i<onum; i++) { cout<<"Please insert the name of worker #"<<(i+1)<<"\n"; cin.getline(oved.name,50); cout<<"\nPlease insert the age of the worker\n"; cin.getline(buffer,10); oved.age = atoi(buffer); } } void mizrahi::get_ovdim() { int onum=3; for(int i=0; i<onum; i++) { cout<<"\nThe name of worker "<<(i+1)<<" is "<<oved.name; cout<<"\nhis age is "<<oved.age; } } void mizrahi::set_lakohot() { int lnum=5; char buffer[10]; for(int i=0; i<lnum; i++) { cout<<"Please insert the name of client #"<<(i+1)<<"\n"; cin.getline(lakoah.name,50); cout<<"\nPlease insert the amount of money he has left\n"; cin.getline(buffer,20); lakoah.left = atof(buffer); cout<<"\nPlease insert the client's id\n"; cin.getline(lakoah.id,11); } } void mizrahi::get_lakohot() { int lnum=5; for(int i=0; i<lnum; i++) { cout<<"\nThe name of client "<<(i+1)<<" is "<<oved.name; cout<<"\nmoney left in his account: "<<lakoah.left; cout<<"\nhis id is "<<lakoah.id; } } int main() { mizrahi branch[34]; int m=34; for(int n=0; n<m; n++) { branch[n].set_city(); branch[n].set_street(); branch[n].set_ovdim(); branch[n].set_lakohot(); } for(int n=0; n<m; n++) { cout<<branch[n].get_city(); cout<<endl<<branch[n].get_street(); cout<<"\n\tList of workers in this branch:\n"; branch[n].get_ovdim(); cout<<"\n\tList of clients in this branch:\n"; branch[n].get_lakohot(); cout<<endl; } return(0); }​
 
למעלה