C++ string assignment to struct
-
Hello every one please i have a problem when i try to assign the contain of struct array to another as in the code
bool delet(){ Int f=0; Cout<<"enter name:"; char del\[20\]; Con.getline(del,20); for(int i=0; i<=index; ++i){ If(phone\[i\].name==del){ for(int j=i; j<=index; ++j){ Phone\[j\].num=Phone\[j+1\].num; Phone\[j\].TphoneN=phone\[j+1\].TphoneN; Phone\[j\].name=Phone\[j+1\].name; } f=1; }
}
}<\pre>
-
Hello every one please i have a problem when i try to assign the contain of struct array to another as in the code
bool delet(){ Int f=0; Cout<<"enter name:"; char del\[20\]; Con.getline(del,20); for(int i=0; i<=index; ++i){ If(phone\[i\].name==del){ for(int j=i; j<=index; ++j){ Phone\[j\].num=Phone\[j+1\].num; Phone\[j\].TphoneN=phone\[j+1\].TphoneN; Phone\[j\].name=Phone\[j+1\].name; } f=1; }
}
}<\pre>
-
Hello every one please i have a problem when i try to assign the contain of struct array to another as in the code
bool delet(){ Int f=0; Cout<<"enter name:"; char del\[20\]; Con.getline(del,20); for(int i=0; i<=index; ++i){ If(phone\[i\].name==del){ for(int j=i; j<=index; ++j){ Phone\[j\].num=Phone\[j+1\].num; Phone\[j\].TphoneN=phone\[j+1\].TphoneN; Phone\[j\].name=Phone\[j+1\].name; } f=1; }
}
}<\pre>
It is a pseudo-code? Could you instead post a compilable and properly formatted code snippet? Or, at least, explain what errors or other problems you have with this code?
-
Hello every one please i have a problem when i try to assign the contain of struct array to another as in the code
bool delet(){ Int f=0; Cout<<"enter name:"; char del\[20\]; Con.getline(del,20); for(int i=0; i<=index; ++i){ If(phone\[i\].name==del){ for(int j=i; j<=index; ++j){ Phone\[j\].num=Phone\[j+1\].num; Phone\[j\].TphoneN=phone\[j+1\].TphoneN; Phone\[j\].name=Phone\[j+1\].name; } f=1; }
}
}<\pre>
-
It is a pseudo-code? Could you instead post a compilable and properly formatted code snippet? Or, at least, explain what errors or other problems you have with this code?
no it is not a pseudo code am trying to write a program that can register a contact, delete or update the contact. so when i compile the is an error message "invalid array assignment" within the the delete function which is the code i posted before. the complete code is
#include
#include
#include
#includeusing namespace std;
enum type{ home,work,fax,mobile,other };
struct contact{
char num[15];
char name[20];
char TphoneN[6];
}phone[5];
const int max=5;
int index=0;//reading function
bool reading(){
char v;
if(index<=5){
cout<<"please enter phone number: ";
gets(phone[index].num);cout<<"please enter the name: "; gets(phone\[index\].name); cout<<"the type of number( fax, mobile, home, work, other): "; gets(phone\[index\].TphoneN); /\*cin>>v; switch(v){ case 'f': phone\[index\].TphoneN='fax'; break; case 'm': phone\[index\].TphoneN='mobile'; break; case 'h': phone\[index\].TphoneN='home'; break; case 'w': phone\[index\].TphoneN='work'; break; case 'o': phone\[index\].TphoneN='other'; break; default: cout<<"please enter a valid key: "; }\*/ index++; return true; } else return false; }
//deleting function
bool delet(){
int flag=0;
cout<<"please enter the name of the person u wanna delete: ";
char del[20];
cin.getline(del,20);
for(int i=0; i<=index; ++i){
if(phone[i].name==del){
for(int j=i; j<=index; ++j){phone\[j\].num=phone\[j+1\].num; phone\[j\].TphoneN=phone\[j+1\].TphoneN; phone\[j\].name=phone\[j+1\].name; } flag=1; }
}
if(!flag){ return false; } else{ index--; return true; }
}
//function displqying all contacts
void all(){
for(int i=0; i<=index; ++i){
cout<>x;
switch(x){
case 1:
bool y=reading();
if(y==true){
cout<<"contact successfuly saved";
}
else
cout<<"an error accure could not save your contact";
break;
case 2:
bool y=delet();
if(y==true){
cout<<"contact successfuly deleted";
}
else
cout<<"sorry your name does not exist";
break;
case 3:
all();
break;
case 4:
cout<<"still sea -
You cannot use the
==
operator to compare arrays. You need to usestrcmp
or similar. You -
That is because you are assigning arrays to arrays, and that is invalid code in C/C++:
phone[j].num=phone[j+1].num;
phone[j].TphoneN=phone[j+1].TphoneN;
phone[j].name=phone[j+1].name;You can use the appropriate functions for C strings, e. g. strcpy. But it would be much better if you just used std::string instead of C strings!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
That is because you are assigning arrays to arrays, and that is invalid code in C/C++:
phone[j].num=phone[j+1].num;
phone[j].TphoneN=phone[j+1].TphoneN;
phone[j].name=phone[j+1].name;You can use the appropriate functions for C strings, e. g. strcpy. But it would be much better if you just used std::string instead of C strings!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)