LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-25-2007, 04:32 PM   #1
BarryKamp
LQ Newbie
 
Registered: Apr 2007
Posts: 4

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


Hi, Im a beginner at C, i just need some help getting something sorted where i can display information. I can add more to it for what i want to do, i just wanted to know why the error "ISO C++ forbids comparison between pointer and integer" comes for all the lines of "if ( fuel == "Petrol" ) {". Thanks for the help.

#include <stdio.h>
#include <stdlib.h>

void Petrol();
void Oil();
void Coal();
void Wood();
void Electricity();
void Naturalgas();


int main(void)

{
int fuel;

printf("Please input your fuel or activity: ");
scanf ( "%g", &fuel );

if ( fuel == "Petrol" ) {
printf("you are using petrol\n");
}
else if ( fuel == "Oil" ) {
printf("you are using oil\n");
}
else if ( fuel == "Coal" ) {
printf("you are using coal\n");
}
else if ( fuel == "Wood" ) {
printf("you are using wood\n");
}
else if ( fuel == "Electricity" ) {
printf("you are using Electricity\n");
}
else if ( fuel == "Natural Gas" ) {
printf("you are using Natural Gas\n");
}
else {
printf("you are using Air travel\n");
}

getchar();
return 0;

}
 
Old 04-25-2007, 04:48 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It's rather simple. You declare fuel as an int:
Quote:
int fuel;
And then you compare it to a string literal: "Petrol"

