dont know what im doing wrong... array

This is a discussion on dont know what im doing wrong... array within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { double scores[5] = {1, 2, 3, 4, 5}; cout << scores ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    106

    dont know what im doing wrong... array

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double scores[5] = {1, 2, 3, 4, 5};
        cout << scores << " ";
        cin.get();
    }
    ok so i know this is very simple, but isnt this supposed to display
    1 2 3 4 5 ?

    i get 0x22ff20 when i compile im rather a noob but please help
    thanks
    CJ

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    scores is not 1, or 2, or 3, or 4, or 5. scores[0] is 1, so if you want to see 1 you should probably print scores[0]. Similarly, scores[1] is 2, so if you want to see that you need to print scores[1], and so on.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    106
    ok now ive got it thanks cj

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > cout << scores << " ";
    This only 'works' when scores is an array of chars.

    Eg.
    char scores[] = "hello";
    cout << scores;

    This is a convenience, since 99.99% of the time, it's exactly what you would want to happen.

    For every other data type, there is considerable flexibility in how you might choose to present the data (field widths, padding, etc).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    106
    ok i also figured out how to be able to display all the scores....
    im sure yall are not interested as youll already know all this but ill post it any way...
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int i;
        i = 0;
        int scores[5] = {1, 2, 3, 4, 5};
        for ( i = 0; i <= 5; i++ ){
        cout << scores[i] << " ";
    }
        cin.get();
    }

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    But the output is: 1 2 3 4 5 14 (yours may be different). There is still a small problem: why does it output 6, not 5 numbers?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    106
    sorry i fixed that problem it should be i < 5 instead of i <= 5 for the condition within the for loop... sorry about that.... heres the actual code....

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int i;
        i = 0;
        int scores[5] = {1, 2, 3, 4, 5};
        for ( i = 0; i < 5; i++ ){
        cout << scores[i] << " ";
    }
        cin.get();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Whats wrong with my array?
    By ammochck21 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2006, 01:11 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM

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