how to I check a string in "if" loops??

C

chutsu

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

if (Array[ ] == 'APPLE'){
do something;
}

or do I need to use a different method to check?
Thanks
Chris
 
O

osmium

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.

if (Array[ ] == 'APPLE'){
do something;
}

or do I need to use a different method to check?

That won't work. Use strcmp() in <string.h>
 
B

Ben Pfaff

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" */
...
}
 
F

Flash Gordon

Ben Pfaff wrote, On 20/05/07 20:47:
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?

<snip>

To the OP, the FAQ is located at http://c-faq.com/ and should be checked
before posting questions here.
 
C

chutsu

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" */
...
}

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
 
C

chutsu

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" */
...
}

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
 
B

Ben Pfaff

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>

Add #include said:
double co2(char fuel[], double amount){
double result;

//Petrol
if (strncmp(fuel[], "Car") == 0){

Use strcmp not strncmp.
Use "fuel" not "fuel[]".
result = 2.3 * amount;
} [...]
//Air Travel
else if (strncmp(fuel[], "Flight") == 0){
result = 0.3 * amount;
}

What if it isn't any of those?
return result;
}


//Main program starts here

It's really better to avoid silly comments like this.
Everyone knows that the main function is the main program.
int main ()

It's better to write "int main (void)" explicitly.
{
char fuel[20];
double amount;
char desptn[40];
double total=0;
char answer;

FILE * fptr;
fptr = fopen("info.txt", "r");

Should check for success here.
//Continue program?
printf("Do you what to start the program?(Y/N) ");

Do you "what"? :)
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...");
}

Seems a little late to report that failure here. You knew it
would fail before user interaction started.
printf("Equicalent number of CO2 'rations': %.2lf", total = total /
2500);

It's not very nice for output to have unrelated side effects.
 
I

Ian Collins

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){

The empty [] after fuel is you problem. Remove them, empty [] are only
valid and required when declaring a function parameter type as an array.
 
C

chutsu

Ben Pfaff wrote, On 20/05/07 20:47:
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?

<snip>

To the OP, the FAQ is located athttp://c-faq.com/and should be checked
before posting questions here.

Thank....I got what I needed...I thank you all who answered my
question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top