c - Attaching a fix integer to a string -
im trying find way, when user inputs 2 colors, program automatically gives each integer value of choice.
example:
#include <stdio.h> #include <math.h> enum color { black = 0, brown = 1, red = 2, orange = 3, yellow = 4, green = 5, blue = 6, purple = 7, grey = 8, white = 9 } colors; int main(){ int sum; char first, second; printf("enter first 2 colors"); scanf("%s %s", &first, &second); sum = first + second printf("%d", colors); getch(); return 0; }
first things first, need end each of statements semicolon ;
, there's 1 missing after sum = ...
line.
just side note: when declaring enumerated types that, mean, starting 0
, increasing one, don't have make assignments. write enum color { black, brown, red, ... } colors;
, same.
as per answer @ comments, seem want print sum
out, instead of colors
. so, change printf
line this:
printf( "%d", sum );
another thing; cannot store sequence of characters inside single character. store sequence of characters, need memory space can hold sequence of characters. 1 way declare array of characters, this:
char first[15];
when that, computer automatically allocates 15 * sizeof( char )
big memory you, first 1 of them being first[0]
, last 1 being first[14]
. address of first first + 0
or first
, , address of last first + 14
.
other way manually allocate memory, won't that.
so, obtain 2 strings, code should following:
char first[15]; char second[15]; scanf( "%s %s", first, second ); // notice omitted ampersands (&) // since first , second addresses
now sad part: cannot desire that. variable names, enumerated type labels... labels, don't mean computer.
you can, however, desire in many, many ways. i'll offer easiest one:
// didn't beautiful... #include <string.h> ... int firstid; if ( strcmp( "black", first ) == 0 ) firstid = 0; else if ( strcmp( "brown", first ) == 0 ) firstid = 1; else if ( strcmp( "red", first ) == 0 ) firstid = 2; else if ( strcmp( "orange", first ) == 0 ) firstid = 3; else if ( strcmp( "yellow", first ) == 0 ) firstid = 4; else if ( strcmp( "green", first ) == 0 ) firstid = 5; else if ( strcmp( "blue", first ) == 0 ) firstid = 6; else if ( strcmp( "purple", first ) == 0 ) firstid = 7; else if ( strcmp( "grey", first ) == 0 ) firstid = 8; else if ( strcmp( "white", first ) == 0 ) firstid = 9; else firstid = -1;
one way or another, checking if string have matches string you'd have necessary. strcmp
function makes lexicographical comparison between 2 strings; returns negative value if first string less second, i.e. can found earlier in dictionary, positive if opposite, 0 if same.
now, have written first one, , looks horrible is... idea abstract thing out of our main
, , put inside function. let's call our function colorid
, , define follows:
int colorid( char colorname[] ){ if ( strcmp( "black", colorname ) == 0 ) return 0; else if ( strcmp( "brown", colorname ) == 0 ) return 1; else if ( strcmp( "red", colorname ) == 0 ) return 2; else if ( strcmp( "orange", colorname ) == 0 ) return 3; else if ( strcmp( "yellow", colorname ) == 0 ) return 4; else if ( strcmp( "green", colorname ) == 0 ) return 5; else if ( strcmp( "blue", colorname ) == 0 ) return 6; else if ( strcmp( "purple", colorname ) == 0 ) return 7; else if ( strcmp( "grey", colorname ) == 0 ) return 8; else if ( strcmp( "white", colorname ) == 0 ) return 9; else return -1; }
now, calling colorid( first );
, colorid( second );
should give individual values you'd them correspond.
i have removed enum
had, included string.h
#include <string.h>
(for strcmp
), added function colorid
above is, , changed main
following:
int main( ){ int sum; char first[15], second[15]; printf( "enter first 2 colors" ); scanf( "%14s %14s", &first, &second ); // 14's between % , s limit amount of characters // obtained , written sum = colorid( first ) + colorid( second ); printf( "%d", sum ); getch( ); return 0; }
and right now, think think desiring do.
Comments
Post a Comment