N
N
NotCoolProgrammer2021-12-08 14:43:24
C++ / C#
NotCoolProgrammer, 2021-12-08 14:43:24

Comparison between pointer and integer. How to decide?

Task: display all numbers that start with "22"
Error (line 29): comparison between pointer and integer ('char *' and 'char')
if (s[0].pnumber == '2' && s[1 ].pnumber == '2')
What should I do to solve the problem?

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main ( )
{
int n;
cout<<"Enter number of subscribers: ";
cin>>n;
struct subscriber
{ int number;
char FIO[40];
char address[50];
char pnumber[10];
int duty;
}s[n];
int i;
for (i=0;i<n;i++)
{ cout<<"Number: "; cin>>s[i].number;
cout<<"FIO: "; cin>>s[i].FIO;
cout<<"Address: "; cin>>s[i].address;
cout<<"Phone number "; cin>>s[i].pnumber;
cout<<"Duty: "; cin>>s[i].duty;
}
float max=0;
int imax= -1;
cout<<"Result"<<endl;
for (i=0;i<n;i++)
if (s[0].pnumber == '2' && s[1].pnumber == '2')
if (s[i].duty> max ) { max= s[i].duty; imax=i;}
if (imax> -1)
{
cout<<"Number: "<<s[imax].number<<endl;
cout<<"FIO: "<<s[imax].FIO<<endl;
cout<<"Address: "<<s[imax].address<<endl;
cout<<"Phone number: "<<s[imax].pnumber<<endl;
cout<<"Duty: "<<s[imax].duty<<endl;
} else cout<<"No debtors were found"<<endl;
return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2021-12-08
@NotCoolProgrammer

Several variants.
1. turn pnumber into a string
2. strncmp(s[0].pnumber, std::size(s[0].pnumber), "2") == 0
3. std::string_view(s[0].pnumber ) == "2"
Sorry, buggy, and there. and it needs double quotes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question