Stuck on using "if" or "strcmp" statements. Trying to write temp converter.

Discussion in 'C' started by pikagod, Aug 3, 2008.

  1. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I have been working on this a couple days, and been looking into this problem. I'm trying to use an if statement, but I want it to use an entire word, not one letter or symbol. I came across a document that said to use %s for strings. I don't know if that is correct, but that is what I've been using. the only problem is it said you can't use if statements with them. It said to use the strcmp command but this requires a ; at the end and therefore will always execute the next set of brackets. Here is the code so far, I've only written for Kelvin conversions.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char a [6];
    	printf("Is the Temperature Kelvin, Fahrenheit, or Celsius?\n");
    	scanf("%s", a);
    	strcmp (a=='k','K')
    		{
    			int b; 
    			char c [7];
    			printf("What is the Temperature?");
    			scanf("%d", &b);
    			printf("What do you want it converted to?");
    			scanf("%s", c);
    			strcmp (c=='c','C');
    				{
    					int d;
    					d = b - 273;
    					printf("%d", &d);
    					return 0;
    				}
    			strcmp (c=='f','F');
    				{
    					printf("This conversion is not possible, please convert to Celsius first and then to");
    					printf("Fahrenheit.");
    					return 0;
    				}
    		}
    }
    
    If someone could please help me that would be great :)
     
    Last edited by a moderator: Aug 3, 2008
  2. erislover

    erislover New Member

    Joined:
    Aug 5, 2008
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    The strcmp function returns a numeric value. This value is zero, unintuitively, if the strings match. So the first thing is simply,
    Code:
    if (strcmp(input, reference))
    { ; } // doesn't match
    else
    { ; } // matches
    But, this function compares null-terminated strings character-by-character. Thus:
    Code:
    if (strcmp(input,"K")) { etc }
    But, you wish to accept upper or lowercase characters, so you really want to alter the case of input first.

    That said, strcmp is unnecessary. In prinicple you only need to check the first character. So you can just use
    Code:
    #define KELVIN 'K'
    // ... later
    if (toupper(input[0]) == KELVIN)
    { ; } // convert kelvin
    else
    { ; } // explain that the Kelvin scale is superior
    With that said, you will probably run into two more problems. 1) A dangling '\n' character sitting around the next time you try to read the input. 2) You are assuming they will not type so many characters. Accepting single-character input is pretty simple, use getchar(), but accepting strings is tricky because you are setting up a buffer and hoping the user doesn't enter too many characters. It could lead to problems. I prefer to just use getchar() and fill the buffer up myself, since I know how big it is and can prevent any overflow problems.

    First step, I suggest creating a small program that simply echoes what the user enters. When that is mastered, this program will be very simple for you.
     
  3. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I made a program that echoes user input and got it working, the problem I have is when I tell it to check just the first letter and they write "Kelvin" out completely, it doesn't work, only if they type K.
     
  4. erislover

    erislover New Member

    Joined:
    Aug 5, 2008
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Well, what you have written is not comparing characters. strcmp compares two null-terminated strings you provide and returns 0 if they match. It is a function, it is not a replacement for an if statement. That is,
    Code:
    char * a = "some string";
    // later
    int result1 = strcmp("k", a); // good: both arguments are strings
    int result2 = strcmp('k', a); // bad: the first argument is a character
    int result3 = strcmp(a=='k','K'); // I don't even know what will happen here, but the arguments are a boolean/int and a character
    But if the user types out more than just a letter, yes, this will fail. This is why I suggested simply comparing the first characters as
    Code:
    if('K' == toupper(a[0]))
    { // Kelvin code
    }
    else
    { // non-Kelvin code
    }
    This compares just the first character.
     
  5. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    alright, well some of the stuff mentioned I've never heard of before o_O but I am learning still so I'm looking into them. My brother also commented that I could declare an integer and manipulate that based on the user input and use the integer for the if statements. Was thinking that'd work, so just throwing it out there as an idea.
     
  6. erislover

    erislover New Member

    Joined:
    Aug 5, 2008
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    You can compare characters directly. The question is how to get at them. I cannot give an exhaustive treatment of strings here, but basically, there are two important differences between character arrays and strings.

    First difference: string literals use ", and character literals use '. So 'k' is the character k, while "k" is a string literal.

    Second difference: strings end with the null character '\0'. (That's a zero.) If you think about this, it makes some sense (though there are potentially better ways to store strings.)

    Let's look at a character array versus a string.
    Code:
    char c_array[] = {'i', 'j', 'k'};
    char s_array[] = {'i', 'j', 'k', '\0'}; // could have declared as char s_array[] = "ijk";
    The difference of the null character at the end is important. It is how a function like strcmp knows when to stop. For instance:
    Code:
    int i = 0;
    while ('\0' != s_array[i])
    {
        putch(s_array[i]);
    }
    This will print the string, and because strings end with the null character, the code knows when to stop.

    Does that help?
     
  7. erislover

    erislover New Member

    Joined:
    Aug 5, 2008
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    oops, code should be
    Code:
    int i = 0;
    while ('\0' != s_array[i])
    {
         putch(s_array[i]); 
        ++i;
    }
    Sorry about that. I have not posted here long enough to edit.
     
  8. pikagod

    pikagod New Member

    Joined:
    Jul 30, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    that helps a bit, but I'm having trouble figuring out how to compare them once you get them. if I use the
    char s_array[] = {'i', 'j', 'k', '\0'}; or
    char s_array[] = "ijk";
    can I then use an if statements saying
    if (s_array[] = "Kelvin");
    Wasn't sure if that would work, and am lacking the compilers to test it on this computer. If that doesn't, then how do I use strings in an if statement. (sorry if this is similar to the begining question, I'm just not getting how to do this)
     
  9. erislover

    erislover New Member

    Joined:
    Aug 5, 2008
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice