Join VipMods site Today!

"Its' free and it takes less than 1 minute!"

Announcement

Collapse
No announcement yet.

[C++] Initializing An Array To String

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [C++] Initializing An Array To String











    Program's Output
    Code:
    Hey! I'm C++owboy! What's your name?
    Basicman
    Well, Basicman, your name has 8 letters and is stored
    in an array of 15 bytes.
    Your initial is B.
    Here are the first 3 characters of my name: C++



  • #2

    nice writeup, There is also array of data like
    Code:
    char Data[] = { 'Hi', 'Hello', 'How are you', 'Im fine thanks for asking', 'goodbye', 'goodnight' }
    
    then to access these array's you would grab the char from the array you wanted with
    
    //get single string from array
    int ID = 2;
    cout << Data[ID] << endl;
    
    //loop all strings in array
    for ( int i = 0; i < 6;i++ ){
          cout << Data[i] << ',' << endl;
    }
    single string result
    prints out Hello.

    loop strings result
    prints our HI,Hello, How are you, Im fine thanks for asking, goodbye, goodnight.


    Comment

    • #3

      Originally posted by Luda View Post
      nice writeup, There is also array of data like
      Code:
      char Data[] = { 'Hi', 'Hello', 'How are you', 'Im fine thanks for asking', 'goodbye', 'goodnight' }
      
      then to access these array's you would grab the char from the array you wanted with
      
      //get single string from array
      int ID = 2;
      cout << Data[ID] << endl;
      
      //loop all strings in array
      for ( int i = 0; i < 6;i++ ){
            cout << Data[i] << ',' << endl;
      }
      single string result
      prints out Hello.

      loop strings result
      prints our HI,Hello, How are you, Im fine thanks for asking, goodbye, goodnight.

      Indeed it's pretty much the same thing ^_^
      You're just braking it up in arrays using the for loop make it initialize it at 0 and end at 6.
      So it will output 0, then 0+1 .. 1+1 ..2+1 .. and so on until the limit set (6).
      Using the ++
      I'll keep adding stuff like this maybe will get some people interested



      Comment

      • #4

        yea its just covers the basics, which would help anyone trying to learn. also php is not much different than C++ array.

        Code:
        $Data = array( 'Hi', 'Hello', 'How are you', 'Im fine thanks for asking', 'goodbye', 'goodnight' );
        
        for( $i = 0; $i < 6; ++$i ){
            echo $Data[$i]. ", <br>";
        only a variable in php is used with the $ symbol. and echo the results instead of cout, which you could also print( $Data[$i] ); to output the data to your screen.

        Comment

        • #5

          I'm pretty new to this stuff, why use 'char' and not 'String'?

          Comment

          • #6

            Originally posted by Luda View Post
            yea its just covers the basics, which would help anyone trying to learn. also php is not much different than C++ array.

            Code:
            $Data = array( 'Hi', 'Hello', 'How are you', 'Im fine thanks for asking', 'goodbye', 'goodnight' );
            
            for( $i = 0; $i < 6; ++$i ){
            echo $Data[$i]. ", <br>";
            only a variable in php is used with the $ symbol. and echo the results instead of cout, which you could also print( $Data[$i] ); to output the data to your screen.
            i see, ^_^

            Originally posted by PrettyChill View Post
            I'm pretty new to this stuff, why use 'char' and not 'String'?
            Depends on what you want to do!
            string is much better in my opinion.

            It's safer and if you can avoid using char* you should ^_^
            Most of the time char* is used for API functions that require it.
            But it's very easy to convert a string to a const char ..
            using std::string::c_str() to convert an std::string

            Char array maybe would be a better choice in some cases due to performance constraints or prohibition of dynamic memory usage in an embedded environment.



            Comment

            Working...
            X
            Back to Top