LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-15-2009, 03:21 PM   #1
dmckay
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Rep: Reputation: 0
ISO C++ forbids comparison between pointer and integer


Hi there,

I'm new to programming (just started it at University) and I'm creating a simple exercise program which is giving me the error in the topic, here's the code

Code:
#include <stdio.h>


const float lowrate = 2;
const float highrate = 5;
const float taxrate = 20;

float accountbalance, interestpaid, newbalance, tax, total;
char taxed;

main()
{
      printf("\n\n\t\tPlease input your account balance");
      scanf("%d",&accountbalance);
      printf("\n\n\t\tIs the account to be taxed?");
      scanf("%c",&taxed);
      getchar();
      
      /*Calculate the interest */
      if(accountbalance < 1000)
      {
                        interestpaid = accountbalance * (lowrate / 100);
                        newbalance = accountbalance + interestpaid;
                        
      }
      interestpaid = accountbalance * (highrate / 100);
      newbalance = accountbalance + interestpaid;
      if(taxed == "y" )
      {
               tax = interestpaid * (taxrate / 100);
               newbalance = newbalance - tax;
              }        
      /*Display the outcomes*/
      
      printf("\n\n\t\tInterest Added = %d", interestpaid);
      printf("\n\n\t\tTotal Account Balance = %d", newbalance);
      getchar();
}
It is saying there is something wrong with the line
if(taxed == "y" )
 
Old 10-15-2009, 03:27 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
This is actually C, although C++ can compile it.

As for your problem, "y" is a const char * that points to a 2-element array containing the character 'y' and 0(null, '\0'). 'y' is the char literal you want.
 
Old 10-15-2009, 03:31 PM   #3
dmckay
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
So I change "char taxed;" to "const char taxed;" ?

It's still giving me the same compile error but with the following added

uninitialized const `taxed'
 
Old 10-15-2009, 03:35 PM   #4
dmckay
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Seem to have fixed it by changing the line to :-

if( &taxed == "y" )
 
Old 10-15-2009, 03:38 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by tuxdev View Post
'y' is the char literal you want.
Quote:
Originally Posted by dmckay View Post
Seem to have fixed it by changing the line to :-

if( &taxed == "y" )
Try reading the answer you were given.

Change to
Code:
      if( taxed == 'y' )
(&taxed == "y") will compile, but it doesn't have the correct meaning. (taxed == 'y') will compile and has the correct meaning.

Last edited by johnsfine; 10-15-2009 at 03:39 PM.
 
Old 10-15-2009, 03:39 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
Originally Posted by dmckay View Post
Code:
char taxed;
It is saying there is something wrong with the line
if(taxed == "y" )
you have to compare characters using single quotes, so this code should instead be
Code:
if (taxed == 'y')
 
Old 10-15-2009, 03:45 PM   #7
dmckay
LQ Newbie
 
Registered: Oct 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks a lot for the help guys
 
Old 10-15-2009, 03:46 PM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
Originally Posted by dmckay View Post
Seem to have fixed it by changing the line to :-

if( &taxed == "y" )
by "fixed" you mean that the compiler doesnt complain anymore? since youre just starting, be aware that if the compiler compiles your code without errors or warnings, it does not mean your code is correct! it simply means that your code has correct syntax and semantics for the language the compiler is for (i.e., C++ in your case).

also, if youre using an IDE, look around for how to turn on more compiler options, i.e. to be "pedantic" (more strict) and show all warnings, as some might not be shown. if you use the command line to compile your programs, i.e. "g++", add the options "-Wall -pedantic" to get more information about your code from the compiler.

basically, since you got rid of the semantic error, it doesnt mean it "works" (does what you think it does).
 
Old 10-15-2009, 04:09 PM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Also -Wextra and -Weffc++. I also use -ansi for portability.
 
  


Reply



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 comparison between pointer and integer BarryKamp Programming 5 04-25-2007 09:06 PM
error: ISO C++ forbids comparison ... NEED HELP URGENT !!! lx3000 Programming 5 10-02-2006 02:48 PM
comparison between pointer and integer a problem? debiant Programming 7 08-28-2006 07:57 PM
Comparison between pointer and integer ---> WHY?? its_godzilla Programming 10 01-28-2005 09:40 PM
ISO C++ forbids comparison between pointer and integer? pimaster Programming 1 11-06-2003 01:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration