Thread: Trying to create a 5 element integer array

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    Trying to create a 5 element integer array


    I cannot seem to get my program to run. I know it seems like a simple program but i am new to this.
    I am trying to:
    • create a 5 element integer array
    • cin 6 numbers
    • then cout that array to see what my program looks like at runtime.

    but keep getting errors. Here is what i have coded so far:
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    using std::cin;
    
    int main()
    {
    
    	//n is an array of 5 elements.
    	int firstString[5];
    
    		
    		cin >> int;
    		cout << firstString << endl;
    		cout << endl;
    
    		cout << "firstString is: " << firstString << endl;
    		cout << endl;
    
    	for (int i = 0; firstString[i] != '\0'; i++)
    		cout << firstString[i] << ' ';
    		cout << endl;
    	
      
    	return 0;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) "using namespace std" means "use it all", so there is no need to specifically 'use' std::cin.

    2) cin >> int is wrong. You cannot use reserved words like int as names for variables.

    3) cout << firstString. Though valid code, this will only print the adress of the array.

    4) firstString[i] != '\0'. An array of integers doesn't have a NULL terminator, only strings do. You have to manually test if you're out of bounds, like if(i >= NrOfElements).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    29
    Code:
    #include<iostream.h>
    #include<string>
    #define SIZE 5
    int main()
      {
      int firstString[SIZE]={0};  //store array of ints
      int i;
      for (i = 0; i<6 ; i++)   //take in six numbers
        cin >> firstString[i];
    
      cout << "firstString is: " << endl;  //output message
    
      for (i = 0; i < SIZE ; i++)     //output original array
        cout << firstString[i] ;
      getchar();
    return 0;
    }
    I don't know if this will help you?. I did this in c++builder in a console and the input i tried was 1 2 3 4 5 6 . The output was 1 2 3 4 5 . It doesn't store the last character entered because the original array was only 5 in size.
    Colin

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation STRING TIP

    The word "string" normally refers to a string of characters (text). It is an array of characters. Usually a series of numbers in an array is not called a string. Just a tip to help in communicating with other programmers.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>for (i = 0; i<6 ; i++) //take in six numbers
    >>It doesn't store the last character entered because the original array was only 5 in size.
    Yes it does (in your code at least). You inputted 6 numbers, and blew the array bounds, writing to memory you're not allowed to. Naughty naughty
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    Thanks

    I see where my errors are (it helped that they were pointed out). You all were a great help, especially you Colin.

    Thanks!
    Last edited by nadeni0119; 03-26-2003 at 08:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  2. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 09:42 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Representing a Large Integer with an Array
    By random_accident in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2005, 12:23 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM