Jump to content

array of character type

- - - - -

  • Please log in to reply
11 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi :)

Please don't mind my asking the following question(s).

I was under the impression that the character arrays work as regular arrays such as of type int, float etc. I was thinking that one can put more than one digit (e.g. 12, 13, etc.) or more than one letter (such as ab, bb, ccc, etc.).

Please have a look on this ASCII table.

The character values given for decimal values from "0" to "32" consist of more than one character. For example, for decimal value "0" the character value given is "NUL", and for decimal value "9" we have character value "TAB".

I was wrong. An array element of character type can contain only one digit or character. The character values which consist of more than character such as "NUL" or "TAB" stand for particular function or signs. For example, "TAB" is a set of white spaces, and "Space" (decimal value is 32) is single white space.

Please correct me if I'm wrong without confusing me more!:p


// learning_character_strings.cpp

// learning how character string work


#include <iostream>

#include <cstdlib>

#include <cstring>


using namespace std;


int main()

{

        int i;

        const int C = 5;


        char string[C];


        for (i=0; i<C; i++)

        {

                cout << "enter string #" << (i+1) << ": ";

                cin >> string[i];

        }


        cout << "\n\n";


        for (i=0; i<C; i++)

        {


                cout << "string #" << (i+1) << " is: " << string[i] << endl;

        }


        system("pause");

        return 0;


}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I am very unsure of what you are asking (I see no question actually)

Jackson said:

For example, "TAB" is a set of white spaces, and "Space" (decimal value is 32) is single white space.

Tab is a single character, it just represents more than one space (a tab stop.)

Quote

The character values which consist of more than character such as "NUL" or "TAB" stand for particular function or signs.
What do you mean function or sign? Each ASCII character has a name, NUL is just a name.

I am also unsure of what your code is supposed to do (it seems fishy to do it like that, and it just displays individual ASCII characters, I am not sure what you were trying to show.)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Alexander.

I am myself not very sure what I was doing. Sorry, if it was too silly.

1: The following code is the output for the code given at the bottom. You see it doesn't take in the value for string #4. Why? I think what happens is this: string #3 extracts the 'C' from "CK" and 'K' gets left behind in the input stream; then string #4 takes in the 'K' from the input stream.


enter string #1: J

enter string #2: A

enter string #3: CK

enter string #4:


string #1 is: J

string #2 is: A

string #3 is: C

string #4 is: K

Press any key to continue . . .


2: The decimal value "7" on ASCII table stands for BEL (which I think produces the sound of a bell). How do I use that "BEL"?

I used this code but it doesn't ring.

for (i=0; i<C; i++)

        {


                cout << "string #" << (i+1) << " is: " << string[i] << endl;

        }


        cout << '7' << endl;




// learning_character_strings.cpp

// learning how character string work


#include <iostream>

#include <cstdlib>

#include <cstring>


using namespace std;


int main()

{

        int i;

        const int C = 4;


        char string[C];


        for (i=0; i<C; i++)

        {

                cout << "enter string #" << (i+1) << ": ";

                cin >> string[i];

        }


        cout << "\n\n";


        for (i=0; i<C; i++)

        {


                cout << "string #" << (i+1) << " is: " << string[i] << endl;

        }


        system("pause");

        return 0;


}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Ah, that is better. These questions are very interesting, not many people even try to understand them so I welcome them.

Quote

enter string #1: J
enter string #2: A
enter string #3: CK
enter string #4:
Remember the input buffer?

cin sees that you are placing input in to a char element (cin >> string[i], i.e. one of the chars inside your char array), so it only accepts one character at a time.

When you type in CK, it takes C, and then leaves K in the input buffer, which plugs directly in to "#4" when it reaches it.

Quote

I used this code but it doesn't ring.
Remember, that is ASCII seven you are typing out, not integer 7.Know ASCII is just a pile of numbers (0..127) that represent characters, so '7' is actually 55 in integer, 55 == '7' for example.

Also remember, you can assume char's are one byte integers, so you can freely assign integers to them:

