Sainer overly newbish question

This is a discussion on Sainer overly newbish question within the C++ Programming forums, part of the General Programming Boards category; Ok, I have a problem getting an array or string to print. Using cout gives me the memory address of ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Saner overly newbish question

    Ok, I have a problem getting an array or string to print. Using cout gives me the memory address of the beginning of the string or array.


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
         int main(void){
            int iArray[5] = {5,3,2,1,8};
              
           cout<< iArray;
         
         return 0;
     
        }
    I want a command that outputs the array as a whole i.e. 53218 not 0x86fdd0. Hopefully not using a loop.
    I appologize for the lack of clarity in my previous post.
    Last edited by parrellel; 07-09-2003 at 09:02 PM.

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Right now, you're printing the pointer to the array. You cannot print the whole array with a standard function/something. You'll have to code it yourself with a loop:
    Code:
    // prints the whole array, with no delimeter
    for (int i = 0; i < 5; i++)
      cout << iArray[i];

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If you don't want to see the loop or code one every time you print your array, you could write a function that accepts the array and the number of members in it and prints it the way you want it. Something like:
    Code:
    PrintArray(iArray, 5);
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. sad overly newbish question on output
    By parrellel in forum C++ Programming
    Replies: 10
    Last Post: 07-07-2003, 10:26 AM
  3. Newbish Question On String Extract
    By JJB in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2003, 09:46 AM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Math Question, hard
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 12-08-2001, 10:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21