LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-01-2006, 10:34 PM   #1
lx3000
LQ Newbie
 
Registered: Mar 2006
Distribution: Red Hat Enterprise Linux 4, Fedora Core 7
Posts: 26

Rep: Reputation: 15
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 ....

Last edited by lx3000; 10-01-2006 at 10:36 PM.
 
Old 10-01-2006, 10:35 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
A. Use std::string, not char arrays.
B. String use "" delimiters, not ''
 
Old 10-01-2006, 10:53 PM   #3
lx3000
LQ Newbie
 
Registered: Mar 2006
Distribution: Red Hat Enterprise Linux 4, Fedora Core 7
Posts: 26

Original Poster
Rep: Reputation: 15
Ha !!! tuxdev thanks a lot !!! It worked - I'll keep that std::string in mind ...THANKS !!!
 
Old 10-02-2006, 11:04 AM   #4
bong
LQ Newbie
 
Registered: Aug 2003
Location: Lansing, MI
Posts: 1

Rep: Reputation: 0
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.
 
Old 10-02-2006, 12:56 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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++.
 
Old 10-02-2006, 02:48 PM   #6
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.

Last edited by dmail; 10-02-2006 at 03:04 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ISO C++ forbids declaration of... lucky6969b Programming 3 03-23-2006 07:54 AM
printk --- ISO C90 forbids mixed declaration and code Igor007 Programming 3 09-08-2005 04:05 AM
Error comparing strings and acting on the comparison WindowsBurner Programming 4 10-21-2004 12:37 PM
ISO C++ forbids comparison between pointer and integer? pimaster Programming 1 11-06-2003 01:45 PM
C programming error. warning: comparison between pointer and integer Linh Programming 4 06-06-2003 03:49 PM


All times are GMT -5. The time now is 02:25 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration