cin

This is a discussion on cin within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main ( void ) { int a=0; int array[20]; int i = 0; ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question cin

    Code:
    #include <iostream>
    using namespace std;
    
    int main ( void )
    
    {
    
    	int a=0;
    	int array[20];
    	int i = 0;
    
    	cout << "Enter up to 20 numbers" << endl;
    
    	while ((cin >> array[i]) !=EOF)
    
    	{
    		array[20] = array[i++];
    
    		if (i == 20)
    
    			break;
    	}
    
    		for(a=0;a<=16;a+=4)
    
    	{
    
    		cout << array[a] << array[a+1] << array[a+2] << array[a+3] << endl;
    
    	}
    
    	return 0;
    
    }
    Why is cin causing errors in this code?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    int array[20];

    array[20] = array[i++];
    If you declare an array with 20 elements, they range from 0 to 19, not 20.
    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
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Unhappy

    Originally posted by Magos
    If you declare an array with 20 elements, they range from 0 to 19, not 20.
    But that doesn't answer my question.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by volk
    But that doesn't answer my question.
    If you try to access an element outside its range your program might crash. If that's not an error, than what is .
    What errors do you get then? At compile time?
    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.

  5. #5
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Yeah.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)

    It's probably that damn EOF.

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    EOF = End of file

    not

    EOF = End of Array

  8. #8
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Unhappy

    The code works well with scanf but I don't want to use that C language function.

    I need to read more about the syntax of cin.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 12:14 PM
  2. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 09:16 AM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 03:57 PM
  5. multiple instances of cin
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2004, 05:51 PM

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