locked
Help With Error message C2108 RRS feed

  • Question

  • #include <iostream>
    using namespace std;
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
    bool ReadFile(float leaves[],ifstream &fileinput,string &file);
    float findave(float leaves[], float &rsum);
    void sort(float leaves[], float total);
    void write(float ave, float leaves[], float total);
    
    int main()
    {
    	ifstream fileinput;	
    	float leaf_size[25], total=25;
    	string file;
    	bool results; 
    	cout<<"\n Please enter the full path of the File \n";
    	cin>>file;
    	results = ReadFile(leaf_size,fileinput,file);
    	float sum, ave;
    	ave = findave (leaf_size, sum);
    	write (ave, leaf_size, total);
    	return 0;
    }
    	bool ReadFile(float leaves[],ifstream &fileinput,string &file)
    	{
    	fileinput.open(file.c_str());
    	if(!fileinput)
    	{
    		cout<<"\n Sorry I Can't Read Your File \n";
    		return false;
    	}
    	else
    	{
    		cout<<"\n File has been opened, Good Job \n";
    	}
    	for(int i=0;i<25;i++)
    	{
    		fileinput>>leaves[i];
    		cout<<"\n"<<leaves[i]<<"\n";
    	}
    		return true;
    	}
    	float findave(float leaves[], float &rsum)
    	{
    		rsum = 0.000;
    		for (int i = 0; i< 25; ++i)
    		{
    			rsum=rsum+leaves[i];
    		}
    		return rsum/25;
    	}
    	void sort(float leaves[], float total)
    	{
    		float i ,j, temp;
    		
    
    
    		for (i=0; i < total-1; ++i)
    		{for(j=1; j<total; ++j)
    		{if (leaves[j-1] > leaves [j])
    		{ temp = leaves[j];
    		leaves[j] = leaves[j-1];
    		leaves[j-1]=temp;
    		}
    		}
    		}
    	}
    	void write(float ave, float leaves[], float total)
    	{
    		
    
    		cout << "\n The Average Leaf Size is: " << ave;
    		cout << "\n The 3 Largest Leaves Are: \n";
    		for (float i=0; i<3; ++i)
    			cout << "leaves["<<i<<"]"<<endl;
    		cout << "The 3 Smallest Leaves are: \n";
    		for (float i = total -3; i<total; ++i)
    			cout << "leaves["<<i<<"]"<<endl;
    	}
    I am getting an error message saying that 
    1>c:\users\thomas family\documents\visual studio 2010\projects\olea\olea\olea.cpp(62): error C2108: subscript is not of integral type
    1>c:\users\thomas family\documents\visual studio 2010\projects\olea\olea\olea.cpp(62): error C2108: subscript is not of integral type
    1>c:\users\thomas family\documents\visual studio 2010\projects\olea\olea\olea.cpp(63): error C2108: subscript is not of integral type
    1>c:\users\thomas family\documents\visual studio 2010\projects\olea\olea\olea.cpp(64): error C2108: subscript is not of integral type
    1>c:\users\thomas family\documents\visual studio 2010\projects\olea\olea\olea.cpp(64): error C2108: subscript is not of integral type
    1>c:\users\thomas family\documents\visual studio 2010\projects\olea\olea\olea.cpp(65): error C2108: subscript is not of integral type
    
    Is ther anybody that can tell me what I am doing wrong here? I am pretty new to this so go easy on me ;)
    Saturday, March 12, 2011 4:28 AM

Answers

  • >I guess I am unclear by what the verbage means when it
    >says that it is type integral.

    Integral numbers are whole numbers - they have no fractional
    or decimal parts. Floats (floating point numbers) and doubles
    (double precision floats) have decimal (fractional) parts.

    So int variables can only hold/represent whole numbers.
    1, 2, 88, 10099, -87, etc.

    Floats and doubles may hold numbers such as:
    1.5, 2.33, -89.23, etc.

    You can't use floats or doubles where the C/C++ language
    *requires* an integer type: int, unsigned, long, short,
    etc. If you try, you will get an error message.

    Indexes (subscripts) quite logically must be integral.
    Think about it - if you are using the index to select
    an element from an array, how could you possibly select
    *part* of an element?

    Given char array[10]; there are 10 chars in the array,
    indexed from 0-9. How could you possibly select element
    array[2.5]? How can you get half a char from the array?
    So logically the type used for the index *must* be an
    integer type.

    - Wayne
    • Marked as answer by RT1811 Saturday, March 12, 2011 4:20 PM
    Saturday, March 12, 2011 7:44 AM

All replies

  • RT1811 wrote:

    float i ,j, temp;
    {if (leaves[j-1] > leaves [j])

    1>c:\users\thomas family\documents\visual studio  2010\projects\olea\olea\olea.cpp(62): error C2108: subscript is not of  integral
    type

    "Subscript" is the expression inside square brackets. Which type is it  of?


    Igor Tandetnik

    Saturday, March 12, 2011 4:40 AM
  • Basically the program is asking for 25 number with 3 decimal places values from a txt file, reading them to the screen. Calculating the average, sorting them, and the output should be the largest 3 numbers and smallest 3.

    My output looks kinda like this when I remove the leaves[i] from the end of the lines in the write function:

     

    All Values listed here in a list

    The Average is displayed next

    The 3 Largest Leaves are:

    leaves[0]

    leaves[1]    ****** This is where I am trying to put the values which are not showing up. Is it a problem with the sorting array???****

    leaves[2]

    The 3 smallest leaves are:

    leaves [22]

    leaves [23]

    leaves [24]

     

     

    IDK if that answers your question or not!! Thanks man for helping me out

     

     

     

    Saturday, March 12, 2011 5:07 AM
  • RT1811 wrote:

    IDK if that answers your question or not!! Thanks man for helping me  out

    I didn't ask that question to satisfy my curiosity. I asked it to give  you a hint. Let me try again, more explicitly.

    You have something like this in your program:

    float i;
    i = 0;
    if (leaves[i]) ...

    In this fragment, what expression appears in the square brackets (also  known as a "subscript")? What is the type of this expression? Is this  type integral? When the compiler says "subscript is not of integral  type", what is it complaining about, in your opinion?


    Igor Tandetnik

    Saturday, March 12, 2011 5:27 AM
  • This forum is very strange. I created a user profile and now the link takes me to sign in with my student email???

     

    Anyways... I KNOW you are trying to help and not just satisfy your curiosity.  Here is what I see... the subscript [i] is the value of the array as it is being presented in that part of the function right?? I guess I am unclear by what the verbage means when it says that it is type integral.

     

    Here is what I have corrected so far...

     

    in the sort funct I have changed the I and j to long int... that cleared most of the errors. 

     

    There are still 2 errors in the write functions.. let me show you... it helps me explain and learn.

     

    void write(float ave, float leaves[], float total)

    {

    cout << "\n The Average Leaf Size is: " << ave;

    cout << "\n The 3 Largest Leaves Are: \n";

    for (float i=0; i<3; ++i)

    cout<<"Leaf["<<i<<"]" <<leaves[i]<<endl;   ****  Just before the endl; the leaves[i] is giving me an error. I am confused here. Integral???? It is the current value of the largest 3 leaves right?

    cout << "The 3 Smallest Leaves are: \n";

    for (float i = total -3; i<total; ++i)

    cout << "leaves["<<i<<"]"<<leaves[i]<< endl;

    }

    Saturday, March 12, 2011 5:47 AM
  • >I guess I am unclear by what the verbage means when it
    >says that it is type integral.

    Integral numbers are whole numbers - they have no fractional
    or decimal parts. Floats (floating point numbers) and doubles
    (double precision floats) have decimal (fractional) parts.

    So int variables can only hold/represent whole numbers.
    1, 2, 88, 10099, -87, etc.

    Floats and doubles may hold numbers such as:
    1.5, 2.33, -89.23, etc.

    You can't use floats or doubles where the C/C++ language
    *requires* an integer type: int, unsigned, long, short,
    etc. If you try, you will get an error message.

    Indexes (subscripts) quite logically must be integral.
    Think about it - if you are using the index to select
    an element from an array, how could you possibly select
    *part* of an element?

    Given char array[10]; there are 10 chars in the array,
    indexed from 0-9. How could you possibly select element
    array[2.5]? How can you get half a char from the array?
    So logically the type used for the index *must* be an
    integer type.

    - Wayne
    • Marked as answer by RT1811 Saturday, March 12, 2011 4:20 PM
    Saturday, March 12, 2011 7:44 AM
  • Awesome, thanks for clearing that up.

    Just one problem though. The assignment states that I am supposed to hold the decimal place! How do I do this if I have to use a type  int? The txt file I read from has 25 values in a list with 3 decimal places. I am to read the file with a readfile function (done), return a boolean indicating file open/read status (done), Pass the array to a findave function and return the average (done), sort the array using a sort function, (it gives an example of something I am supposed to modify which is similar to what I have here), send the array and average to the write functthat writes the average three largest and three smallest (attempted and failed, well... the average works ). 

     

    Given what you guys are saying I cannot pass the decimal places. . How am I supposed to sort these numbers and pass the three smallest using only type int as my understanding is that it does not hold decimal places?

    I know I am a pain in the rear, and thanks for your help!!

     

     

    Saturday, March 12, 2011 3:00 PM
  • RT1811 wrote:

    Awesome, thanks for clearing that up.

    Just one problem though. The assignment states that I am supposed to  hold the decimal place! How do I do this if I have to use a
    type int?

    The values of the array elements can very well be float or double. The  indexes into the array must be int. Do you understand the difference  between the value of an array's element, and its index?

    // This is OK
    int a[10];
    float b[10];
    a[1] = 42;
    b[2] = 4.2;

    int i = 3;
    a[i] = 10;
    b[i] = 20;

    // This is not OK - index must have integral type
    a[4.2] = 1;
    float i = 2.5;
    a[i] = 2;

    // This is not OK - even though the value of the index is a whole  number, the type is wrong.
    float j = 5;
    a[j] = 3;

    I don't know how to make it any clearer.


    Igor Tandetnik

    Saturday, March 12, 2011 4:07 PM