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

برنامج لدخال معلومات عن شخص ورقم تلفونه

مغلق
بدأه Mrs.n في 18 أكتوبر 2004 · 14 رد · 800 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

كيفكم

هذا برنامج عن ادخل معلومات عن شخص وتطلب من الشخص كم رقم تلفون عنده

ودخل رقم التلفون على انه string وتضع بين ارقمين المدخله سبيس

ومن بعدها تستخدم الدوال الجاهزه لفصل ارقمين وتحويلها الى integer الارقام هذا كله عادي

وعملته

المشكله اللي صادفتني هي func tion read

اني ادخل رقم الشخص

واسمه

وعدد التلفونات

ورقم تلفونه

عملتها بس المشكله في الفيجول يقلي مايصير اعمل

cin وبعده gets

تشتغل معي شاشة الرن بس الاشياء اللي ادخالتها ب gets ماتدخل لانها قبلها cin

عرفت متغير من نوع char trash[2]

وعملت له gets قبل كل لثفس ايغى ادخلها من اول وبرضو مو راضي يزبط

فيااااااااااااااريت تساعدوني

لانه الواجب لازم اسلمه اليوم وذي المشكله اللي صادفتني لاني شغلت البرنامج على البورلند بيشتغل

وهو مطلوب منا نعمل البرنامج على فيجول سي بلس

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

#include <ctype.h>
#include <conio.h>
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
  ///////////////////////////////
struct customer
{
 int acc_no;
    char name[30];
    int no_phones;
    int* phones;
};
////////////////////////////////////
void read(customer* arr[10],int&n);
void token(customer* arr,int n,char ph[50]);
void disp(customer* arr[10],int n);
void del(customer* arr[10],int&n);
customer* search(customer* x,int t2);
  /////////////////////////////////////////
void main()
{
 int ch,n=0;
    customer* arr[10];
	cout<<"\n 1-Read customer";
     cout<<"\n 2-Display customer";
     cout<<"\n 2-Delete All Customer array ";
     cout<<"\n 4-Exit";
     
   do
   {
    cout<<"\nEnter number  choes:";
        cin>>ch;
     
     switch(ch)
  {
        case 1: read(arr,n);
               break;
        case 2:disp(arr,n);
              break;
        case 3: del(arr,n);break; //put this call in exit case. Our program has 3 cases only and the delete is done when exiting.
        case 4:cout<<"\n Exit";
  }
   }while(ch!=4); 
}
  /////////////////////////////////////////////////
void del(customer *arr[10],int &n)
{ 
 for(int i=0;i<n;i++)                            //for loop to get each customer
 { //for(int j=0;j<arr->no_phones;j++)        //for loop to get each phone no
       delete arr->phones;                     //del the phone no
       delete arr;                          //del the array of customer
 }   
    cout<<"\The array of customer is deleted";
}
  //////////////////////////////////////////////
void read(customer* arr[10],int&n)
{

    char ph[50],trash[2];                                     //define string to read the phone no

        arr[n]=new customer;                           //create new customer
       cout<<"\n enter account number";
       cin>>arr[n]->acc_no;                          //read the customer account
       cout<<"\n enter name";
        cin>>arr[n]->name;                           //read the customer name
       cout<<"\n enter how many phone numbers ";
        cin>>arr[n]->no_phones;                     //read the customer number
       cout<<"\n enter number of phones ";
       gets(ph);                                 //read the customer phones number
       token(arr[n],n,ph);                      //call token fn to break the string of phone numbers
                                             //to different integer
       n++;                                    //add 1 to the customer counter

       
 }
  //////////////////////////////////////////////////////
void token(customer* x,int n,char ph[50])
{
 char* y;int i=0;                      //define a char pointer and i counter
    y=strtok(ph," ");                     //start the fn strtok to break the string if there is spaces

    while(y!=NULL)
 {                                //wihle the string of numbers y not NULL
      n=atoi(y);                     //convert it ti integer n using atoi
      y=strtok(NULL," ");
      x->phones=n;              //put n in the phone integer pointer of the customer
      i++;                             //increment i
 }
}
  /////////////////////////////////////////////////
customer* search(customer* x,int t2)
{
  for(int h=0;h<x->no_phones;h++)       //loop for all phone no for each customer
    if(x->phones[h]==t2)
       return x;
  return NULL;
}
   /////////////////////////////////////////////////
void disp(customer* arr[10],int n)
{
 customer *x;

    int t2;
    cout<<"\n Enter phone number to search";
    cin>>t2;                                //enter phone no to display the persone
   for(int i=0;i<n;i++)                       //loop for all customers
   {
     x=search(arr,t2);
     if(x!=NULL)                           //check if any of the phone numbers is equal to the required phone
  {
  cout<<"\naccount number";           //display its components
        cout<<x->acc_no;
        cout<<"\nname: ";
  cout<<x->name;
        cout<<"\nno of phones ";
  cout<<x->no_phones;
        for(int h=0;h<x->no_phones;h++)          //dispaly all the customer phone numbers
  
   cout<<"\nphone# "<<x->phones[h]; 

  }
   }                           
}
///////////////////////////////////////////////////
#2

ممكن احد يساعدني

#3

بليز ساعدوني

#4

حسنا تم حل المشكلة ,,

أحيانا جملة ال cin يكون فيها باقي مدخلات سابقة في داريء الدخل Input Buffer ,, لذا يجب أن ننظفه أولا قبل طلب نوع جديد من البيانات من المستخدم ,, ويتم ذلك باستخدام جملة

cin.ignore();

خذي الكود المعدل ,, وكان فيه خطأ أيضا آخر يتعلق بعدم حجز الذاكرة للمتغير phones من البنيى custumer ,, حيث هي عبارة عن متغير Pointer داخل بمتغير Pointer وهو عندك ,,

arr[n]

المهم أضفت السطر

{code]

arr[n]->phones = new int[arr[n]->no_phones];

لاحظي اليه وجربي حذفه وجربي البرنامج وستفهمين كل شيء :)

اليكي الكود :

#include <ctype.h>
#include <conio.h>
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 ///////////////////////////////
struct customer
{
int acc_no;
   char name[30];
   int no_phones;
   int* phones;
};
////////////////////////////////////
void read(customer* arr[10],int&n);
void token(customer* arr,int n,char ph[50]);
void disp(customer* arr[10],int n);
void del(customer* arr[10],int&n);
customer* search(customer* x,int t2);
 /////////////////////////////////////////
void main()
{
int ch,n=0;
   customer* arr[10];
cout<<"\n 1-Read customer";
    cout<<"\n 2-Display customer";
    cout<<"\n 2-Delete All Customer array ";
    cout<<"\n 4-Exit";
    
  do
  {
   cout<<"\nEnter number  choes:";
       cin>>ch;
    
    switch(ch)
 {
       case 1: read(arr,n);
              break;
       case 2:disp(arr,n);
             break;
       case 3: del(arr,n);break; //put this call in exit case. Our program has 3 cases only and the delete is done when exiting.
       case 4:cout<<"\n Exit";
 }
  }while(ch!=4); 
}
 /////////////////////////////////////////////////
void del(customer *arr[10],int &n)
{ 
for(int i=0;i<n;i++)                            //for loop to get each customer
{ //for(int j=0;j<arr->no_phones;j++)        //for loop to get each phone no
      delete arr->phones;                     //del the phone no
      delete arr;                          //del the array of customer
}   
   cout<<"\The array of customer is deleted";
}
 //////////////////////////////////////////////
void read(customer* arr[10],int&n)
{

   char ph[50],trash[2];                                     //define string to read the phone no

       arr[n]=new customer;                           //create new customer
      cout<<"\n enter account number";
      cin>>arr[n]->acc_no;                          //read the customer account
      cout<<"\n enter name";
       cin>>arr[n]->name;                           //read the customer name
      cout<<"\n enter how many phone numbers ";
       cin>>arr[n]->no_phones;                     //read the customer number
    arr[n]->phones = new int[arr[n]->no_phones];
      cout<<"\n enter number of phones ";
   cin.ignore();
      gets(ph);                                 //read the customer phones number
   //cin>>ph;
      token(arr[n],n,ph);                      //call token fn to break the string of phone numbers
                                            //to different integer
      n++;                                    //add 1 to the customer counter

      
}
 //////////////////////////////////////////////////////
