Apr 25, 2012 at 1:44pm UTC
if i input character like "red" how i can assign its value like 1 or 2 or...
Apr 25, 2012 at 2:13pm UTC
I don't think I understand.
"red" is 3 characters (4 if you count the null character).
Last edited on Apr 25, 2012 at 2:14pm UTC
Apr 25, 2012 at 10:24pm UTC
thanks for reply...
value=(First color Second color)*10^Third color
if i input First color= red then its assign value=2
then input Second color= black its value =0
then input Third color= brown its value=1
so the value=20*10^1=200
how can i do this????
Apr 26, 2012 at 12:50am UTC
Raps, I have no idea. Nor any idea on what you're trying to accomplish. Though it is starting to look like a program for transistor values.
Apr 26, 2012 at 1:42am UTC
Why not use an enum construct e.g.
enum res_color(red=1.brown,black};//brown will have value 2 and black 3
Apr 26, 2012 at 1:47pm UTC
or, to make it more kile raps said:
enum res_color{black=0, brown=1, red=2}// all of them must be assigned in order for all of them to be convertable to numbers
Apr 26, 2012 at 3:00pm UTC
enum res_color{black=0, brown=1, red=2};
Because they are numbered in order starting from 0 you can write it as
enum res_color{black, brown, red};
(Some people might not like this because it's less explicit)
Apr 28, 2012 at 11:49am UTC
But how i can input the name of the color?
And print the value of the color what i input.
enum res_color{black=0, brown=1, red=2};
Apr 28, 2012 at 12:59pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#include <iostream>
std::string StrToLower(std::string s)
{
for (size_t i = 0; i<s.length();++i)
{
s[i] = tolower(s[i]);
}
return s;
}
int ColorToValue(std::string input) //assign values like this
{
enum res_colors {black,brown,red};
int value;
if (input=="red" )
{
value = red;
}
else if (input=="brown" )
{
value = brown;
}
else if (input=="black" )
{
value = black;
}
else
{
value = -1;
}
return value;
}
int main()
{
std::string input;
std::cout << "Enter first colour " ;
std::cin >> input;
input = StrToLower(input);
int firstvalue = ColorToValue(input);
if (firstvalue >= 0)
{
std::cout << "First colour is " << input << " which has value " << firstvalue<< std::endl;
}
else
{
std::cout << "You entered " << input << " which is unknown" << std::endl;
}
return 0;
}
Last edited on Apr 28, 2012 at 1:00pm UTC
Apr 29, 2012 at 4:26pm UTC
thanks for reply...
can you explain the use of <map>
@Pravesh Koirala
Apr 29, 2012 at 5:22pm UTC
map is an asocciative container. take this code for an example:
1 2 3 4 5 6 7 8 9 10 11
#include <map>
#include <iostream>
int main(){
map<string, double > price_kg;
price_kg["apple" ]=10.2;
price_kg["potato" ]=price["apple" ]*2;//price_kg["potato"]=20.4
price_kg["water" ]=price["potato" ]/10.0//price_kg["water"]=2.04
string a;
cin>>a;
cout<<"the price per kg of" <<a<<"is " <<price_kg[a];
return 0;}
Last edited on Apr 29, 2012 at 5:22pm UTC
Apr 30, 2012 at 11:58am UTC
thanks everyone for helping me to solve my problem...