English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i've assigned a certain value on a variable... and then i get input from the user, how will i compare the variable value to the input???

2007-01-16 20:19:30 · 4 answers · asked by Sammy Baby 1 in Computers & Internet Programming & Design

4 answers

If you mean to compare a number entered via the keyboard which is stored in a string to a value store in say an int then you need to convert the number in the string.

int userval, aval;

aval = 10;
sscanf(buf, "%d", &userval);
if (aval == userval) {
do something........
}
/* Where buf is the string entered */
Note this assumes the value is at the start of the buffer!

You can scan the input from the user directly into the var :-
scanf("%d", &userval);

2007-01-16 21:54:43 · answer #1 · answered by Anonymous · 0 0

First, I like using enums to keep things straight. So, enum bandcolor { BC_BLACK = 0, BC_BROWN, BC_RED, BC_ORANGE, BC_YELLOW, BC_GREEN, BC_BLUE, BC_VIOLET, BC_PURPLE = BC_VIOLET, BC_GREY, BC_WHITE, BC_GOLD, BC_SILVER }; enum bandvalue { BV_ONES = 0, BV_TENTHS, BV_POWER }; But wait, you say, no global variables or no credit! Ahh, but you see, those are constants, not variables. You can also use enum bandcolor values as integers without a cast. Alright. They gave you some good hints, so I can go ahead and give you function prototypes for those: char *toLower(char **input); unsigned int colorVal(const char *input); double getResistorVal(unsigned int ones, unsigned int tenths, unsigned int power); Now, I can show you how to call the above functions. First, you'd prototype your variables like char input[3][71]; enum bandcolor colors[3]; And you'd use your variables like this, mystring = toLower(&mystring); colors[BV_ONES] = (enum bandcolor) colorVal((const char *) input[BV_ONES]); printf("Resistor value: %f\n", getResistorVal(colors[BV_ONES], colors[BV_TENTHS], colors[BV_POWER])); That's all I can tell you with the hints you've been given, but I can give you some more hints of my own: - If you include string.h, you can zero out the bandcolor array with memset(bandcolor, 0, sizeof(enum bandcolor) * 3); - You can read input safely with fscanf(stdin, "%s %s %s", input[BV_ONES], input[BV_TENTHS], input[BV_POWER]); - Since you know colors are only 6 characters long at most, you can use strncmp with 6 as the third argument. - you can impress your teacher by accepting multiple forms of input. For example, both "purple" and "violet" should return BC_VIOLET. Good luck!

2016-05-23 23:28:09 · answer #2 · answered by Anonymous · 0 0

A string in C is simply a pointer to characters, terminated by a null (zero) byte. All strcmp is doing is iterating over the strings comparing characters until it encounters the end of either string.

The following function is equivelent to the standard library function strlen, including the return value.

int compareStrings(const char* a, const char* b)
{
while (*a == *b++)
if (*a++ == 0)
return 0;
return (*(const unsigned char *) a - *(const unsigned char *) (b - 1));
}

2007-01-16 20:33:53 · answer #3 · answered by David B 3 · 0 0

You can use Array to Compaire.

2007-01-24 17:25:29 · answer #4 · answered by Rupp 2 · 0 0

fedest.com, questions and answers