void token(customer* x,int n,char ph[50])
{
char* y;int i=0;                      //define a char pointer and i counter
   y=strtok(ph," ");                     //start the fn strtok to break the string if there is spaces

   while(y!=NULL)
{                                //wihle the string of numbers y not NULL
     n=atoi(y);                     //convert it ti integer n using atoi
     y=strtok(NULL," ");
     x->phones=n;              //put n in the phone integer pointer of the customer
     i++;                             //increment i
}
}
 /////////////////////////////////////////////////
customer* search(customer* x,int t2)
{
 for(int h=0;h<x->no_phones;h++)       //loop for all phone no for each customer
   if(x->phones[h]==t2)
      return x;
 return NULL;
}
  /////////////////////////////////////////////////
void disp(customer* arr[10],int n)
{
customer *x;

   int t2;
   cout<<"\n Enter phone number to search";
   cin>>t2;                                //enter phone no to display the persone
  for(int i=0;i<n;i++)                       //loop for all customers
  {
    x=search(arr,t2);
    if(x!=NULL)                           //check if any of the phone numbers is equal to the required phone
 {
 cout<<"\naccount number";           //display its components
       cout<<x->acc_no;
       cout<<"\nname: ";
 cout<<x->name;
       cout<<"\nno of phones ";
 cout<<x->no_phones;
       for(int h=0;h<x->no_phones;h++)          //dispaly all the customer phone numbers

  cout<<"\nphone# "<<x->phones[h]; 

 }
  }                           
}
/////

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#5

المشكله نفسها ما تعدلت انا مشكلتي في ادخال رقم التلفون

ما يدخل لو عملت run يخرج للي المسج بس مابيخلني اكتبه

يعني المشكله في

gets(ph);                                 //read the customer phones number

وهنا تدخل ارقمين ويخلي بينها سبيس بعدين احوله من استرينق الى انتقر

بس المشكله مابي يدخلها

عندي معلومه فقيره جدا انه الفيجول ما بيقبل احيان ادخال عن طريق cin وبعده gets

وفي طريقه للمعالجة هالشي بس المشكله ما اعرف هالطريقه

برنامجي عالبورلند بيشتغل بس الحل مطلوب على الفيجول

#6

السلام عليكم ,,

كيف لايعمل ؟!!

يعني لايصل التنفيذ لجملة gets أصلا ؟

لأنه يصل لدي هنا باستخدام مصرف Visual Studio 2003 ,,

أنا في الانتظار ,

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#7

يس

هذا اللي احاول اوصله

يعني مايصل لها مره؟؟

وما اعرف كيف اعالج هالمشكله

قريت

انه لازم نعرف

char trash [2]

نعمل لها

gets(trash);

بعد جملة cin

وتكون بعدها

gets اللي بدخلها

وما عرفت اطبقه لانه ماتشتغل معي واعاني من نفس المشكله

#8

غريبة البرنامج يصل لدي حتى أكثر من المرحلة التي وصل اليها عندك ,, انظر الصورة :

Console3.jpg

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#9

المشكله عندي في include

لاني لما غيرة شوي فيها بيشتغل

بس برضو بيوقف عندي البرنامج بعد ما بدخل

يعني المشكله كلها في included

ممكن include

لاني مو شايفتها في الصوره

#10
#include <ctype.h>
#include <conio.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using std::cin;
using std::cout;
using std::endl;

هذي بعد ماعدلتها بس صار يقراء عادي بس مايرجعني لقائمة menu

#11
[CODE


//hw2


#include <ctype.h>
#include <conio.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
using std::cin;
using std::cout;
using std::endl;

///////////////////////////////
struct customer
{
int acc_no;
char name[30];
int no_phones;
int* phones;

};
////////////////////////////////////
void read(customer* arr[10],int &n);
void token(customer* arr,int n,char ph[50]);
void disp(customer* arr[10],int n);
void del(customer* arr[10],int&n);
customer* search(customer* x,int t2) ;

/////////////////////////////////////////
void main()
{
int ch,n=0;
customer* arr[10];
do
{

cout<<endl<<" 1-Read customer";
cout<<endl<<" 2-Display customer";
cout<<endl<<" 2-Delete All Customer array ";
cout<<endl<<" 4-Exit";
cout<<endl<<"Enter number choes:";
cin>>ch;

switch(ch)
{
case 1: read(arr,n);
break;
case 2:disp(arr,n);
break;
case 3: del(arr,n);break;
case 4:cout<<"\n Exit";
}

}while(ch!=4);
}
/////////////////////////////////////////////////

void del(customer *arr[10],int &n)
{
for(int i=0;i<n;i++) //for loop to get each customer
{ //for(int j=0;j<arr->no_phones;j++) //for loop to get each phone no
delete arr->phones; //del the phone no
delete arr; //del the array of customer
}

cout<<endl<<"The array of customer is deleted";
}

//////////////////////////////////////////////
void read(customer* arr[10],int &n)
{

char ph[50]; //define string to read the phone no
char trash[2];
arr[n]=new customer; //create new customer
cout<<endl<<" enter account number:";
cin>>arr[n]->acc_no; //read the customer account
gets(trash);
cout<<"\n enter name:";
gets(arr[n]->name); //read the customer name
cout<<endl<<" enter how many phone numbers :";
cin>>arr[n]->no_phones; //read the customer number
gets(trash);
cout<<endl<<" enter number of phones: ";
gets(ph); //read the customer phones number
token(arr[n],n,ph); //call token fn to break the string of phone numbers
//to different integer
n++; //add 1 to the customer counter


}
//////////////////////////////////////////////////////

void token(customer* x,int n,char ph[50])
{
char* y;
int i=0; //define a char pointer and i counter
y=strtok(ph," "); //start the fn strtok to break the string if there is spaces
while(y!=NULL)
{ //wihle the string of numbers y not NULL
n=atoi(y); //convert it ti integer n using atoi
y=strtok(NULL," ") ;
x->phones=n; //put n in the phone integer pointer of the customer
i++; //increment i
}
}
/////////////////////////////////////////////////
customer* search(customer* x,int t2)
{

for(int h=0;h<x->no_phones;h++) //loop for all phone no for each customer

if(x->phones[h]==t2)
return x;
return NULL;




}

/////////////////////////////////////////////////
void disp(customer* arr[10],int n)
{
customer *x;
int t2;
cout<<"\n Enter phone number to search";
cin>>t2; //enter phone no to display the persone
for(int i=0;i<n;i++) //loop for all customers
{
x=search(arr,t2);
if(x!=NULL) //check if any of the phone numbers is equal to the required phone
{
cout<<endl<<"account number:"; //display its components
cout<<x->acc_no;
cout<<endl<<"name: ";
cout<<x->name;
cout<<endl<<"no of phones: ";
cout<<x->no_phones;
for(int h=0;h<x->no_phones;h++) //dispaly all the customer phone numbers

cout<<endl<<"phone# "<<x->phones[h];


}
}
}
//////////////////////////////////////////////////////

]

هذا هوه البرنامج حليت مشكله وقفت معي مشكله ثانيه

واتوقع انه من include

#12

حسنا حاول في الموضوع وسأعود لاحقا اليوم مرة أخرى ,,

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#13

مشكوووووووووووووووووووووووووووووور

خلاص زبط البرنامج

وشكرا على مساعدتك

#include <ctype.h>
//#include <conio.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
using std::cin;
using std::cout;
using std::endl;

  ///////////////////////////////
struct customer
{
	int acc_no;
    char name[30];
    int no_phones;
    int* phones;

};
////////////////////////////////////
void read(customer* arr[10],int &n);
void token(customer* arr,int n,char ph[50]);
void disp(customer* arr[10],int n);
void del(customer* arr[10],int&n);
customer* search(customer* x,int t2);

  /////////////////////////////////////////
