Error invalid operands of types 'void' and 'int'...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SunDevil1171
    New Member
    • Oct 2014
    • 2

    Error invalid operands of types 'void' and 'int'...

    I making a simple single number scrambler/encryptor and as I tried to build the console application the following error
    occurred:

    error: invalid operands of types 'void' and 'int' to binary 'operator%'

    I have no idea what this means. The source code is bellow.

    Code:
    #include <iostream>
    #include <math.h>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        int nTimer;
        int nInput;
        int nRand;
        int nInRa;
    
        nTimer = 0;
        nRand = 1;
        nInRa = 0;
    
        cout << "Input a number" << endl;
        cin >> nInput;
    
        while(nTimer < 5){
            nInRa = srand(nRand)% 3 + 1;//error is on this line
    
            if(nInRa == 1){
                nInput = nInput + 1;
            }
            if(nInRa == 2){
                nInput = nInput + 2;
            }
            if(nInRa == 3){
                nInput = nInput + 3;
            }
            nTimer = nTimer + 1;
            nRand = nRand + 1;
    
    
        }
        cout << nInput;
        return 0;
    }
    I am running code::blocks as my IDE on Ubuntu.
    Thank you for any help!
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    srand doesn't return a value, but you're trying to assign it as though it did to nInRa. srand is your seed, which generally takes time(NULL) as its argument... include time.h or ctime

    you'd want rand to generate the "random" sequence.

    Code:
    srand((unsigned)time(NULL));
    
    nInRa = rand() % 3 + 1;

    Comment

    • SunDevil1171
      New Member
      • Oct 2014
      • 2

      #3
      Thank you so much I heard you could use time.h but I didnt know how!

      Comment

      Working...