MOD operator not working right...

This is a discussion on MOD operator not working right... within the C++ Programming forums, part of the General Programming Boards category; Hello all.... I am writing a program that will work similar to Matlab (a math program) ... but, I can ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    19

    MOD operator not working right...

    Hello all.... I am writing a program that will work similar to Matlab (a math program) ... but, I can not get MOD to work right... can some one please explain how to make it work... here is my code...

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int getOperand(char ch); 
    int IsInList(char ch);
    
    char cVar[10]; 
    double dVar[10]; 
    int varNum; 
    
    int main ( )
    {
     char ch;
        char op;
        int op1;
        int op2;
        double res;
        varNum = 0;
    
        do 
        {
            cout << ">> "; 
            cin >> ch;
            if (ch == 'q') 
            {
                break;
            }
            
        op1 = getOperand(ch);
    
        cin >> ch;
        if(ch == '=')
        {
            cin >> dVar[op1];
            cout << cVar[op1] << " =\n\n";
            cout << "      " << dVar[op1] << "\n\n";
        }
        else
        {
            op = ch;
    
            cin >> ch;
            op2 = getOperand(ch);
    
            switch(op)
            {
            case '+':
                    res = dVar[op1] + dVar[op2];
                    cout << "Ans = \n\n"; 
                    cout << "       " << res << "\n\n";
                    break;
            case '*':
                    res = dVar[op1] * dVar[op2];
                    cout << "Ans = \n\n";
                    cout << "        " << res << "\n\n";
                    break;
            case '/':
                    res = dVar[op1]/dVar[op2];
                    cout << "Ans = \n\n";
                    cout << "        " << res << "\n\n";
                    break;
            case '-':
                    res = dVar[op1] - dVar[op2];
                    cout << "Ans = \n\n";
                    cout << "        " << res << "\n\n";
                    break;
            case 'mod':
                    res = MOD(dVar[op1],dVar[op2]);
                    cout << "Ans = \n\n";
                    cout << "        " << res << "\n\n";
                    break;
            }
        }
    
        }
        while (1); 
        
    }
    
    int getOperand(char ch) 
    {
        int iVar;
        iVar = IsInList(ch);
    
            if (iVar == -1)
            {
                cVar[varNum] = ch;
                iVar = varNum;
                varNum++;
            }
        
        return iVar; 
    }
    
    int IsInList(char ch) 
    {
        for (int i = 0; i < 10; i++)
        {
            if(ch == cVar[i]) 
            {
                return i;
            }
        }
    
        return -1;    
    }
    the "case 'mod':" is not working... can someone please review and tell me what i am doing wrong? Thanks.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    res = MOD(dVar[op1],dVar[op2]);
    Well, MOD is not a defined function. The mod operator is %. Try this:
    Code:
    res = dVar[op1] % dVar[op2];
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    change this:

    case 'mod':
    res = MOD(dVar[op1],dVar[op2]);

    to this:

    case '%'
    res = dVar[op1] % dVar[op2];

    The modulus operator, %, in C++ is built in, and is used like the +-/* operators are.

    using 'mod' as a case option won't work as case options have to resolve to an int. Since individual characters resolve to ints using the character set embedded in your compiler, using individual char will suffice, using a string, like mod, will not. If you want to refer to a literal string you should use "mod" not 'mod', but strings never resolve to an int.
    You're only born perfect.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    19
    Okay I changed it but, when I compile i get an error... it says "invalid operands of types 'double' and 'double' to binary'

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Aye. The modulus operator is not defined for real types, only integers.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can do mod on doubles by using fmod() from <cmath>

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    19
    Ok... so how do I fix it?
    This is my first time taking this class. I am not that good.
    Thanks.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    19
    disregard i got it thanks thantos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MOD function help
    By dimaash in forum C++ Programming
    Replies: 2
    Last Post: 04-19-2006, 01:57 AM
  2. Call Of Duty 2 Mod
    By Gusthelegend in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-12-2005, 06:35 AM
  3. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 04:21 AM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 08:47 PM
  5. Need programmers for multiplayer mod
    By ice cold in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 11-06-2004, 01:09 AM

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