Thread: arrays

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    38

    arrays

    wow im glad i joined this forum haha. ok so my newest problem is with arrays.

    #1. somehow the array im getting is -8,5,8,9,9,3,4,6,0 instead of 0-6. i dont even know how its getting 9 slots.

    #2. i dont know how to make it so you can pick 1 slot that you want to view. i gave it my best shot but the way I have it my program crashed when you try to choose a slot.

    thank you. heres my code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	int a;
    	int b;
    	int x;
    	int array [7];
    	int *z;
    	z = array;
    
    	cout<< "press 1 to see the whole array, press 2 to pick 1 slot.\n";
    	cin>> a;
    
    	switch (a) {
    		case 1:
    			cout<< "this be yo' array dawg:\n";
    	for (x = 0; x < 7; x++) {
    			cout<< array [x];
    			break;
    		case 2:
    			cout<< "which slot would you like to view? 0-6\n";
    			cin>> b;
    			cout<< array [*z];
    			break;
    		default:
    			cout<< "NO\n";
    			break;
    	}
    	}
    	cin.get();
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    wow now that i look at it that last part is screwed up cuz i dont even use b... i dont really know how i would though i either get a crash with the pointer or it returns the whole array with b.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    Your array has not been initialized, so basically it just contains garbage.
    Reason it's crashing when selecting slot goes to what I've just mentioned. Selecting a slot makes it try and access something that doesn't exist. You need to initialize each member in the array.

    Might want to look at your for() statement too, not sure that's going to work properly in its current set-up, since it spread across more than 1 case statement.
    Last edited by Tropod; 10-14-2009 at 02:34 AM.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    so how would i go about initializing this? and should i just make a for() statement for each case?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    To initialise this array, can do something like;
    Code:
    for (int i = 0;i<7;i++)
    {
    	int array [i] = i;
    }
    //this essentially initialises each variable in the array to something, in this case 0-7.

    As for the for() statement in the switch();
    Once it reaches the break; it's going to exit, even though the end of the for() loop is beyond the case: statement. So it may cause undesirable results.
    Move the "}" to just before break; in the case 1: where the for() should all be encompassed.

    Code:
    	switch (a)
    	{
    		case 1:
    			cout<< "this be yo' array dawg:\n";
    			for (x = 0; x < 7; x++)
    			{
    				cout<< array [x]<<endl;
    			} //<<<here should be closing brace of for() loop.
    			break;

    This makes sure the for() loop is properly executed before hitting the break, otherwise it won't loop properly.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    ok thank you that helps a lot. also a completely unrelated question: what does <<endl; do?

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    ok wow im having a lot of problems with this code. i tried what you said to initialize it but im getting 3 errors:
    Code:
    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0
    error C2440: 'initializing' : cannot convert from 'int' to 'int []'
    and when i take that part out to try to use the pointer to view 1 section of it it just shows the whole thing.

    heres my code now:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	int a;
    	int array [7];
    	int *z;
    	z = array;
    
    	for (int i = 0; i < 7; i++)
    	{
    		int array [i] = i;
    	}
    
    	cout<< "press 1 to see the whole array, press 2 to pick one slot.\n";
    	cin>> a;
    
    	switch (a) {
    		case 1:
    			cout<< "this be yo' array dawg:\n";
    			cout<< ""<< array <<"\n";
    			break;
    		case 2:
    			cout<< "which slot would you like to view? 0-6\n";
    			cin>> *z;
    			cout<< array [*z];
    			break;
    		default:
    			cout<< "NO\n";
    			break;
    	}
    	cin.get();
    }

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by dyelax View Post
    ok thank you that helps a lot. also a completely unrelated question: what does <<endl; do?
    endl adds a newline character and flushes the output stream.

    Quote Originally Posted by dyelax View Post
    ok wow im having a lot of problems with this code. i tried what you said to initialize it but im getting 3 errors:
    Code:
    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0
    error C2440: 'initializing' : cannot convert from 'int' to 'int []'
    and when i take that part out to try to use the pointer to view 1 section of it it just shows the whole thing.

    heres my code now:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	int a;
    	int array [7];
    	int *z;
    	z = array;
    
    	for (int i = 0; i < 7; i++)
    	{
    		int array [i] = i;
    	}
    
    	cout<< "press 1 to see the whole array, press 2 to pick one slot.\n";
    	cin>> a;
    
    	switch (a) {
    		case 1:
    			cout<< "this be yo' array dawg:\n";
    			cout<< ""<< array <<"\n";
    			break;
    		case 2:
    			cout<< "which slot would you like to view? 0-6\n";
    			cin>> *z;
    			cout<< array [*z];
    			break;
    		default:
    			cout<< "NO\n";
    			break;
    	}
    	cin.get();
    }
    Your for loop is attempting to create a new array every iteration. Did you mean to do this instead?
    Code:
    array[i] = i;
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    			cout<< "this be yo' array dawg:\n";
    			cout<< ""<< array <<"\n";
    More like, this be yo' memory address broseph. Printing an array name does not print the contents of the array. Access each item individually to print.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

Tags for this Thread