On May 20, 8:47 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
"chu...@gmail.com" <chu...@gmail.comwrites:
I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array[10] then how can I check it with a if statement.
This is in the FAQ.
8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?
char *string;
...
if(string == "value") {
/* string matches "value" */
...
}
A: Strings in C are represented as arrays of characters, and C
never manipulates (assigns, compares, etc.) arrays as a whole.
The == operator in the code fragment above compares two pointers
-- the value of the pointer variable string and a pointer to the
string literal "value" -- to see if they are equal, that is, if
they point to the same place. They probably don't, so the
comparison never succeeds.
To compare two strings, you generally use the library function
strcmp():
if(strcmp(string, "value") == 0) {
/* string matches "value" */
...
}
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Thanks for the help..I corrected my program as you told me, but I get
a small and irritating bug, when I compile the program it says:
trial2-1.c: In function 'co2':
trial2-1.c:18: error: parse error before ']' token
Here is the Source:
#include <stdio.h>
double co2(char fuel[], double amount){
double result;
//Petrol
if (strncmp(fuel[], "Car") == 0){
result = 2.3 * amount;
}
//Oil
else if (strncmp(fuel[], "Oil") == 0){
result = 2.7 * amount;
}
//Coal
else if (strncmp(fuel[], "Coal") == 0){
result = 2.4 * amount;
}
//Wood
else if (strncmp(fuel[], "Wood") == 0){
result = 0 * amount;
}
//Electricity
else if (strncmp(fuel[], "Electricity") == 0){
result = 0.4 * Amount;
}
//Natural Gas
else if (strncmp(fuel[], "Gas") == 0){
result = 0.2 * amount;
}
//Air Travel
else if (strncmp(fuel[], "Flight") == 0){
result = 0.3 * amount;
}
return result;
}
//Main program starts here
int main ()
{
char fuel[20];
double amount;
char desptn[40];
double total=0;
char answer;
FILE * fptr;
fptr = fopen("info.txt", "r");
//Displays the programs function
printf("\n\n");
printf("This program is designed to caculate the net CO2 emissions
\n");
printf("- Using the information listed in a file\n");
printf("- Read and analyse the information\n");
printf("- Give total CO2 emission in kg and 'rations'\n");
printf("- Provide a breakdown of the emissions\n");
printf("- Make use of the 'srncmp' function\n\n");
//Continue program?
printf("Do you what to start the program?(Y/N) ");
scanf(" %c",&answer);
printf("Reading File...\n\n\n");
//If Yes?
if(answer == 'Y' || answer == 'y'){
if (fptr == NULL ){
printf("Cannot Locate File, Please Try again!\n");
printf("Quitting...");
}
while (fscanf(fptr,"%19s %lf %39[^\n]",fuel , &amount, desptn) !=
EOF){
printf("%s \t %.2lf \t %s", fuel, amount, desptn);
printf("\n");
total += co2(fuel,amount);
}
}
printf("The total CO2 emission in kg: %.2lf kg", total);
printf("\n");
printf("Equicalent number of CO2 'rations': %.2lf", total = total /
2500);
printf("\nEnd Program!\n\n");
fclose(fptr);
return 0;
}
Thanks
Chris