array

so something very funny is going on right now.at least to me.
when i input a value,it should display:
1)the value you just entered in the array
2)the value in index 0
3)the value in index 50
4)the sum of the number in index 50 and the number 23

the problems:
1)on the first input of 20,it should display the value i just entered which is 20.but it displayed 0 instead.but inputs after the first will display the value which i just entered.
why is that?

2)this "value for the first index in the array",should just display the first value which i entered at the start,which can be found stored inside index 0.but as i kept inputting values,the first index in the array would keep adding 1 to itself.when it should just kept displaying the first value inputted.
why is that?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 #include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;



int main(int nNumberofArgs, char* pszArgs[])
{

    //
int timmy[] = {} ;
int bob;
int counter = 0;

for(;;)
{

cout<<"input numbers-";
cin>>bob;


timmy[counter] = bob;


cout<<endl;
cout<<"value in the array = "<<timmy[counter];
cout<<endl;
cout<<"value for the first index in the array = "<<timmy[0];
cout<<endl;
++counter;
cout<<"value inside index 50 = "<<timmy[50];
cout<<endl;
cout<<"value in index 50 plus 23 = "<<timmy[50]+23;
cout<<endl;
}
}



int timmy[] = {} ;

What's the size of this array? How many ints can you put in it? Does it have an index 0? Does it have an index 50?
The code will not compile.

Line 12:
 
    int timmy[] = {} ;

generates a compilation error:

[Error] zero-size array 'timmy'


You need to fix that before attempting to proceed any further.

Also - check your compiler settings. If it does not report an error, then you should change the settings so that it does.
Last edited on
Hello Repeater,

Keep in mind that arrays start at zero not one.
http://www.cplusplus.com/doc/tutorial/arrays/

Inside your for loop there is no way out. No way to end the for loop. Consider adding something like this:

1
2
3
		std::cin >> bob;

		if (bob == 0) break;


Your variable names may mean something to you but in the future choose better names that would be more understandable and more descriptive to others and yourself. This also makes you program easier to understand when you look at it six months from now.

Hope that helps,

Andy
Last edited on
Hello Repeater,


Hi Andy. Did you mean Trumpler?
thanks
Hello Repeater,

Yes I did. I coppied the wrong name. Sorry.

Andy
Topic archived. No new replies allowed.