In C++ a string literal (a sequence of characters expressly listed in a program's source code) is substituted by a pointer to the location in memory that the compiler reserved to store the sequence of characters. Therefore, you have an int (fuel) being compared (==) with a pointer ("Petrol").

You need to do one of two things:

1. Display a menu and have the user select a fuel type by selecting a number. For instance:
Code:
Select a fuel type:
  1. Petrol
  2. Oil
  3. Coal
  4. Wood
  5. Electricity
  6. Natural gas

Your choice:
This way, you change your if statement to be "if ( fuel == 1 ) {" to trigger the Petrol section of code, and similar comparisons for the others.

2. Redeclare fuel as a char *, reserve memory for the variable, change your scanf() call to read in a sequence of characters, and then change your if statements to something like "if ( strcmp( fuel, "Petrol" ) == 0 ) {"

Note: #2 above is the standard C way of doing things (which will work in C++), but the "C++ way" would likely be to use the AnsiString class with a few modifications to the steps I listed.
 
Old 04-25-2007, 05:06 PM   #3
BarryKamp
LQ Newbie
 
Registered: Apr 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks alot for the tips
 
Old 04-25-2007, 05:27 PM   #4
BarryKamp
LQ Newbie
 
Registered: Apr 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Right... Now i wish to get the certain types of fuel which i think i have given values for the co2 emission per unit to times by how many units the user would put into it.
#include <stdio.h>
#include <stdlib.h>

int main(void)

{
double fuel,unit,a,b,c,d,e,f,g;
// these are the different CO2 emissions for each fuel.
a = 2.3;
b = 2.7;
c = 2.4;
d = 0.0;
e = 0.4;
f = 0.2;
g = 0.3;

printf ("State your fuel use or way of travel\n");

printf (" a. Petrol\n b. Oil\n c. Coal\n d. Wood\n e. Electricity\n f. Natural gas\n g. Air Travel ");
scanf ( "%g", &fuel );
//this provides my table in a menu style.
if ( fuel == a ) {
printf("you are using petrol\n");
}
else if ( fuel == b ) {
printf("you are using oil\n");
}
else if ( fuel == c ) {
printf("you are using coal\n");
}
else if ( fuel == d ) {
printf("you are using wood\n");
}
else if ( fuel == e ) {
printf("you are using Electricity\n");
}
else if ( fuel == f ) {
printf("you are using Natural Gas\n");
}
else {
printf("you are using Air travel\n");
}
printf ("State the amount of fuel or travel distance");

getchar();
return 0;

}
Am i on the right track here or am i running into a blind alley.
 
Old 04-25-2007, 07:48 PM   #5
BarryKamp
LQ Newbie
 
Registered: Apr 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Right... so i have changed the second variable to units and adding a bit to the switch, but i wish for the answer to be displayed and at the moment it isnt displayed the bash cell closes after asking me how many units.
Thanks once again in advance.



1.
#include <stdio.h>
2.

3.
int main(void)
4.
5.
{
6.
7.
int fuel,units;
8.
9.
printf ("State your fuel use or way of travel\n");
10.
11.
printf (" 1. Petrol\n 2. Oil\n 3. Coal\n 4. Wood\n 5. Electricity\n 6. Natural gas\n 7. Air Travel ");
12.
{
13.
scanf ( "%g", &fuel );
14.
}
15.
printf (" How many units of fuel or distance travelled has been used?\n");
16.
scanf ( "%g", &units );
17.
switch( fuel)
18.
19.
{
20.
21.
case 1:
22.
23.
24.
printf("you are using $%f of kg CO2 by using Petrol\n", (float)fuel * 2.3);
25.
26.
break;
27.
28.
case 2:
29.
30.
printf("you are using $%f of kg CO2 by using Oil\n", (float)fuel * 2.7);
31.
32.
break;
33.
34.
case 3:
35.

36.
printf("you are using $%f of kg CO2 by using Coal\n", (float)fuel * 2.4);
37.
38.
break;
39.
40.
case 4:
41.
42.
printf("you are using $%f of kg CO2 by using Wood\n", (float)fuel * 0.0);
43.
44.
break;
45.
46.
case 5:
47.
48.
printf("you are using $%f of kg CO2 by using Electricity\n", (float)fuel * 0.4);
49.
50.
break;
51.
52.
case 6:
53.
54.
printf("you are using $%f of kg CO2 by using Natural Gas\n", (float)fuel * 0.2);
55.
56.
break;
57.
58.
case 7:
59.
60.
printf("you are using $%f of kg CO2 by using Air Travel\n", (float)fuel * 0.3);
61.
62.
break;
63.
64.
}
65.
getchar();
66.

67.
return 0;
68.
69.
}

#include <stdio.h>

int main(void)

{

int fuel,units;

printf ("State your fuel use or way of travel\n");

printf (" 1. Petrol\n 2. Oil\n 3. Coal\n 4. Wood\n 5. Electricity\n 6. Natural gas\n 7. Air Travel ");
{
scanf ( "%g", &fuel );
}
printf (" How many units of fuel or distance travelled has been used?\n");
scanf ( "%g", &units );
switch( fuel)

{

case 1:


printf("you are using $%f of kg CO2 by using Petrol\n", (float)fuel * 2.3);

break;

case 2:

printf("you are using $%f of kg CO2 by using Oil\n", (float)fuel * 2.7);

break;

case 3:

printf("you are using $%f of kg CO2 by using Coal\n", (float)fuel * 2.4);

break;

case 4:

printf("you are using $%f of kg CO2 by using Wood\n", (float)fuel * 0.0);

break;

case 5:

printf("you are using $%f of kg CO2 by using Electricity\n", (float)fuel * 0.4);

break;

case 6:

printf("you are using $%f of kg CO2 by using Natural Gas\n", (float)fuel * 0.2);

break;

case 7:

printf("you are using $%f of kg CO2 by using Air Travel\n", (float)fuel * 0.3);

break;

}
getchar();

return 0;

}
 
Old 04-25-2007, 09:06 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Ok, first thing... more of an "administrative" detail. When posting source code, it makes it a whole lot easier for everyone to read if you surround the code with "code" tags. That is:

[ code ]

[ /code ]

But leave out the spaces inside the square brackets - otherwise LQ would have interpreted it as though I wanted to add code in my message

I'll post, in code tags, your program from the last message minus the line numbers (and using my indention preferences).

Code:
#include <stdio.h>

int main(void)
{
  int fuel,units;

  printf ("State your fuel use or way of travel\n");
  printf (" 1. Petrol\n 2. Oil\n 3. Coal\n 4. Wood\n 5. Electricity\n 6. Natural gas\n 7. Air Travel ");
  {
    scanf ( "%g", &fuel );
  }

  printf (" How many units of fuel or distance travelled has been used?\n");
  scanf ( "%g", &units );

  switch( fuel)
  {
    case 1:
      printf("you are using $%f of kg CO2 by using Petrol\n", (float)fuel * 2.3);
    break;

    case 2:
      printf("you are using $%f of kg CO2 by using Oil\n", (float)fuel * 2.7);
    break;

    case 3:
      printf("you are using $%f of kg CO2 by using Coal\n", (float)fuel * 2.4);
    break;

    case 4:
      printf("you are using $%f of kg CO2 by using Wood\n", (float)fuel * 0.0);
    break;

    case 5:
      printf("you are using $%f of kg CO2 by using Electricity\n", (float)fuel * 0.4);
    break;

    case 6:
      printf("you are using $%f of kg CO2 by using Natural Gas\n", (float)fuel * 0.2);
    break;

    case 7:
      printf("you are using $%f of kg CO2 by using Air Travel\n", (float)fuel * 0.3);
    break;
  }

  getchar();

  return 0;
}
I get the impression this is a homework/class assignment. So I'm not going to give you the answer, but point you in the right direction.

First, understand that your use of scanf() to read in a variable needs a conversion string that matches the data type of the variable you're trying to store data in. Review you notes/man pages to determine whether %g is an appropriate conversion string for an int data type (man scanf). The binary format of the data is different for some data types--which is why you need to make sure the conversion string and the data type match.

Second, it's generally good programming practice to never leave a logical "gap" in your code. That means to always include "else" statements for each "if" and "default" blocks for every "switch". I have a feeling if you placed an error message like this:
Code:
    default:
      printf( "Unexpected value for fuel! (%d)\n", fuel );
    break;
in your switch block, it likely would have shown you a symptom of the problem resulting from the scanf()-data type issue I mentioned earlier.
 
  


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
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
C programming error. warning: comparison between pointer and integer Linh Programming 4 06-06-2003 03:49 PM

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

All times are GMT -5. The time now is 06:57 PM.

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