New to C++ Stuck on Arrays. Help please!

This is a discussion on New to C++ Stuck on Arrays. Help please! within the C++ Programming forums, part of the General Programming Boards category; I am learning C++ and I am currently learning Arrays. However, I am stuck I type the following code: Code: ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    20

    New to C++ Stuck on Arrays. Help please!

    I am learning C++ and I am currently learning Arrays.
    However, I am stuck
    I type the following code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int examplearray[100];
    	cout << examplearray[2] << "\n";
    	system("pause");
    	return 0;
    }
    Shouldn't this just display the integer 2?

    The program compiles, however i get this message:
    Run-Time Check Failure #3 - The variable 'examplearray' is being used without being initialized.
    I press continue and the program displays:
    -858993460
    Press any key to continue...

  2. #2
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by PersianStyle View Post
    I am learning C++ and I am currently learning Arrays.
    However, I am stuck
    I type the following code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int examplearray[100];
    	cout << examplearray[2] << "\n";
    	system("pause");
    	return 0;
    }
    Shouldn't this just display the integer 2?

    The program compiles, however i get this message:
    Run-Time Check Failure #3 - The variable 'examplearray' is being used without being initialized.
    I press continue and the program displays:
    -858993460
    Press any key to continue...
    examplearray[2] will return element 3 of examplearray (0-99)

    alas you have not set it to any value yet, thus it fails irl.

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    What do you think this says?
    Code:
    int examplearray[100];

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Shouldn't this just display the integer 2?

    The program compiles, however i get this message:
    Run-Time Check Failure #3 - The variable 'examplearray' is being used without being initialized.
    I press continue and the program displays:
    -858993460
    Press any key to continue...
    [2] is not a value in the array, it is an index.
    If you want it to be two you will have to write
    examplearray[2] = 2;

    Arrays are a block of memory which hold a certain amount of data of a specific type. The general syntax works like this -

    type name[number of members];

    name[index] = x;

    or

    x = name[index];

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    20
    Yes, OK i figured it out on my own, I have to set the values of the array. So I fixed my code.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int examplearray[100];
    	int t;
    	for(t=0; t<100; t++)
    	{
    		examplearray[t] = t;
    	}
    	cout << examplearray[2];
    	system("pause");
    	return 0;
    }

  6. #6
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by PersianStyle View Post
    Yes, OK i figured it out on my own, I have to set the values of the array. So I fixed my code.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int examplearray[100];
    	int t;
    	for(t=0; t<100; t++)
    	{
    		examplearray[t] = t;
    	}
    	cout << examplearray[2];
    	system("pause");
    	return 0;
    }
    Be patient and don't start a thread the first thing you do

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck, arrays
    By NoobieGecko in forum C Programming
    Replies: 26
    Last Post: 02-17-2008, 12:14 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Stuck again: Arrays
    By bliss in forum C++ Programming
    Replies: 2
    Last Post: 05-13-2005, 01:37 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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