void main()
{
	int ch,n=0;
    customer* arr[10];
  cout<<endl<<" 1-Read customer";
     cout<<endl<<" 2-Display customer";
     
     cout<<endl<<" 3-Exit";
   do
   {

     cout<<endl<<" 1-Read customer";
     cout<<endl<<" 2-Display customer";
     
     cout<<endl<<" 3-Exit";
     cout<<endl<<"Enter number  choes:";
     cin>>ch;

     switch(ch)
  {
        case 1: read(arr,n);
               break;
        case 2:disp(arr,n);
              break;
        case 3:cout<<"\n Exit"; 
 	 del(arr,n);break;
        
  }

   }while(ch!=3); 
}
  /////////////////////////////////////////////////

void del(customer *arr[10],int &n)
{ 
	for(int i=0;i<n;i++)                            //for loop to get each customer
	{ //for(int j=0;j<arr->no_phones;j++)        //for loop to get each phone no
       delete arr->phones;                     //del the phone no
       delete arr;                          //del the array of customer
	}   

    cout<<endl<<"The array of customer is deleted";
}

  //////////////////////////////////////////////
void read(customer* arr[10],int &n)
{

    char ph[50];                //define string to read the phone no
    char trash[2];
       arr[n]=new customer;                           //create new customer
       cout<<endl<<" enter account number:";
       cin>>arr[n]->acc_no;                         //read the customer account
    gets(trash);
       cout<<"\n enter name:";
    gets(arr[n]->name);                           //read the customer name
       cout<<endl<<" enter how many phone numbers :";
    cin>>arr[n]->no_phones;                         //read the customer number
   arr[n]->phones = new int[arr[n]->no_phones];

    gets(trash);
       cout<<endl<<" enter number of phones: ";
       gets(ph);                                 //read the customer phones number
       token(arr[n],n,ph);                      //call token fn to break the string of phone numbers
                                             //to different integer
       n++;                                    //add 1 to the customer counter
                    
  
 }
  //////////////////////////////////////////////////////

void token(customer* x,int n,char ph[50])
{
	char* y;
	int i=0;                              //define a char pointer and i counter
    y=strtok(ph," ");                     //start the fn strtok to break the string if there is spaces
    while(y!=NULL)
	{                                //wihle the string of numbers y not NULL
      n=atoi(y);                     //convert it ti integer n using atoi
      y=strtok(NULL," ");
      x->phones=n;              //put n in the phone integer pointer of the customer
      i++;                         //increment i
	}
}
  /////////////////////////////////////////////////
customer* search(customer* x,int t2)
{
  
     for(int h=0;h<x->no_phones;h++)       //loop for all phone no for each customer
  
       if(x->phones[h]==t2)
          return x;
       return NULL;
  
   
   

}

   /////////////////////////////////////////////////
void disp(customer* arr[10],int n)
{
     customer *x;
    int t2;
    cout<<"\n Enter phone number to search";
    cin>>t2;                                //enter phone no to display the persone
   for(int i=0;i<n;i++)                       //loop for all customers
   {
     x=search(arr,t2);
     if(x!=NULL)                           //check if any of the phone numbers is equal to the required phone
  {
  cout<<endl<<"account number:";           //display its components
        cout<<x->acc_no;
        cout<<endl<<"name: ";
  cout<<x->name;
        cout<<endl<<"no of phones: ";
  cout<<x->no_phones;
        for(int h=0;h<x->no_phones;h++)          //dispaly all the customer phone numbers
  
 	 cout<<endl<<"phone# "<<x->phones[h]; 


  }
   }                           
}
/////////////////////////////////
#14

حمد لله على السلامة :)

banner_60_468.gif

NOTHING IS IMPOSSIBLE

#15

كانت المشكله اني نسيت اعمل

new اللي انت اضفتها للي

والحمدالله اضفتها ومشى معي البرنامج

وطبعا بديت ازبط فيه واحسن من شكل البرنامج

والله يوفقك دنيا واخره على المساعده

هذا الموضوع مغلق.

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…