![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 16
| 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();
}
|
| dyelax is offline | |
| | #2 |
| Registered User Join Date: Oct 2009
Posts: 16
| 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. |
| dyelax is offline | |
| | #3 |
| Registered User Join Date: Apr 2008 Location: Australia
Posts: 26
| 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. |
| Tropod is offline | |
| | #4 |
| Registered User Join Date: Oct 2009
Posts: 16
| so how would i go about initializing this? and should i just make a for() statement for each case? |
| dyelax is offline | |
| | #5 |
| Registered User Join Date: Apr 2008 Location: Australia
Posts: 26
| 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. |
| Tropod is offline | |
| | #6 |
| Registered User Join Date: Oct 2009
Posts: 16
| ok thank you that helps a lot. also a completely unrelated question: what does <<endl; do? |
| dyelax is offline | |
| | #7 |
| Registered User Join Date: Oct 2009
Posts: 16
| 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 []' 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();
}
|
| dyelax is offline | |
| | #8 | ||
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Quote:
Quote:
Code: array[i] = i;
__________________ bit∙hub [bit-huhb] n. A source and destination for information. | ||
| bithub is offline | |
| | #9 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| Code: cout<< "this be yo' array dawg:\n"; cout<< ""<< array <<"\n";
__________________ Os iusti meditabitur sapientiam Et lingua eius loquetur indicium "There is nothing either good or bad, but thinking makes it so." (Shakespeare, Hamlet, Act II scene ii) http://www.myspace.com/whiteflags99 |
| whiteflags is offline | |
![]() |
| Tags |
| array, c++, numbers |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| pointers & arrays and realloc! | zesty | C Programming | 14 | 01-19-2008 04:24 PM |
| Parallel arrays, dynamic memory allocation, and sorting arrays | sbaker1313 | C Programming | 16 | 01-01-2008 04:07 PM |
| Dynamic (Numeric) Arrays | DavidB | C++ Programming | 5 | 05-03-2006 07:34 PM |
| Need Help With 3 Parallel Arrays Selction Sort | slickwilly440 | C++ Programming | 4 | 11-19-2005 10:47 PM |
| Crazy memory problem with arrays | fusikon | C++ Programming | 9 | 01-15-2003 09:24 PM |