![]() |
Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
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> |
Re: Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
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))Code:
if (strcmp(input,"K")) { etc }That said, strcmp is unnecessary. In prinicple you only need to check the first character. So you can just use Code:
#define KELVIN 'K'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. |
Re: Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
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.
|
Re: Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
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";Code:
if('K' == toupper(a[0])) |
Re: Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
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.
|
Re: Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
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: c
Code: c
Does that help? |
Re: Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
oops, code should be
Code: c
|
Re: Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
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) |
Re: Stuck on using "if" or "strcmp" statements. Trying to write temp converter.
You will want to read this: A Tutorial on Pointers and Arrays in C.
|
| All times are GMT +5.5. The time now is 12:48. |