Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

c++ - 基于索引的C ++字符数组元素无比较(C++ Character array element based on index no comparison)

I'm attempting a logical comparison at an element in a char-array based on index number but my compiler says illegal comparison.

(我正在尝试根据索引号对char数组中的元素进行逻辑比较,但是我的编译器说非法比较。)

Any ideas about what's going on?

(有什么想法吗?)

#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>

using namespace std;

ofstream MyFile("Assignmentfile.txt");
int userno;
char name[16];
char lastname[16];
char address[51];
char cellno[14];
char landlineno[12];
int sent;

void userinput();
void searchfunc();
void deletecontact();
void displaycontact();
void modifycontact();
void sortcontact();
void findcontact();

int main()
{
    MyFile<<"First Name   Last Name   Address   Cell Number   Landline Number"<<endl;       
    userinput();        
    return 0;
}

void userinput()
{
    {   
        cout<<"Would you like to enter a new contact? (1/0) ";
        cin>>sent;
        while (sent==1)
        {
            cout<<"Enter Name: ";
            cin>>name;
            MyFile<<left<<setw(16)<<name<<"|";
            cout<<"Enter Last name: ";
            cin>>lastname;
            MyFile<<left<<setw(16)<<lastname<<"|";
            cout<<"Enter Address: ";
            cin>>address;
            MyFile<<left<<setw(51)<<address<<"|";
            cout<<"Enter Cell Number: ";
            cin>>cellno;
            if (cellno[0]=="+")
            {
                cout<<"Enter Cell number again starting with +92"; // The problem appears here //
            }
            MyFile<<left<<setw(14)<<cellno<<"|";
            cout<<"Enter Landline Number: ";
            cin>>landlineno;
            MyFile<<left<<setw(12)<<landlineno<<endl;
            cout<<endl;
            cout<<"Would you like to enter a new contact? (1/0) ";
            cin>>sent;

        }
        MyFile.close();
    }
}

The program must be able to write and read from a text file.

(该程序必须能够写入和读取文本文件。)

It can create contacts, modify them, delete them, sort them and search through them.

(它可以创建联系人,对其进行修改,将其删除,对其进行排序并对其进行搜索。)

The problem is that the cell number must start from "+92" ie "+923454356568" .

(问题在于,单元号必须从"+92"开始,即"+923454356568" 。)

I thought that if (cellno[0]=="+") and so on would work.

(我认为, if (cellno[0]=="+")等等可以工作。)

I cannot use strings and only have to rely on character type arrays.

(我不能使用字符串,而只能依靠字符类型数组。)

Using strings would make all of this a piece of cake.

(使用字符串会使这一切变得轻松。)

Below is the assignment I wish to complete.

(以下是我要完成的作业。)

在此处输入图片说明

  ask by Hassan Iftikhar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your problem is this line:

(您的问题是这一行:)

if (cellno[0]=="+")

You are comparing single character, at index 0 in cellno char array (which hopefully contains a C string) with string literal , which is a pointer (as your compler error says).

(您正在将cellno char数组(希望包含一个C字符串)的索引0处的单个字符与字符串literal (它是一个指针)进行比较(正如您的compler错误所说)。)

You want to compare a single char, like this:

(您要比较单个字符,如下所示:)

if (cellno[0]=='+')

Note how single and double quotes have a very different meaning!

(注意单引号和双引号的含义有很大不同!)


You code has a lot of other issues, too many to list here, but that should solve the problem you are asking about.

(您的代码还有很多其他问题,在这里没有太多列出,但这应该可以解决您所要解决的问题。)

But one advice I add: do not use C strings in C++, if you can avoid it!

(但是,我建议一个建议:如果可以避免,不要在C ++中使用C字符串!)

Use std::string as soon as you are allowed to!

(允许后立即使用std::string !)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...