448,712 Members | 1,591 Online Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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 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 May 20 '07 #1
9 Replies

 P: n/a May 20 '07 #2

 P: n/a "ch****@gmail.com"

 P: n/a On May 20, 8:47 pm, Ben Pfaff 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; double amount; char desptn; 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 May 20 '07 #4

 P: n/a On May 20, 8:47 pm, Ben Pfaff 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; double amount; char desptn; 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 May 20 '07 #5

 P: n/a ch****@gmail.com wrote: > 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 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. -- Ian Collins. May 20 '07 #7

 P: n/a Ben Pfaff wrote, On 20/05/07 20:47: "ch****@gmail.com" I got a simple program, and I was wondering how do you check if thestring in an array = a string. For example if I put "APPLE" in arrayArray 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? To the OP, the FAQ is located at http://c-faq.com/ and should be checked before posting questions here. -- Flash Gordon May 20 '07 #8

 P: n/a On May 20, 9:09 pm, Flash Gordon To the OP, the FAQ is located athttp://c-faq.com/and should be checked before posting questions here. -- Flash Gordon Thank....I got what I needed...I thank you all who answered my question. May 20 '07 #9

 P: n/a "ch****@gmail.com" wrote: > .... snip ... > 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 double co2(char fuel[], double amount){ double result; //Petrol if (strncmp(fuel[], "Car") == 0){ result = 2.3 * amount; } .... snip ... > //Air Travel else if (strncmp(fuel[], "Flight") == 0){ result = 0.3 * amount; } return result; } Use strcmp(fuel, "Car"), not strncmp (which you are miscalling). Be aware that you may return an undefined result value if no case matches.. -- cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com May 21 '07 #10

### This discussion thread is closed

Replies have been disabled for this discussion. 