-2

I'm attempting a logical comparison at an element in a char-array based on index number but my compiler says illegal comparison. 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" i.e "+923454356568". I thought that if (cellno[0]=="+") and so on would work.

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.

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Provide a minimal (one more a minimal) complete program that reproduces the problem. – Vlad from Moscow Nov 28 '19 at 20:48
  • 2
    `if (cellno[0]=="+")` won't work, but did you try `if (cellno[0]=='+')` ? – Romen Nov 28 '19 at 20:50
  • @Romen ill try. – Hassan Iftikhar Nov 28 '19 at 20:54
  • @VladfromMoscow this is the complete program – Hassan Iftikhar Nov 28 '19 at 20:57
  • 1
    @HassanIftikhar That should be "complete" in the sense of "fully compiles while demonstrating the error", not in the sense of "does everything my assignment requires". The goal is **minimal** and *complete*: all the code needed to demonstrate the problem, but no more than needed (c.f. [mcve]). If asked well, this question should have no need to include your full assignment description. – JaMiT Nov 28 '19 at 21:02
  • Also the file is cleaned blank each time i run the program. I would like the data entered into the file to stay. How do i do that? – Hassan Iftikhar Nov 28 '19 at 21:02
  • @JaMiT This is my first semester and first ever experience at coding. Not to sound rude or anything i came here for help on my code not a lesson on how to carry out things. I posted the whole assignment to be safe and so that it is easy for people to understand why i'm doing what i'm asking and what is the basic functionality of the program. If I didn't mention I can't use strings people would say "just use a string instead of char". – Hassan Iftikhar Nov 28 '19 at 21:06
  • You have a problem. Create a minimal example that reproduces the problem. Your example is not minimal. Read [mcve] – Thomas Sablik Nov 28 '19 at 21:08
  • @VladfromMoscow I'm very new to programming and i'm sorry if this is all confusing but i literally have no idea about a lot of stuff. If you mean that i only post a program that deals with the character array and its functionality alone i'll post that. – Hassan Iftikhar Nov 28 '19 at 21:08
  • @ThomasSablik Alright thanks – Hassan Iftikhar Nov 28 '19 at 21:09
  • If you don't want to overwrite or truncate the old file you have to open it with the corresponding flag [app](https://en.cppreference.com/w/cpp/io/ios_base/openmode) https://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream – Thomas Sablik Nov 28 '19 at 21:10
  • @HassanIftikhar As I understand it, you came here for help on your code, not help on getting help on your code. You have no interest in having your experience help others. You also believe that if someone asks you to make it easier for them to help you, then that is unfairly shifting a burden from them (the volunteer) to you (the only one who will benefit). Kind of short-sighted and self-centered, but I guess it worked out this time. Since someone had the time to sort through your code, I was able to find a duplicate. – JaMiT Nov 29 '19 at 16:08
  • Does this answer your question? [Single quotes vs. double quotes in C or C++](https://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c-or-c) – JaMiT Nov 29 '19 at 16:09
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 01 '19 at 15:52

1 Answers1

3

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).

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! Use std::string as soon as you are allowed to!

hyde
  • 60,639
  • 21
  • 115
  • 176
  • Tysm. Tbh you are the only person here who's not triggered and isn't scolding me for posting the whole thing and assuming that I should naturally just know the solution to this problem. This fixes it and solves my problem for now. Thank you! – Hassan Iftikhar Nov 28 '19 at 21:16
  • Just to add a few suggestions to your code in general. Do not use global variables if they are not necessary, use getline for getting a line of text from user input (for example to get the address which will probably have multiple words). Why the additional {} scope inside of the userinput function? – Meliodas Nov 28 '19 at 21:23
  • @HassanIftikhar: don't take the feedback personally. When you posted here, it looked like you wanted someone to do your work for you, which usually is not well received. Readers also saw that you wanted volunteers here to be affected by your deadline (which I have now removed). In the circumstances, some critical feedback is to be expected. – halfer Dec 01 '19 at 15:56