problem

if i input character like "red" how i can assign its value like 1 or 2 or...
I don't think I understand.

"red" is 3 characters (4 if you count the null character).
Last edited on
@raps

Is this what you were meaning??
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
// test.cpp : main project file.

#include <iostream>
#include <string>

using namespace std;

int main()
{
string colors[]={"red","green","blue","violet"};
int pick_color=-1;
string choose;
do
{
	cout << "Enter the color red, green blue or violet..  Enter anything else to end." << endl << endl;
	cin >> choose;
	for(int i=0;i<4;i++)
		if(choose == colors[i])
		{
			pick_color=i+1;
			break;
		}
		else
			pick_color=0;
	cout << "Pick_color = " << pick_color << endl;

} while (pick_color !=0);


cout << "Program terminated" << endl;
return 0;
}
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????
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.
Why not use an enum construct e.g.
enum res_color(red=1.brown,black};//brown will have value 2 and black 3
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
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)
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};
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
Or, you can use a map..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <map>
#include <string>
using namespace std;

map<string,int> code;

int main()
{
   code["red"] = 2;
   code["brown"] = 1;
   code["black"] = 0;

   string first,second,third;
   cin >> first >> second >> third;
   cout << first << "=" << code[first] << endl;
   cout << second << "=" << code[second] << endl;
   cout << third << "=" << code[third] << endl;
}
thanks for reply...
can you explain the use of <map>
@Pravesh Koirala

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
thanks everyone for helping me to solve my problem...
Topic archived. No new replies allowed.