Thread: Array help needed

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    13

    Question Array help needed

    I started taking a C++ class two weeks ago, so I'm relatively new to this. I'm trying to grasp the fundamentals of arrays, but I'm absolutely stuck with this problem.

    I'm trying to write a program that has a declaration in main() to store the string "Vacation is near"
    into an array named 'message'. It should have a function call to 'display()' that accepts 'message' in a parameter
    named 'strng' and then display the first eight elements (by 'elements', does that include spaces?) of the 'message' array.

    I'm not exactly sure as to what to put inside the 'for' loop.
    If you could help clarify, I'd appreciate any help!

    My program is here:

    Code:
    #include <iostream>
    using namespace std;
    
    // function prototype
    void display(char[]);
    
    int main()
    {
        char message[] = "Vacation is near";
        
        // call display() function
        display (message);
        
        system ("pause");
        return 0;
    }
    
    void display(char strng[])
    {
         for (int i = 0; i < 8; i++)
         {
         // I'm completely confused as to what to put here?    
         }
         return;
    }
    Last edited by ieatcalculus; 07-24-2010 at 11:42 AM.

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    Quote Originally Posted by ieatcalculus View Post
    // I'm completely confused as to what to put here?
    In that case you need to read about C-style strings bit more to realize that each character of the string can be accessed using an index, as in strng[3] or strng[4] etc.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    Wow that was a fast edit! I fixed it; thank you!

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by ieatcalculus View Post
    named 'strng' and then display the first eight elements (by 'elements', does that include spaces?) of the 'message' array.
    Yep, "elements" includes spaces in my book.

    You may be overthinking this. Each array element is a character -
    message[0] is 'V'
    message[1] is 'a'
    message[2] is 'c'
    and so on.

    So you just want to print each character up to the 8th.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    Regarding the spacing, why is it that
    Code:
         for (int i = 0; i <= 10; i++)
         {
             cout << strng[i];
         }

    outputs the same message as
    Code:
         for (int i = 0; i <= 11; i++)
         {
             cout << strng[i];
         }
    ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Probably because the trailing space for the latter is indistinguishable. One thing you can do is to print say, a quote before starting to print the string, and then print a quote to end.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    That makes sense.

    Hypothetically speaking, for the for loop, what would I replace
    Code:
     for (int i = 0; i <= 8; i++)
    with if I wanted to print out the entire string?
    (That is, do I necessarily have to determine the string size first?)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ieatcalculus
    Hypothetically speaking, for the for loop, what would I replace (...) with if I wanted to print out the entire string?
    You would just replace the entire loop with:
    Code:
    cout << strng;
    Quote Originally Posted by ieatcalculus
    (That is, do I necessarily have to determine the string size first?)
    There is no need to determine the string size even if you did have to use a loop yourself since you would just need to terminate looping when the null character is detected.

    By the way, why are you not using std::string?
    Last edited by laserlight; 07-24-2010 at 12:11 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    Oh wow, I really did overcomplicate this! Thanks for your help (:

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by ieatcalculus View Post
    That makes sense.

    Hypothetically speaking, for the for loop, what would I replace
    Code:
     for (int i = 0; i <= 8; i++)
    with if I wanted to print out the entire string?
    (That is, do I necessarily have to determine the string size first?)
    for (int i = 0; i < strlen(message); i++)

    Or loop and check for the null terminator. C strings are terminated by a 0-character.

    Code:
    int i = 0;
    while (message[i] != 0) // or != '\0'
       cout message[i];
       ++i;
    edited to add.. or yes, just use cout << message;
    Last edited by smokeyangel; 07-24-2010 at 12:15 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>void display(char[]);
    Really should be
    void display(char strng[]);
    SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM