How to put string data into an array....

How can I make an array of string's ? I am trying to create a resistor color code chart (very, very simple for now)....Once Ive created the program. I want to re-do the program using things like objects and classes, vectors, etc.....EX....

You have:
Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Gray, White, Gold, Silver, No Band

Band 1: 0 through 9
Band 2: 0 through 9
Band 3: multiples of 10 (multiplier) ie......1,..10..100...1,000....10,000....etc...
Band 4: Tolerance Band: Gold = 5% Silver = 10% No Band = 20%

I have used arrays many times before, but I havent used array's for colors or objects, just interger's....Ive heard that using vector's is an easier method, but I have never used vectors before. I also have to take into account that once I have passed 999 ohms that Ill have to start using engineering notation in order to properly classify the ohms value...ex =
Brown = 1, Black = 0, Red = 100 (which makes 1000 or 1K ohm value).....


I want the user to enter the colors, then the program to output the resistor's ohm value....
Last edited on

Array of strings example:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
	std::string Bands[13] = { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", 
		"Violet", "Gray", "White", "Gold", "Silver", "" };
	std::cout << Bands[1];	// show Brown
	return 0;
}
cout << Bands[1] is starting at the index spot of the bands array correct ? So as an example, if I wanted to navigate through the Bands array. I could write it as:

cout << Bands[2] = Red
cout << Bands[3] = Orange

and so forth.....

I wonder if i can assign a numerical value to the colors without creating 4 more array's....I could create 4 different arrays. That's not a problem.....Just seeing what I can do..I appreciate the help, and Ill stop by sometime with what I got.....

http://s15.postimg.org/fh9af4jbf/IMG_20150504_053227.jpg

image of Resistor Color Code Chart ^
Last edited on
Hi,

I am not sure if i understood you correctly, but you can use enum if you want to assign numerical value
1
2
3
4
5
6
7
8
9
10
// declaration
enum colors { Black = 0, Brown, ... };
colors color = Brown;
...
//print color
switch( color  ){
 Black: cout<<"black"<<endl; break;
 Brown: cout<<"Brown"<<endl; break;
}
As the color names are all constant values I would very probably just use a const char* array in this case. But if you want to use std::vector to help you learn how to use it, then it's

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <vector>

...

// Use C++11 initializer list approach to intialize array
const std::vector<std::string> value_colors = { "Black" , "Brown" , "Red" ,
                                                "Orange", "Yellow", "Green",
                                                "Blue"  , "Violet", "Gray" ,
                                                "White" };


I've assumed that the colors will be split into two sets, those for the values 0..9 and those for the tolerances, as they are distinct sets. Esp. if you're wanting to map color to value.

C++11 (and newer) also provides std::array which is kind of a stripped down version of std::vector for fixed sized arrays, which is a good fit for your problem.

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <array>

...

// std::array needs to be given size
const std::array<std::string, 10> value_colors = { "Black" , "Brown" , "Red" ,
                                                   "Orange", "Yellow", "Green",
                                                    "Blue"  , "Violet", "Gray" ,
                                                   "White" };



cout << Bands[1] is starting at the index spot of the bands array correct ?

(If I understand you correctly...) Array indices start at 0 not 1, so the first 4 bands will have indices 0, 1, 2, and 3.

I could write it as:

cout << Bands[2] = Red
cout << Bands[3] = Orange

This isn't valid C++? (Not sure what you're trying to do here.)

I wonder if i can assign a numerical value to the colors without creating 4 more array's

Only the tolerance band should need an additional array to define its values.

I want the user to enter the colors, then the program to output the resistor's ohm value....

If you want to go from colors to value using arrays, then you'll need to scan the arrays to find the values. This is why I split the colors into two sets.

In the basic case you can just use a loop to check the elements of the color array for the one you want, but you could also use the std::find algorithm.
http://www.cplusplus.com/reference/algorithm/find/

If you're writing your program for educational purposes, you might want to consider using std::map to handle color -> value.
http://www.cplusplus.com/reference/map/map/

(Using std::map for this problem feels a bit heavy handed to me for some or other reason.)

Andy

PS Wikipedia.org has a more readable Resistor Color Code Chart
http://en.wikipedia.org/wiki/Electronic_color_code#Resistor_color-coding
Last edited on

If you were going down the route of using a class, you could do something like this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

class Data
{
private:
	std::string colour;
	int value;
	double multiplier;
public:
	Data(std::string col, int val, double mult) {
		colour = col;
		value = val;
		multiplier = mult;
	}
	// getters if/when we need them.
	std::string getColour() { return colour; }
	int getValue() { return value; }
	double getMultiplier() { return multiplier; }
};


Then in your main you could create the objects and populate the data:

1
2
3
4
5
6
7
8

	Data Resisters[12] = {
		{ "Black", 0, 1 }, { "Brown", 1, 10 }, { "Red", 2, 100 },
		{ "Orange", 3, 1000 }, { "Yellow", 4, 10000 }, { "Green", 5, 100000 },
		{ "Blue", 6, 1000000 }, { "Violet", 7, 10000000 }, { "Grey", 8, 100000000 },
		{ "White", 9, 1000000000 }, { "Gold", 0, 0.1 }, { "Silver", 0, 0.01 }
	};


You could just as easy use a structure, difference being that everything is public.
Topic archived. No new replies allowed.