2 byte char array needed c++?

i need 2 byte char type so I can store A+ in array
I need to save this data in size 10 array

A[0]= A+
A[1]=B+
A[2]=B-
A[3]=A-
A[4]=A+
A[5]=C+
A[6]=D+
A[7]=D-
A[8]=D+
A[9]=C-


I found out that wchar_t has 2 byte but don't know how to use this
I am using windows and compiler is Borland 5.02 I am learner so plz don't say anything about compiler I just need help in this.
Last edited on
> i need 2 byte char type so I can store A+ in array

Use std::string http://www.mochima.com/tutorials/strings.html

1
2
3
4
5
std::string A[10] = { "A+", "B+", /* ... */ } ;
A[3] = "A-" ;
std::string grade = "C+" ;
for( int i = 0 ; i < 10 ; ++i ) if( grade == A[i] ) { /* ... */ }
// etc. 

You could also do an array/vector of a custom object/class that you create that stores two chars or a string =p but probably more work than is needed since a string should get the job done.
in run time can i store this in A[0]=A+
> in run time can i store this in A[0]=A+

A[0] = "A+" ; // quoted string
You can define the array the following way

char A[10][3];

To store "A+" in this array you should use standard C function strcpy the following way

strcpy( A[0], "A+" );

To compare an element with a string literal you should use standard C function strcmp. For example

if ( strcmp( A[0], "A+" ) == 0 ) std::cout << "A[0] is equal to \"A+\"" << std::endl;
Last edited on
I found out that wchar_t has 2 byte but don't know how to use this

On Windows, wchar_t is for 2-byte character sets (i.e. 16-bit Unicode), not for storing two normal chars, e.g.

const char msg[] = "Hello world!"; // normal chars

const wchar_t wmsg[] = L"Hello wide world!"; // Unicode chars (L for long, I suppose.)

Here msg is 13 bytes long (inc. null terminator), whereas wmsg is 26 bytes long.

Note that wchar_t is usually 32-bits on Linux systems (as they use 32-bit Unicode.)

i need 2 byte char type so I can store A+ in array

Do you mean a fixed set of strings, or do you need to change them?

compiler is Borland 5.02

You're using a compiler from 1997? You might want to consider obtaining a new compiler (and IDE). It certaining won't understand the C++11 code that JLBorges posted. (Edit: after looking at the code properly and seeing it isn't C++11.)

Andy
Last edited on
You're using a compiler from 1997? You might want to consider obtaining a new compiler (and IDE). It certaining won't understand the C++11 code that JLBorges posted.

Last time I checked (with the freeware version 5.5) it doesn't fully support C++98. That's not to say it was all bad, it appeared to be heavily optimized for speed (at least on old machines).

On topic, I was going to post a suggestion to use an array of pointers to const char but then I refreshed and saw JLBorges' reply, so...
Topic archived. No new replies allowed.