ISO C++ forbids comparison between pointer and integer
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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");
}
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.
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.
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);
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.