//ask more if it is unclear
cout << 7; //wrong, cin likely adds +48 to the ASCII to make it '7'!
cout << (char)7 //correct
Remember, cout can detect what you are entering. It assumes 7 is actually ascii '7' (and does this conversion for you, try not to confuse this) so you must force it to assume it is a character instead.

This will tell cout to display ASCII 7 (integer 7 cast to a character rather than converted to a character like '7')

In C you can just do this:
printf("%c", 7);
or
putc(7);
It doesn't hide the ASCII from you.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Alexander. Sorry, if I sound so dense. I'm still struggling to hear that bell sound! It doesn't work for me.

On the ASCII table decimal 7 stands for ASCII "BEL".


#include <iostream>

#include <cstdlib>

#include <cstring>


using namespace std;


int main()

{

        int i;

        const int C = 4;


        char string[C];


        for (i=0; i<C; i++)

        {

                cout << "enter string #" << (i+1) << ": ";

                cin >> string[i];

        }


        cout << "\n\n";


        for (i=0; i<C; i++)

        {


                cout << "string #" << (i+1) << " is: " << string[i] << endl;

        }


        [SIZE="3"][COLOR="red"]cout << static_cast<char>(int 7) << endl;[/COLOR]

[/SIZE]

        system("pause");

        return 0;


}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#6
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Hi Jackson! This produces bell sound

 

    char c = 7;

    cout << c << endl;


Also in your above code i just replaced the 'int' in following line i.e.


        cout << static_cast<char>(7) << endl;


also produces bell, i just verified using Visual studio on windows

Edited by fayyazlodhi, 11 June 2011 - 11:35 AM.
Mistake fixed


#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I apologize, my reply got lost when the server was being maintained.

Your static cast has (int) 7 (which I believe you were trying to do)

7 is already an integer, so you do not need to cast, just <char>(7)

Also, even though the system bell is often within the motherboard (it should be plugged in to the same area as the lights that light up the HDD indicator) although modern systems often emulate it. You can go in to system volume (Start menu -> All programs -> Accessories -> Entertainment -> Sound Volume) and hit Options -> Advanced I believe to access the system bell (beep)'s volume if it is muted or low.

You can then simply put it in a loop:
for(int i = 0; i < 20; i++) {
   cout << (char)7; //or static cast, we need to cast to (char)7 as cout will otherwise translate it to '7' (such as adding +48 to the ASCII to make it the ASCII number)
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks a lot, Fayyaz, Alexander.

I'm learning C-strings these days. I always include the header "#include <cstring>" but there is also another header "#include <string>"? What's the difference? Is it that the latter is for the C++ strings? Please let me know. Thank you.

Best regards
Jackson
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#9
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
cstring is basically header file string.h containing many standard string utility functions such as strcpy, strcat, strcmp to name a few

"string" alone is a C++ string which is basically part of Standard Template Library. Without going into complexity of what STL or class is, since you are at an early stage, you can think of this c++ string as a new data type with many additional features such as string1 = string2 (assignment), if(str1 == str2) (comparison) and much more.

#10
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Okay. Thank you for the information. You have been very helpful and I have learned a lot from you!

I'm surprised that I was able to input values for all the array elements with one cin. But it seems it would ONLY work for arrays of char type. It wouldn't work for an array of int type. Why I think so? An element of array of char type could obvious hold only one digit (such as '1', '2', '3', etc.) or one character (such as 'a', 'A', 'c', etc.); in other words the 'size' of the element in this case is fixed. But for an element of array type int type the 'size' is not fixed. So, probably, when we input values for an array of char type, when we have entered one character with cin, we have already entered second element, and so on.

Best wishes
Jackson
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#11
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
You are welcome.

Your understanding is mostly correct since cin inputs only when enter is pressed. Whatever is in the buffer at that time is attempted to be copied.

But one important point is, even for integer array if you enter number with spaces in between, it would input them all once a single enter is pressed.

i.e.


    int n[5];


    for(int i=0; i<5; i++)

        cin >> n[i];


Normally this code would take five inputs one after the other if i press "1 <enter> 2 <enter> 3 <enter>... up to five".
However, if i write "1 2 3 4 5" <enter> (note the spaces in between), the loop terminates after the first enter.

#12
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you. I understand your point.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users