Is it possible to store * and # in an array? I thought I could do this through the data type char. On top of that I was wondering if I could check the array elements to see if the element equaled that character, but I don't think that's possible.

Basically I have the following trial if someone could help me. I am still writing the pseudo code for my real program but I am trying to make sure I understand this because otherwise my pseudo code is all wrong and I am getting nowhere.

My code is the following:

char play[3];         // here i am trying to declare the array
play[2] = "*";       // set the 3rd element equal to the *

if(play[2] == "*")   // if it equals * then display
cout << "That seat is taken." << endl;

Dani AI

Generated

This is a classic C++ distinction: characters are single values and use single quotes, while double quotes produce string literals. Storing '*' and '#' in a char array is fine — those are ordinary character values. As pointed out, use a character literal rather than a string literal; 's comment explains the single- vs double-quote difference. later confirmed the change fixed the immediate problem.

Practical points to avoid future surprises: pick the right container (raw char[] requires correct sizing and, if used as a C-string, a null terminator). Indexing is zero-based so watch bounds. If the data represents text or a seat map, std::string or std::vector<char> usually makes life easier than manual char arrays. For simple occupied/unoccupied flags, a bool array or an enum is clearer than hidden marker characters. Comparing an element to a character literal is the normal way to test a stored symbol.

Typical compiler diagnostics are informative: messages about converting const char* to char or about pointer/integer assignments indicate a string literal was used where a char was expected. For authoritative background, see the C++ character-literal and basic_string references: character literal and std::string (basic_string).

Recommended Answers

All 4 Replies

i dont think you should assign a char inside double quotes. Try

play[2] = '*'
if (play[2] == '*')

i dont think you should assign a char inside double quotes. Try

play[2] = '*'
if (play[2] == '*')

thanks that worked ;-D

'a' == char
"a" == string

'a' == char
"a" == string

Oh I see. Thank you as well.

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.