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;

}

Recommended Answers

All 16 Replies

Is not only fuel == "Petrol" the once with problems.
You are trying to compare the value of an integer fuel with an array of characters. All the if that you have with strings will give you an error. When you include a word among " " it became a literal string. Are you trying to use the user made functions prototyped at the beginning of your code?.

I'm sorry but is there a way you could tell me how to fix this as you understand what i am trying to do? Im not great with programming and wish to learn.

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


I read your prototypes: void Petrol(); void Oil(); void etc..., and had
a laugh, I wish my house and my car could void all these thing also.

What are you trying to do?. Where are these functions?.

ok i took into what you were saying and now my code reads as..

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

int main(void)
                        
{
  int fuel;                           
  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 );                          
                      
  if ( fuel == 1 ) {                
     printf("you are using petrol\n"); 
  }
  else if ( fuel == 2 ) {            
     printf("you are using oil\n");          
  }
  else if ( fuel == 3 ) {
    printf("you are using coal\n");  
  }
  else if ( fuel == 4 ) {
    printf("you are using wood\n"); 
  }
  else if ( fuel == 5 ) {
    printf("you are using Electricity\n"); 
}
else if ( fuel == 6 ) {
    printf("you are using Natural Gas\n"); 
}
    else {
       printf("you are using Air travel\n"); 
  }

  getchar();
  return 0;

}

This has helped as my code actually runs now with a menu as such function. Thanks for the help.

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.

Considere a switch expression for your code.

#include <stdio.h>

int main(void)
{
    int fuel;
    
    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 );

    switch( fuel)
    {
        case 1:
            printf("you are using petrol\n");
            break;
        case 2:
            printf("you are using oil\n");
            break;
        case 3:
            printf("you are using coal\n");
            break;
        case 4:
            printf("you are using wood\n");
            break;
        case 5:
            printf("you are using Electricity\n");
            break;
        case 6:
            printf("you are using Natural Gas\n");
            break;
        default:
            printf("you are using Air travel\n");
            break;
    }

    getchar();
    return 0;
}

that helps alot. Ive modified it slightly by putting in the second question with the scanf and the values for each of the co2 emissions and i cant seem to get the worked out value printed in the switch.

#include <stdio.h>

      int main(void)
   
      {
   
      int fuel;
   
      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", &fuel );
      switch( fuel)
  
      {
  
      case 1:
  
      printf("you are using petrol\n");
      fuel * 2.3;
      
      break;
  
      case 2:
  
      printf("you are using oil\n");
      fuel * 2.7;
      
      break;
  
      case 3:

      printf("you are using coal\n");
      fuel * 2.4;
     
      break;
  
      case 4:
  
      printf("you are using wood\n");
      fuel * 0.0;
      
      break;
  
      case 5:
  
      printf("you are using Electricity\n");
      fuel * 0.4;
      
      break;
  
      case 6:
  
      printf("you are using Natural Gas\n");
      fuel * 0.2;
      
      break;
  
      case 7:
  
      printf("you are using Air travel\n");
      fuel * 0.3;
      
      break;
  
      }
      getchar();
 
      return 0;
  
      }

Thanks by the way. Your help is greatly apriciated. If only i could employ you as a personal tutor rather than teaching myself.

Please start using code tags.

> scanf ( "%g", &fuel );
What about "%d"?

And why are you storing the number of fuel units travelled in the same variable as the menu option? The menu choice is completely wiped out when the user enters the number of fuel units travelled.

>i cant seem to get the worked out value printed in the switch.
Do you mean something like this?:

case 1:
  
      printf("you are using $%f in petrol\n", (float)fuel * 2.3);

well like this.. printf("you are using $%f of kg CO2 by using petrol\n", (float)fuel * 2.3); but ye this wont "print" on my bash cell

Right... so i have taken into account what you have both said i hope by changing the second variable to units and adding your 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.

#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;

  }

You still haven't changed "%g" to "%d". %g in scanf() means that you want a floating point number. "%d" means that you want an integer.

Regarding the bash window closing: that's because getchar() is picking up the newline left behind by scanf. You could always use fgets() instead of scanf(), which would eliminate this problem. Here's a good tutorial:
http://www.daniweb.com/tutorials/tutorial45806.html

Right... 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.

Every time you use scanf() to read input, it leaves at least as a minumus the ENTER key in the stdin buffer. Learn to use fgets() to read input.
To make your code work for now, after every scanf() write getchar();
that will read the ENTER and at the end of the program your code will pause.

Thanks guys and thanks for the link. For this certain program ive just done the easy option but i will definately use the tutorial to improve my programming.

One final thing before you two can have a rest . hehe. I wish to make a loop i guess so that after they have been asked and answered the questions faced they can go back to the beginning or just be asked the questions again. is there an easy way to do this?

#include <stdio.h>

      int main(void)
   
      {
   
      int fuel,units;
      printf ("Petrol 2.3kg CO2 / unit\nOil 2.3kg CO2 / unit\nCoal 2.3kg CO2 / unit\nWood 2.3kg CO2 / unit\n");
      printf ("Electricity 2.3kg CO2 / unit \nNatural gas 2.3kg CO2 / unit \nAir travel 2.3kg CO2 / unit \n\n\n"); 
      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 ( "%d", &fuel );
      
      getchar();
      } 
      
      printf (" How many units of fuel or distance travelled has been used?\n");
      
      scanf ( "%d", &units );
      
      getchar();
     
      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;
      }

Going back to my switch example:

#include <stdio.h>

int main(void)
{
    int fuel;
    int c;
    
    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 Any key. Air Travel ");

    do
    {
 
        scanf ( "%d", &fuel);
        getchar();
        
        switch( fuel)
        {
            case 1:
                printf("you are using petrol\n");
                break;
            case 2:
                printf("you are using oil\n");
                break;
            case 3:
                printf("you are using coal\n");
                break;
            case 4:
                printf("you are using wood\n");
                break;
            case 5:
                printf("you are using Electricity\n");
                break;
            case 6:
                printf("you are using Natural Gas\n");
                break;
            default:
                printf("you are using Air travel\n");
                break;
        }
        printf("Would you like to do it again?(Y/N): ");
 
        
        c = toupper(getchar());
        if(c == 'Y')
        {
            printf("What number is going to be?: ");
        }
        
    }
    while( c != 'N');

    getchar();
    return 0;
}

Now if the user enters any thing that is not those two letters (y/n); you have a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.