Thread: problem in array

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    18

    problem in array

    Code:
    #include <iostream>#include <string>
    using namespace std;
    
    
    
    
    int main()
    {
        
    
    
    int a[3]={0,1,2};
    
    
    		for(int i=0; i<=5 ; i++) ;
    		{
    		cout<<a[1];}
    	}
    expected output:11111 and what i got is:1
    istead of printing five 1,s it producing single 1.help me to figure out what is going on.thank you in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have an extra semi-colon at your for loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Without the extraneous semi-colon, it will produce six 1's, not five.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  2. Array problem. Can you find the problem with this?
    By Aitra in forum C Programming
    Replies: 21
    Last Post: 01-03-2013, 10:33 AM
  3. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  4. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  5. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM