Simple Error

This is a discussion on Simple Error within the C++ Programming forums, part of the General Programming Boards category; When I compile this, is gives me this error: C:\Documents and Settings\Brandon\Desktop\C++ Folder\final\Cpp1.cpp(38) : error C2297: '%' : illegal, right ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    6

    Simple Error

    When I compile this, is gives me this error: C:\Documents and Settings\Brandon\Desktop\C++ Folder\final\Cpp1.cpp(38) : error C2297: '%' : illegal, right operand has type 'double' . I have the error commented below inside of the int GetRandomNumber() function, but I simpy have no idea how to fix it. Any help will be greatly appreciated.


    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    int  GetRandomNumber();
    void ProgramDescription();
    void Menu();
    void Addition();
    void Subtraction();
    void Multiplication();
    void CorrectResponse(int t);
    void AskPlayAgain(bool & play);
    
    const double low = -20;
    const double high = 20;
    const int LOOP_LIMIT = 10;
    
    void main()
    {
    	bool play;
    
    	srand(time(0));
    
    	ProgramDescription();
    	do{
    		Menu();
    		AskPlayAgain(play);
    	}while (play==true);
    }
    
    
    //ERROR
    int GetRandomNumber()
    {
    	srand(time(0));
    	//ERROR IS HERE 
                    return low + rand()%(high - low + 1);
    }
    
    void ProgramDescription()
    {
    	cout << "This program will give a student ten questions of their choice:\n";
    	cout << "Addition, Subtraction, and Multiplication.\n";
    	cout << "Welcome to the Math Wizard.";
    }
    
    void Menu()
    {
    	int option;
    	cout << "Menu\n";
    	cout << "----\n";
    	cout << "1 - Addition\n";
    	cout << "2 - Subtraction\n";
    	cout << "3 - Multiplication\n";
    	cin  >> option;
    
    	while (option != 1 && option != 2 && option != 3)
    	{
    		cout << "Please pick option 1 - 3.\n";
    	}
    	if (option == 1)
    	{
    		Addition();
    	}
    	else if (option == 2)
    	{
    		Subtraction();
    	}
    	else if (option == 3)
    	{
    		Multiplication();
    	}
    }
    
    void Addition()
    {
    
    	int response;
    	int numbers;
    	int something;
    	int answer;
    	int number_1;
    	int number_2;
    	int counter;
    
    	for (numbers = 1; numbers = LOOP_LIMIT; numbers = counter + 1)
    	{
    		number_1 = GetRandomNumber();
    		number_2 = GetRandomNumber();
    		answer = number_1 + number_2;
    		cout << number_1 << " + " << number_2 << " = ";
    		cin  >> response;
    
    		srand(time(0));
    		while (response != answer)
    		{
    			something = 1 + rand() % (4 - 1 + 1);
    			switch (something)
    			{
    			case 1 : cout << "Nope, try again.\n";
    					 break;
    			case 2 : cout << "That is incorrect.\n";
    					 break;
    			case 3 : cout << "Not quite.\n";
    					 break;
    			case 4 : cout << "Come on you can do it.\n";
    					 break;
    			}
    			
    		cout << "Please try again.\n";
    		cin  >> response;
    		}
    		if(response == answer)
    		{
    			something = 1 + rand() % (4-1+1);
    			switch (something)
    			{
    			case 1 : cout << "Good Job!\n";
    					 break;
    			case 2 : cout << "Excellent!\n";
    					 break;
    			case 3 : cout << "Very Good!\n";
    					 break;
    			case 4 : cout << "Your're Good!\n";
    					 break;
    			}
    			cout << "You're ready for the next one.\n";
    		}
    	}
    }
    
    void Subtraction()
    {
    
    	int response;
    	int numbers;
    	int something;
    	int answer;
    	int number_1;
    	int number_2;
    	int counter;
    
    	for (numbers = 1; numbers = LOOP_LIMIT; numbers = counter + 1)
    	{
    		number_1 = GetRandomNumber();
    		number_2 = GetRandomNumber();
    		answer = number_1 - number_2;
    		cout << number_1 << " - " << number_2 << " = ";
    		cin  >> response;
    
    		srand(time(0));
    		while (response != answer)
    		{
    			something = 1 + rand() % (4 - 1 + 1);
    			switch (something)
    			{
    			case 1 : cout << "Nope, try again.\n";
    					 break;
    			case 2 : cout << "That is incorrect.\n";
    					 break;
    			case 3 : cout << "Not quite.\n";
    					 break;
    			case 4 : cout << "Come on you can do it.\n";
    					 break;
    			}
    			
    		cout << "Please try again.\n";
    		cin  >> response;
    		}
    		if(response == answer)
    		{
    			something = 1 + rand() % (4-1+1);
    			switch (something)
    			{
    			case 1 : cout << "Good Job!\n";
    					 break;
    			case 2 : cout << "Excellent!\n";
    					 break;
    			case 3 : cout << "Very Good!\n";
    					 break;
    			case 4 : cout << "Your're Good!\n";
    					 break;
    			}
    			cout << "You're ready for the next one.\n";
    		}
    	}
    }
    
    void Multiplication()
    {
    
    	int response;
    	int numbers;
    	int something;
    	int answer;
    	int number_1;
    	int number_2;
    	int counter;
    
    	for (numbers = 1; numbers = LOOP_LIMIT; numbers = counter + 1)
    	{
    		number_1 = GetRandomNumber();
    		number_2 = GetRandomNumber();
    		answer = number_1 * number_2;
    		cout << number_1 << " * " << number_2 << " = ";
    		cin  >> response;
    
    		srand(time(0));
    		while (response != answer)
    		{
    			something = 1 + rand() % (4 - 1 + 1);
    			switch (something)
    			{
    			case 1 : cout << "Nope, try again.\n";
    					 break;
    			case 2 : cout << "That is incorrect.\n";
    					 break;
    			case 3 : cout << "Not quite.\n";
    					 break;
    			case 4 : cout << "Come on you can do it.\n";
    					 break;
    			}
    			
    		cout << "Please try again.\n";
    		cin  >> response;
    		}
    		if(response == answer)
    		{
    			something = 1 + rand() % (4-1+1);
    			switch (something)
    			{
    			case 1 : cout << "Good Job!\n";
    					 break;
    			case 2 : cout << "Excellent!\n";
    					 break;
    			case 3 : cout << "Very Good!\n";
    					 break;
    			case 4 : cout << "Your're Good!\n";
    					 break;
    			}
    			cout << "You're ready for the next one.\n";
    		}
    	}
    }
    
    void AsPlayAgain(bool & play)
    {
    	char response;
    
    	cout << "Do you want more problems? (y or n)";
    	cin  >> response;
    
    	while (response != 'y' && response != 'Y' && response != 'n' && response != 'N')
    	{
    		cout << "Please enter y or n.";
    		cin  >> response;
    	}
    	if (response == 'y'  || response == 'Y')
    	{
    		play = true;
    	}
    	else
    	{
    		play = false;
    	}
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    82
    I don't have access to a compiler at the moment, so this is just a guess.

    It looks like the modulo (%) operator can't take doubles as parameters. Try changing high and low to ints.

    You might try finding a reference on the modulo operator, as I could be wrong on this (haven't actually used it since first year c++).

    Also a style note. If you break up long lines of statements, storing the results in temporary variables, the compiler can be more helpful in pointing out the exact line that is causing problems. There is a balance issue, as you don't want to make your code so long that it obscures the logic, but if you have an error on a long line, breaking it up can be useful.
    Last edited by AH_Tze; 01-20-2005 at 05:50 PM.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    82
    I checked the code, and the probelm was using the modulo on doubles. Just turn high and low into int's and it works more or less okay.

    You should do a bit of searching (like in the FAQ's) about headers and the format of main though. You should use the standard headers (no more '.h', and you have to add a 'c' to a few of them), and main() must return an int. I had to make these changes to get it to compile, but take a close look at the modified code:

    Code:
    
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int  GetRandomNumber();
    void ProgramDescription();
    void Menu();
    void Addition();
    void Subtraction();
    void Multiplication();
    void CorrectResponse(int t);
    void AskPlayAgain(bool & play);
    
    const int low = -20;
    const int high = 20;
    const int LOOP_LIMIT = 10;
    
    int main()
    {
    	bool play;
    
    	srand(time(0));
    
    	ProgramDescription();
    	do{
    		Menu();
    		AskPlayAgain(play);
    	}while (play==true);
    
            return 0;  // not strictly necessary, but never hurts to state it explicitly
    }

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yep... Modulo is only defined for integers (so, the intgeral types signed/unsigned char, int, short, long). It could be defined for a user defined floating point class, but it has been discussed before (search GD board), and really the problem is that it isn't entirely clear what it would return.

    It is well defined algebraically for all numbers (actually, converting them into classes of numbers), but that definition is useless as far as the computer is concerned (so, I won't go into the details). But then, you could define it based on some of the properties that is has when used with the integers, but you wouldn't be able to keep all of the properties.

    One interpretation is used by the standard <cmath> header in terms of the function fmod(double, double). http://cppreference.com/stdmath/

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 12:54 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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