Results 1 to 7 of 7

Thread: error with array

  1. #1
    Join Date
    Apr 2017
    Posts
    5

    Default error with array

    Hi guys so this part of code is to display 2d array . The first 2 arrays are working great but the last one which is the one post is not working and I can't find out why !! I will post the output also so u can see the problem . thx for ur help .



    Qt Code:
    1. int V [][12] = {0};
    2.  
    3. int colm = 5 , row = 12;
    4.  
    5. cout<<" V area is :\n"
    6. <<"----------------\n\n";
    7.  
    8. for( int i = 0; i < colm ; ++i ) {
    9.  
    10. for (int k = 0 ; k < row ; ++k) {
    11.  
    12. cout<< V [5][12];
    13. }
    14. cout<<endl;
    15. }
    16. cout<<"\n";
    17. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. V Area :
    2. ------------------
    3. 151515151515151515151515
    4. 151515151515151515151515
    5. 151515151515151515151515
    6. 151515151515151515151515
    7. 151515151515151515151515
    To copy to clipboard, switch view to plain text mode 
    Last edited by cooka; 16th May 2017 at 16:36.

  2. #2
    Join Date
    May 2011
    Posts
    77
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: error with array

    cout<< Parking_S [5][12];

    should be:

    cout<< Parking_S [i][k];

  3. #3
    Join Date
    Apr 2017
    Posts
    5

    Default Re: error with array

    I only got the first line of "0" then another weird output then last line "o" again??

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: error with array

    you are not initializing the whole array properly, you initialized only one dimension, the other dimension has garbage in it, which is what you see in the output.
    Qt Code:
    1. const int colm = 5 , row = 12;
    2. int Parking_S [colm][row] = {0};
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 16th May 2017 at 17:16.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Apr 2017
    Posts
    5

    Default Re: error with array

    not working its says undeclared row and colm . I don't understand the first 2 are written just same way and there are working . And I noticed something If I take only this piece of code to another c++ file and I compile it work great u see ??


    Added after 20 minutes:


    I solved it I had to use const int row and column and cout<< Parking_B [i][k] like u said
    Last edited by cooka; 16th May 2017 at 16:35.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: error with array

    I had to use const int row and column
    ah yes, missed that one, sorry.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Apr 2017
    Posts
    5

    Default Re: error with array

    NO prob dude it happens.

Similar Threads

  1. Array error: Don't know how to fix it.
    By robgeek in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2016, 02:54
  2. How to parse nested JSON Array and Object inside array
    By Radhika in forum General Programming
    Replies: 5
    Last Post: 17th December 2015, 08:42
  3. How to catch out of range array crashed error?
    By binary001 in forum Newbie
    Replies: 1
    Last Post: 10th October 2015, 18:12
  4. Replies: 0
    Last Post: 9th August 2011, 10:15
  5. Replies: 2
    Last Post: 12th November 2010, 14:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.