LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   error: ISO C++ forbids comparison ... NEED HELP URGENT !!! (https://www.linuxquestions.org/questions/programming-9/error-iso-c-forbids-comparison-need-help-urgent-488608/)

lx3000 10-01-2006 10:34 PM

error: ISO C++ forbids comparison ... NEED HELP URGENT !!!
 
I've got this error while compiling one of my c++ progs:
Friends_command.cpp: In function `int main()':
Friends_command.cpp:30: error: ISO C++ forbids comparison between pointer and integer
Friends_command.cpp:30:35: warning: character constant too long for its type
Friends_command.cpp:30: error: ISO C++ forbids comparison between pointer and integer
Friends_command.cpp:93:16: warning: multi-character character constant
Friends_command.cpp:93: error: ISO C++ forbids comparison between pointer and integer
Friends_command.cpp:93:35: warning: character constant too long for its type
Friends_command.cpp:93: error: ISO C++ forbids comparison between pointer and integer
Friends_command.cpp:103:16: warning: multi-character character constant
Friends_command.cpp:103: error: ISO C++ forbids comparison between pointer and integer
Friends_command.cpp:103:35: warning: character constant too long for its type
Friends_command.cpp:103: error: ISO C++ forbids comparison between pointer and integer
Friends_command.cpp:109:16: warning: multi-character character constant
Friends_command.cpp:109: error: ISO C++ forbids comparison between pointer and integer
Friends_command.cpp:109:35: warning: character constant too long for its type
Friends_command.cpp:109: error: ISO C++ forbids comparison between pointer and integer

here is my code:
Code:

#include<iostream>

#include<fstream>

#include<iomanip.h>
#include<stdlib.h> //need to use for the system() function
using namespace std;



struct Friends

{

      char name[30];

      int age;

      char mail[40];

      int telephone;

      char birthday[15];

      char sex[20];

      char address[30];

};

int main()

{
        fstream outFile("Friends.txt",ios::out|ios::app); //for writing to file (ofstream)

                                                    //and ios::app - to make the file updatable

  ifstream inFile ("Friends.txt"); //for reading from file
       
        int i,n,max;
        //HERE IS THE PROBLEM ....
        char command[11];

        cout << "Enter the command(--help - for help): ";
        cin >> command;
        cout << "\n\n";

        if(command == '-e' || command == '--enter') //<------HERE IS THE PROBLEM
        {
                cout << "======== YOUR FRIEND RECORD ========\n\n";

          cout << "Enter the number of friends: ";

          cin >> n;

                  max=n;

          Friends fr[max];

                for(i=0;i<n;i++)

          {

              cout << "Friend [ " << i << " ] : ";

              cout << "Enter the name of the friend : ";

              cin >> fr[i].name;

              cout << "\n";

              cout << "              Enter the age of the friend : ";

              cin >> fr[i].age;

              cout << "\n";

              cout << "              Enter the sex of the friend (male/female) : ";

              cin >> fr[i].sex;

              cout << "\n";

              cout << "              Enter the birthday of the friend (dd/mm/yy) : ";

              cin >> fr[i].birthday;

              cout << "\n";

              cout << "              Enter the e-mail of the friend : ";

              cin >> fr[i].mail;

              cout << "\n";

              cout << "              Enter the telephone of the friend : ";

              cin >> fr[i].telephone;

              cout << "\n";

              cout << "              Enter the address of the friend : ";

              cin >> fr[i].address;

              cout << "\n\n\n";


              //writing to file

              cout << "Writing to file ... \n\n\n";

              if (! outFile)

              {

                cout << "ERROR. Unable to Open the file\n";

              }

              else

              {

                outFile << "Data of " << fr[i].name;

                outFile << "\n";

                outFile << "=============================\n\n";

                outFile << "NAME : " << fr[i].name;

                outFile << "\n";

                outFile << "AGE : " << fr[i].age;

                outFile << "\n";

                outFile << "SEX : " << fr[i].sex;

                outFile << "\n";

                outFile << "BIRHTDAY (dd/mm/yy): " << fr[i].birthday;

                outFile << "\n";

                outFile << "MAIL : " << fr[i].mail;

                outFile << "\n";

                outFile << "TELEPHONE : " << fr[i].telephone;

                outFile << "\n";

                outFile << "ADDRESS : " << fr[i].address;

                outFile << "\n";

              }

          }

          outFile.close();
        }

        if(command == '-v' || command == '--visualize')//<------HERE IS THE PROBLEM
        {
                cout << "======== YOUR FRIEND RECORD ========\n\n";

          while (inFile.good())

          {

              cout << (char)inFile.get();

          }

          inFile.close();
        }

        if(command == '-d' || command == '--delete')//<------HERE IS THE PROBLEM
        {
                cout << "Deleting created file ...\n\n\n";

          system("rm Friends.txt"); //eliminates the created file
        }

        if(command == '-h' || command == '--help')//<------HERE IS THE PROBLEM
        {
                cout << "-e  --enter      :  Enter the new friend record.\n";
                cout << "-v  --visualize  :  Visualize all friend records.\n";
                cout << "-d  --delete    :  Delete the storage file of friend record.\n";
                cout << "-h  --help      :  Visualize the help option.\n";
                cout << "\n\n";
        }
        return 0;
}

How can I compare my command with the options I want ??

P.S: Sorry for the comlicated code .... :)

tuxdev 10-01-2006 10:35 PM

A. Use std::string, not char arrays.
B. String use "" delimiters, not ''

lx3000 10-01-2006 10:53 PM

Ha !!! tuxdev thanks a lot !!! It worked - I'll keep that std::string in mind ...THANKS !!!

bong 10-02-2006 11:04 AM

The string comparison still won't work even if you use double quotes. To compare strings using char arrays you have to use strcmp in string.h:


Code:

if (strcmp(command, "-e") == 0 || strmcp(command, "--enter") == 0) {
    cout << "..." << endl;
}


and so on. This statement:


Code:

if (command == "-e" || command == "--enter") {
    ...
}


is comparing pointer values; command and "-e" are both pointers and very unlikely to be equal.

ta0kira 10-02-2006 12:56 PM

Quote:

Originally Posted by bong
The string comparison still won't work even if you use double quotes.

If the first arg is a std::string then the second can be a string constant or an otherwise NULL terminated string:
Code:

#include <string>
#include <iostream>

int main()
{
        std::string Test = "TEST";

        if (Test == "TEST") std::cout << "Match 1\n";

        if (std::string("TEST") == "TEST") std::cout << "Match 2\n";
}

ta0kira

PS To OP: you should use <cstdlib> instead of <stdlib.h> when using C++.

dmail 10-02-2006 02:48 PM

The following should stop this code from compiling
Code:

...
cin >> n;
max=n;
Friends fr[max];
...

You need to allocate memory here as this is a runtime variable.
Please refrain from putting crap like
Quote:

NEED HELP URGENT !!!
in your thread title.


All times are GMT -5. The time now is 01:41 AM.