Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to use an if statement that will take the information from the file and run each number through. I am unsure how to format it because I am using arrays. The error that I receive is "invalid types ‘int[int]’ for array subscript" for each if statement part.
Here is my full code. I know there are more errors in this code, but that's the part that is confusing me the most at this point. Thank you for any help.

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
 
using namespace std ;
 
int main () 
{
	string names [50] ;		// Elf's name
	int numToys [50] ;		// Number of toys made by each elf
	string rating [50] ;		// Rating for elves based on number of toys
	string line = " " ;
	ifstream ins ; 			
	ins.open ("elves.dat") ;
	int i = 0 ;
	istringstream iss (line) ;
	while (getline(ins, line))
	{
		istringstream iss (line) ;
		iss >> names [i] >> numToys [i] ;
		i++ ;
	for (int ii = 0 ; ii < 50 ; ii++)
	{
		cout << setw(0) << left << "Elves name:" << names[ii] ;
		cout << setw(10) << "Number of Toys Produced:" << numToys[ii] ;
		cout << setw(20) << "Rating:" << rating[ii] ;
	}
	cin.ignore () ;
	cout << names[0] ;
	}
}

void elfRating (int numToys, string&rating) 
{
	int t = 0 ;
	if (numToys [t] < 200)
	{
		rating = "-" ;
		t++ ;
	}
	else if (numToys [t] < 300)
	{
		rating = "*" ;
		t++ ;
	}
	else if (numToys [t] < 500)
	{
		rating = "***" ;
		t++ ;
	}
	else if (numToys [t] > 500)
	{
		rating = "*****" ;
		t++ ;
	}
	else
	{
		cout << "Error" << endl ;
	}
}


What I have tried:

I have tried establishing t and using t++ after each, but I don't think I did that part correctly.
Posted
Updated 4-Dec-17 20:13pm
Comments
Mohibur Rashid 4-Dec-17 21:43pm    
so, if the value of numToys[t] == 500 then it will output error?

in function you have declared numToys as integer but you are suing this as array, there is your error
Member 13479017 5-Dec-17 18:10pm    
whoops. Thank you, I fixed it.

1 solution

You have declared numtoys as an int, not an array - and you cannot index into an integer:
void elfRating (int numToys, string&rating) 
{
	int t = 0 ;
	if (numToys [t] < 200)
	{
But the solution is to pass the array index value to the function, not teh array:
void elfRating (int numToys, string&rating) 
{
	if (numToys  < 200)
	{
           ...
And to call that from your main code:
cout << setw(10) << "Number of Toys Produced:" << numToys[ii] ;
elfRating(numToys[ii], rating);
cout << setw(20) << "Rating:" << rating ;
 
Share this answer
 
Comments
CPallini 5-Dec-17 3:20am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900