i have a list of words to put into an array but how do i do this
the order goes from 0 to 9 so black is zero
[black][brown][red][orange][yellow][green][blue][purple][grey][white]

Recommended Answers

All 34 Replies

Tell us what language you're trying to do this in. (ie. C or C++ strings?)

um i usually do c++ so c++

string myString[] = {"blah", "blah", "etc"};

what do i put string myString[here]

>what do i put string myString[here]
In C/C++, the first array subscript can have the value omitted when declaring an array. If you want, you can fill that in with the number of strings that you're adding. I'd recommend against it though, because it means more work if you want add more strings to the array.

hey...can anyone teach me c basics...like the while loop??thx...

hey...can anyone teach me c basics...like the while loop??thx...

what does that have to do with the rest of this thred pleas only comment to things already said in the form and if it doesnt really relate start a new thred. not mad or anything just wanted to share the rules of this site cuz i had to learn the hard way to

so would i still need the [] or could i just leave it out and put
string myString= {"blah", "blah", "etc"};
also do u have to have " or could u just do blah,blah

>so would i still need the []
Yes.

>also do u have to have "
Yes.

ok so now that i have my array all set up if i use cout and cin for the user if they type a part of the array will it now reconize it or do i have to use outher stuff like char or anything else to?

>i have my array all set up if i use cout and cin for the user if they type a part of the array will it now reconize it
I'm not quite sure what you mean. You could input data directly from cin into the array, but that doesn't sound like what you're trying to do...

>or do i have to use outher stuff like char or anything else to?
Show us an example of what you're trying to do.

ok here it is

#include <iostream>
using namespace std;
int main()
{
int,a;

a=0;

cout<<"enter color"<<endl;
cin>>a;

string myString[] = {"black", "brown", "red","orange", "yellow", 
"green","blue", "purple", "grey", "white"}

cout<<"the number for the color you entered is"<<a;

system ("pause");
}

No, you're doing it wrong. You first have to first grab the input and store it in a string, and then search through the array, looking for an element that is identical to what the user typed in.

And you might want to declare your variables at the start of the function, it's usually a better idea then declaring them as-you-go...

ok how do grab it and store it as a string do u have to use getchar()?

um ok i read it twice i dont see how that is supoes to help me pluss now im confused

If you had read my tutorial, I'm sure you would have seen how to read in a string:

string line;  // string with which to store the data
getline(cin, line); // grab a whole line of data

Beware that this produces undesired results when used with cin , so you'll have to clear the input buffer or stop using it altogether (which is the better option).

I do question your ability to write programs in C++, as this is a very trivial task. If this is causing you trouble, I suggest relearning what you have already learnt about C++, and getting a better understanding of the fundamentals.

It almost looks like what you want is an enumeration, based on your code. This would create a variable type, I don't know I'm bad at explaining it, and haven't used it much the format is.

enum color {black=1,blue,green,red};

the =1 is used to make it start the enumeration at 1 instead of the default of 0, though you could make it any number. This may or may not work better for your program as I'm not totally certain what you wish to do.

no i want black to start at zero um also this program is just a test for a bigger program i am making.

well then how do i do it with using cin and how do i do it without using cin

how do i put this
string myString[] = {"black", "brown", "red","orange", "yellow",
"green","blue", "purple", "grey", "white"}
together with this
string line; // string with which to store the datagetline(cin, line); // grab a whole line of data

>It almost looks like what you want is an enumeration, based on your code.
Nah, that won't work. Enums are only for making attractive numbers, they can't store strings. You're better off using a string array like I suggested previously.

>how do i put this
Now that you've got the user's input, you must search through the array, trying to find an element that's equal to 'line' (what the user typed in).

ok pretend the user types in red witch is equil to 2 now how do i tell it to search for 2 and i dont think u understood the last post i was trying to say how do u conect this string myString[] = {"black", "brown" "etc.."} with this. string line;/getline(cin, line);

>ok pretend the user types in red witch is equil to 2
Yes.

>now how do i tell it to search for 2
You don't. Remember, you're searching for what the user typed in, so you would search for 'line'.

>i dont think u understood the last post
I understood completely. You're asking how to search an array. Here is an example:

const int array[] = {5, 7, 4, 9, 2, 6};
const int number = 6;
const int arraySize = sizeof array / sizeof *array;
        
// search the array for "number"
for (int i=0; i < arraySize; i++) {
    if (array[i] == number) {
        cout << "Number found at position " << i << "." << endl;
        break;
    }
}

Now considering that you now know how to search ints, the rest should be easy, all you have to do is apply this to strings instead of integers.

commented: Great help - Aia +1

so is this close?

#include <iostream>
using namespace std;
int main()
{
int i;

i=0;


string myString[] = {"black", "brown", "red","orange", "yellow", 
"green","blue", "purple", "grey", "white}

string line= white;
string myStringSize= sizeof myString / sizeof *myString;

cout<<"enter color"<<endl;

for (string i=0; i < myStringSize; i++) {
          if   (string [ i ] == line) {
               cout << "the color you entered is " << i << "." << endl;
          getline(cin, myString);     
break;
return o;
          }
system ("pause");
}

Listen, you don't just replace every int with string . It's why this is called "programming", not "find examples and use my editor's Find&Replace to make it work for me". When you write something like this, I am sure you should be reviewing the basics of C/C++ rather than trying to write a program:

for (string i=0; i < myStringSize; i++) {

Have you ever learned loops? Did you know what std::string was before this thread?

but u told me to do that right here

all you have to do is apply this to strings instead of integers.

Not the whole thing! Just the array part.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.