Cannot convert from 'int' to 'int []' error

This is a discussion on Cannot convert from 'int' to 'int []' error within the C++ Programming forums, part of the General Programming Boards category; I'm currently writing a program that reads information from an already created file, and displays the information in an array. ...

  1. #1
    Registered User fjf314's Avatar
    Join Date
    Jan 2004
    Posts
    13

    Cannot convert from 'int' to 'int []' error

    I'm currently writing a program that reads information from an already created file, and displays the information in an array. I created one function that takes the data from the file and places it in the array. I created a second function to output the array. However, when I place my function to output the the array within my int main(), I get the following error:

    error C2664: 'display_array' : cannot convert parameter 1 from 'int' to 'int []'

    This might be something simple, but I'm new with arrays. Here is my code:
    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<fstream.h>
    
    
    void program_description();
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items );
    void display_array( int arr[], int no_items );
    
    const int max_list = 50;
    
    int main()
    {
    	//Variable Declarations
    	int number;
    	int no_values;
    	int array[max_list];
    	ifstream in_file;
    
    	no_values = 0;
    
    	program_description();
    
    	display_array( array[max_list], no_values );
    
    
    	return 0;
    }
    
    //Function Definitions
    
    void program_description()
    /*=======================================================================================
    This function outputs a description of the program to the user.
    	INPUT: None
    	OUTPUT: None
    =======================================================================================*/
    {
    	cout << "Greetings!  This program is designed to take the ";
    	cout << "data\ncurrently stored in another file and display it to you.  It will then ";
    	cout << "sort the data, number it, and calculate the sum, range, mean, and median.  ";
    	cout << "From there a\nhistogram of the data is displayed.\n\n";
    }
    
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items )
    /*=======================================================================================
    This function opens the input stream, gets the data from the file one item at a time, and 
    stores each item in an array element.  It also counts the number of items in the file.  
    Lastly, the input stream is closed.
    	INPUT: A parameter of type ifstream, an array of integers, an integer which
    			represents the number of items in the file.
    	OUTPUT: This function returns nothing.
    =======================================================================================*/
    {
    	int data;
    	no_items = 0;
    
    	i_file.open("stat_data");
    	i_file >> data;
    	arr[no_items] = data;
    	no_items ++;
    
    	while ( ! i_file.eof() );
    	{
    		i_file >> data;
    		arr[no_items] = data;
    		no_items ++;
    	}
    }
    
    void display_array( int arr[], int no_items )
    /*=======================================================================================
    The function traverses the array of integers, outputting each element in the array.
    	INPUT: an array of integers, an integer which represents the number of items in
    			the file.
    	OUTPUT: This function returns nothing.
    =======================================================================================*/
    {
    	int no_scores;
    	no_scores = 0;
    	
    	for ( int i = 0; i < no_scores; i++ )
    	{
    		cout << arr[i] << " ";
    	}
    }
    Programming With: Microsoft Visual C++ 6.0

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    When you pass the array to a function, do it like this.
    Code:
    display_array( array, max_list, no_values );
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User fjf314's Avatar
    Join Date
    Jan 2004
    Posts
    13
    When I do that, then, how should I go about changing my function? If I leave my function as is, then I'll get an error on having three parameters.
    Programming With: Microsoft Visual C++ 6.0

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    something like this for the prototype and definition
    Code:
    void display_array( int arr[], int max, int no_items )
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User fjf314's Avatar
    Join Date
    Jan 2004
    Posts
    13
    Well, the error problem is solved, but the program itself doesn't seem to be working... Are there any other apparent errors?

    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<fstream.h>
    
    
    void program_description();
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items );
    void display_array( int arr[], int max, int no_items );
    
    const int max_list = 50;
    
    int main()
    {
    	//Variable Declarations
    	int number;
    	int no_values;
    	int array[max_list];
    	ifstream in_file;
    
    	no_values = 0;
    
    	program_description();
    
    	display_array( array, max_list, no_values );
    
    
    	return 0;
    }
    
    //Function Definitions
    
    void program_description()
    /*========================================================================
    This function outputs a description of the program to the user.
    	INPUT: None
    	OUTPUT: None
    ========================================================================*/
    {
    	cout << "Greetings!  This program is designed to take the ";
    	cout << "data currently stored in\nanother file and display it to you.  It will then ";
    	cout << "sort the data, number it,\nand calculate the sum, range, mean, and median.  ";
    	cout << "From there a\nhistogram of the data is displayed.\n\n";
    }
    
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items )
    /*========================================================================
    This function opens the input stream, gets the data from the file one item at a time, and 
    stores each item in an array element.  It also counts the number of items in the file.  
    Lastly, the input stream is closed.
    	INPUT: A parameter of type ifstream, an array of integers, an integer which
    			represents the number of items in the file.
    	OUTPUT: This function returns nothing.
    ========================================================================*/
    {
    	int data;
    	no_items = 0;
    
    	i_file.open("stat_data");
    	i_file >> data;
    	arr[no_items] = data;
    	no_items ++;
    
    	while ( ! i_file.eof() );
    	{
    		i_file >> data;
    		arr[no_items] = data;
    		no_items ++;
    	}
    }
    
    void display_array( int arr[], int max, int no_items )
    /*========================================================================
    The function traverses the array of integers, outputting each element in the array.
    	INPUT: an array of integers, an integer which represents the number of items in
    			the file.
    	OUTPUT: This function returns nothing.
    ========================================================================*/
    {
    	int no_scores;
    	no_scores = 0;
    	
    	for ( int i = 0; i < no_scores; i++ )
    	{
    		cout << arr[i] << " ";
    	}
    }
    Programming With: Microsoft Visual C++ 6.0

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    void program_description();
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items );
    I suppose you want to call those functions from main().
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > program_description();

    > display_array( array, max_list, no_values );

    As Alphaoide said, you need to call get_info_from_file() here:
    program_description();
    get_info_from_file( in_file, array, no_values );
    display_array( array, max_list, no_values );
    Code:
    void display_array( int arr[], int max, int no_items )
    / *=================================================
    =======================
    The function traverses the array of integers, outputting each element in the array.
    	INPUT: an array of integers, an integer which represents the number of items in
    			the file.
    	OUTPUT: This function returns nothing.
     ==================================================
    ======================*/
    {
    	int no_scores;
    	no_scores = 0;
    	
    	for ( int i = 0; i < no_scores; i++ )
    	{
    		cout << arr[i] << " ";
    	}
    }
    This should be:
    Code:
    void display_array( int arr[], int no_items )
    / *=================================================
    =======================
    The function traverses the array of integers, outputting each element in the array.
    	INPUT: an array of integers, an integer which represents the number of items in
    			the file.
    	OUTPUT: This function returns nothing.
     ==================================================
    ======================*/
    {
    	for ( int i = 0; i < no_items; i++ )
    	{
    		cout << arr[i] << " ";
    	}
    }
    Last edited by swoopy; 01-13-2004 at 06:52 PM.

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by alphaoide
    something like this for the prototype and definition
    Code:
    void display_array( int arr[], int max_list, int no_items )
    Sorry, but I missed it. In your program max_list is a global variable so you don't actually need to pass it.
    Code:
    void display_array( int arr[], int no_items )
    But, you should've known that, though
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  9. #9
    Registered User fjf314's Avatar
    Join Date
    Jan 2004
    Posts
    13
    Well, I was able to get my program running at school today. I've started to add some more things, but now I've noticed that my array is acting up. It's repeating the last number in my stat_data file twice. So if the numbers in my file are: 98, 54, 76, 46, and 78 my program outputs: 98, 54, 76, 46, 78, and 78. Sorry for all of the pleas for help, but this is really getting frustrating. Here's my code:

    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<fstream.h>
    
    
    void program_description();
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items );
    void display_array( int arr[], int no_items );
    void sort_ascending( int arr[], int no_items );
    int get_total( int arr[], int no_items );
    int calc_range( int arr[], int no_items );
    
    const int max_list = 50;
    
    int main()
    {
    	//Variable Declarations
    	int no_values;
    	int array[max_list];
    	ifstream in_file;
    
    	no_values = 0;
    
    	program_description();
    
    	get_info_from_file( in_file, array, no_values );
    
    	display_array( array, no_values );
    
    	sort_ascending( array, no_values );
    
    	get_total( array, no_values );
    
    
    	return 0;
    }
    
    //Function Definitions
    
    void program_description()
    /*========================================================================
    This function outputs a description of the program to the user.
    	INPUT: None
    	OUTPUT: None
    ========================================================================*/
    {
    	cout << "Greetings!  This program is designed to take the ";
    	cout << "data currently stored in\nanother file and display it to you.  It will then ";
    	cout << "sort the data, number it,\nand calculate the sum, range, mean, and median.  ";
    	cout << "From there a histogram\nof the data is displayed.\n\n";
    }
    
    void get_info_from_file( ifstream &i_file, int arr[], int &no_items )
    /*========================================================================
    This function opens the input stream, gets the data from the file one item at a time, and 
    stores each item in an array element.  It also counts the number of items in the file.  
    Lastly, the input stream is closed.
    	INPUT: A parameter of type ifstream, an array of integers, an integer which
    			represents the number of items in the file.
    	OUTPUT: This function returns nothing.
    ========================================================================*/
    {
    	int data;
    	no_items = 0;
    
    	i_file.open("stat_data");
    	i_file >> data;
    	arr[no_items] = data;
    	no_items ++;
    	
    	
    	while ( ! i_file.eof() )
    	{
    		i_file >> data;
    		arr[no_items] = data;
    		no_items ++;
    	}
    
    	i_file.close();
    
    	cout << "The number of items in this array is: " << no_items;
    	cout << endl << endl;
    
    	cout << "The array is:\n\n";
    
    	
    }
    
    void display_array( int arr[], int no_items )
    /*========================================================================
    The function traverses the array of integers, outputting each element in the array.
    	INPUT: an array of integers, an integer which represents the number of items in
    			the file.
    	OUTPUT: This function returns nothing.
    ========================================================================*/
    {
    	for ( int i = 0; i < no_items; i++ )
    	{
    		cout << arr[i] << " ";
    	}
    
    	cout << endl << endl;
    }
    
    void sort_ascending( int arr[], int no_items )
    /*========================================================================
    This function sorts an array of integers in ascending order using a selection sort.
    	INPUT: an array of integers, an integer which represents the number of items.
    	OUPUT: This function returns nothing.
    ========================================================================*/
    {
    	int i, hold;
    
    	cout << "This is the array sorted in ascending order: " << endl << endl;
    	
    	for ( int pass = 0; pass < no_items - 1; pass++ )
    
    		for ( i = 0; i < no_items - 1; i++ )
    
    			if (arr[i] > arr[i + 1] )
    			{
    				hold = arr[i];
    				arr[i] = arr[i +1];
    				arr[i+1] = hold;
    			}
    
    	for ( i = 0; i < no_items; i++ )
    		cout << arr[i] << " ";
    		cout << endl;
    
    	cout << endl << endl;
    
    }
    
    int get_total( int arr[], int no_items )
    /*========================================================================
    This function traverses the array of integers and totals the elements in the array.
    	INPUT: an array of integers, an integer which represents the number of items 
    			in the file
    	OUPUT: This function returns an integer which represents the total of all of
    			the elements.
    ========================================================================*/
    {
    	int sum;
    
    	sum = 0;
    
    	for ( int i = 0; i < no_items; i++ )
    	{
    		sum = arr[i]+sum;
    	}
    
    	cout << "The sum of all the data is: " << sum << endl << endl;
    
    	return sum;
    }
    Programming With: Microsoft Visual C++ 6.0

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >	
    >	while ( ! i_file.eof() )
    >	{
    >		i_file >> data;
    >		arr[no_items] = data;
    >		no_items ++;
    >	}
    eof isn't seen until a read is attempted at the end of file (see the Programming FAQ), so you can either use:
    Code:
    	while ( i_file >> data )
    	{
    		arr[no_items] = data;
    		no_items ++;
    	}
    Or something like:
    Code:
    	while ( ! i_file.eof() )
    	{
    		i_file >> data;
    		if ( !i_file.eof() )
    		{
    			arr[no_items] = data;
    			no_items ++;
    		}
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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