% illegal error?

This is a discussion on % illegal error? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I am having this error: Code: error C2296: '%' : illegal, left operand has type 'int *' What ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    % illegal error?

    Hi everyone,
    I am having this error:
    Code:
    error C2296: '%' : illegal, left operand has type 'int *'
    What does this mean?


    Code:
    //Write a function countEven(int*, int) which receives 
    //an integer array and its size, and returns the number 
    //of even numbers in the array.
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    int countEven(int* array, int n)
    {
    	for (int i = 0; i < n; ++i)
    	{
    		if (array % 2 == 0)   // <----ERROR!
    			cout << *array << " ";
    		else
    			continue;
    	}
    	return *array;
    }
    
    int main()
    {
    	int el = 0;
    	cout << "Enter the number of array elements: ";
    	cin >> el;
    
    	int* Array = new int[el]; //dynamic memory allocated
    	
    	cout << "Enter your array values: " << endl;
    	for (int i = 0; i < el; ++i)
    	{
    		cout << "[" << i << "]: ";
    		cin >> *(Array + i);
    	}
    	cout << "Your array values are: {";
    	for (int i = 0; i < el; ++i)
    	{
    		cout << *(Array + i) << ",";
    	}
    	cout << "}" << endl;
    
    	cout << "Your even number elements are: {";
    	countEven(Array, el);
    	cout << "}" << endl;
    
    	delete Array;
    	Array = 0;
    }
    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    What does this mean?
    It means that you are trying to compute the remainder of dividing an address by 2. You actually want to compute the remainder of dividing the integer at that address by 2 (or otherwise determine if the integer is even or odd).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Thanks for the quick reply and a very direct answer, laserlight..
    I modified it, however, it only outputs the first even element, and it outputs it three times. Why do you think that is?
    Thanks..


    Code:
    int countEven(int* array, int n)
    {
    	for (int i = 0; i < n; ++i)
    	{
    		if (array[i] % 2 == 0)
    			cout << *array << " ";
    		else
    			continue;
    	}
    	return *array;
    }
    
    int main()
    {
    	int el = 0;
    	cout << "Enter the number of array elements: ";
    	cin >> el;
    
    	int* Array = new int[el]; //dynamic memory allocated
    	
    	cout << "Enter your array values: " << endl;
    	for (int i = 0; i < el; ++i)
    	{
    		cout << "[" << i << "]: ";
    		cin >> *(Array + i);
    	}
    	cout << "Your array values are: {";
    	for (int i = 0; i < el; ++i)
    	{
    		cout << *(Array + i) << ",";
    	}
    	cout << "}" << endl;
    
    	cout << "Your even number elements are: {";
    	countEven(Array, el);
    	cout << "}" << endl;
    
    	delete Array;
    	Array = 0;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		if (array[i] % 2 == 0)
    			cout << *array << " ";
    The error is in the above. Can you spot it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    I modified it, however, it only outputs the first even element, and it outputs it three times. Why do you think that is?
    Notice that you are printing *array instead of array[i]. Note also that you are returning *array.

    By the way, the else continue is redundant. Oh, and remember to match new[] with delete[], not delete.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int countEven(int* array, int n)
    {
    ...
    	return *array;
    }
    Is the *array actually the number of even numbers?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Thanks, laserlight and matsp..
    I am still a little bit confused when it comes to pointers and actually, references too. Thanks for explaining the errors, laserlight. You're a fine teacher! Thanks!

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Hi matsp,
    Actually no, I got it all wrong, it was supposed to be the even elements. Pointer syntax still confuse me, thanks for your help..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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