Hi i am a beginner and i have to make a simple phonebook programme in C using library . I would definitely use but im not allowed to as it is for an assignment. Below is my code until now and i have 3 errors which i don't know how to solve. I know conversion from char to const char* is not allowed but i really need to compare these two type c arrays and i can't figure it out how to. I am using strcmp and i am using '\0' as a char which seem correct.
#include <iostream>
#include <cstring>
using namespace std;
struct contact {
char name[30];
char surname[30];
char phone_number[30];
};
int main() {
for (int i = 0; i < 30; i )
{
if (strcmp(person.name[i],person.surname[i]) != '\0') <--- //ERROR HERE
cout << person.name[i] << person.surname[i] << person.phone_number[i];
check ;
}
char temp;
char temp1;
cout << "Insert the name of the contact to delete: \n";
cin >> temp;
cout << "Insert the surname of the contact to delete: \n";
cin >> temp1;
int check = 0;
for (int i = 0; i < 30; i )
{
if (strcmp(temp,person.name[i]) == 0 && strcmp(temp1, person.surname[i]) == 0)
{ ^-- // 2 ERRORS HERE CONVERSION FROM 'CHAR' TO 'CONST CHAR*'
cout << "Contact deleted!\n";
person.name[i] = '\0';
person.surname[i] = '\0';
person.phone_number[i] = '\0';
check ;
}
if (check == 0)
{
cout << "This person is not in your contact list\n ";
return 0;
}
CodePudding user response:
maybe you don't understand struct well, here is a sample I have revised, you can take it for reference
#include <iostream>
#include <stdio.h>
using namespace std;
struct person{
char name[30];
char surname[30];
char phone_number[30];
};
int main()
{
person Persons[] = { // structure initialization
{"Bob","Thug Bob","01230123"},
{"Marry","Gangster Marry","9999999"},
{"Somebody","Mr Somebody","777777"}
};
int Size = sizeof(Persons)/sizeof(Persons[0]); // return size of Persons array
for(int i=0;i<Size;i ){
cout << Persons[i].name << "\t"<< Persons[i].surname << "\t"<< Persons[i].phone_number <<endl;
}
return 0;
}
CodePudding user response:
temp and temp1 are char
s. strcmp expects const char*
.
I'd suggest using std::string
for C , it is a wrapper over char* and has more convenience methods to work with.
CodePudding user response:
I will just start from scratch as this code is really inconvenient. I am sorry for making a unnecessary question.