1

I've searched the forums, and I can't seem to find an answer that is suitable for my specific problem (I tried Google as well). I seem to be having an issue comparing the strings ("Yes", "yes", "No", "no") correctly. I tried an if else originally, but I think a while loop is more efficient. Any suggestions?

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

double far = 0;
double cel = 0;
double userValue = 0;
double endResult = 0;
int choice;
char *decision = "";


int main() 
{  
 conversion();
 return 0;   
}

conversion() {
   printf("Please enter a 1 for Celsius to Fahrenheit conversion OR\n a 2 
   for 
   Fahrenheit to Celsius conversion\n");

scanf("%d", &choice);

   if(choice == 1) {
     printf("Please enter a value for Celsius.  Example 32 or 32.6\n");
     scanf("%lf", &userValue);
     endResult = (userValue * (9.0/5.0) + 32);
     printf("%lf\n", endResult);
     yesOrNo();
} 

else 

printf("Please enter a value for Fahrenheit.  Example 212 or 212.6\n");
scanf("%lf", &userValue);
endResult = (userValue -32) * (5.0/9.0);
printf("%lf\n", endResult);
yesOrNo();

}


yesOrNo() {

printf("Do you want to continue?    Enter Yes or No\n");
scanf(" %s", &decision);   

 while(decision == "Yes" || decision == "yes") {

    conversion();

 }

 exit(0);

}
  • don't use == comparator for strings Use `stricmp` instead – Abr001am Sep 10 '17 at 21:43
  • You need to use `strcmp` to compare the contents of strings, otherwise you're just comparing if they are at the same address or not. – Retired Ninja Sep 10 '17 at 21:44
  • http://en.cppreference.com/w/c/string/byte/strcmp – Alessandro Sep 10 '17 at 21:44
  • Note that you're going into recursion on each conversion: main() calls conversion(), conversion calls yesOrNo(), yesOrNo() calls conversion, and so on. You're risking a stack overflow exception. – SBS Sep 10 '17 at 21:47
  • `char *decision = "";`... `scanf(" %s", &decision);`-- this is wrong. `decision` is a pointer to an empty string. You need an array to hold the user input, e.g., `char decision[100];`. Then use `scanf("%99s", decision);` to get user input. – ad absurdum Sep 10 '17 at 21:47
  • Yep, that's how I solved the problem, although I didn't use that exact approach. It occurred to me moments after I posted. I think just posting the question made me think about what I was doing wrong. Thanks for the responses guys. – user3062174 Sep 10 '17 at 21:58

2 Answers2

2

C doesn't have strings. You have to use the function strcmp() to compare string literals and/or null-terminated character arrays.

decision == "Yes" 

should be

strcmp(decision,"Yes") == 0
Scooter
  • 6,802
  • 8
  • 41
  • 64
  • I figured out the problem moments after I posted. In fact, I couldn't even submit my response before it was closed. – user3062174 Sep 10 '17 at 21:54
2

You can't compare string literals using == operator you need to use strcasecmp()() or stricmp() function which are case insensitive.
If the strings are equal strcasecmp() and stricmp() return 0, if first argument is greater than second it returns positive number else negative.

coder
  • 12,832
  • 5
  • 39
  • 53
  • 2
    stricmp in this occurrence – Abr001am Sep 10 '17 at 21:45
  • The POSIX case-insensitive string comparison function is `strcasecmp()` declared in ``, IIRC – Jonathan Leffler Sep 10 '17 at 21:49
  • @Jonathan Leffler, you have right just checked, thank you very much!! – coder Sep 10 '17 at 21:50
  • `You can't compare string literals using ==` It does not matter if it is a literal or not. You can of course compare them using the `==` as well but you compare the pointers not the objects referenced by them – 0___________ Sep 11 '17 at 08:53
  • @ PeterJ_01, yes totally agree, what i meant is that by using `==` operator will not get the result that expected which is to compare the strings, thanks for comment !! – coder Sep 11 '17 at 08:56