467,911 Members | 1,531 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,911 developers. It's quick & easy.

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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int nTimer;
  10.     int nInput;
  11.     int nRand;
  12.     int nInRa;
  13.  
  14.     nTimer = 0;
  15.     nRand = 1;
  16.     nInRa = 0;
  17.  
  18.     cout << "Input a number" << endl;
  19.     cin >> nInput;
  20.  
  21.     while(nTimer < 5){
  22.         nInRa = srand(nRand)% 3 + 1;//error is on this line
  23.  
  24.         if(nInRa == 1){
  25.             nInput = nInput + 1;
  26.         }
  27.         if(nInRa == 2){
  28.             nInput = nInput + 2;
  29.         }
  30.         if(nInRa == 3){
  31.             nInput = nInput + 3;
  32.         }
  33.         nTimer = nTimer + 1;
  34.         nRand = nRand + 1;
  35.  
  36.  
  37.     }
  38.     cout << nInput;
  39.     return 0;
  40. }
  41.  
I am running code::blocks as my IDE on Ubuntu.
Thank you for any help!
Oct 22 '14 #1

✓ answered by divideby0

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.

Expand|Select|Wrap|Line Numbers
  1. srand((unsigned)time(NULL));
  2.  
  3. nInRa = rand() % 3 + 1;
  4.  

  • viewed: 10405
Share:
2 Replies
128KB
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.

Expand|Select|Wrap|Line Numbers
  1. srand((unsigned)time(NULL));
  2.  
  3. nInRa = rand() % 3 + 1;
  4.  
Oct 22 '14 #2
Thank you so much I heard you could use time.h but I didnt know how!
Oct 22 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by muthu | last post: by
11 posts views Thread by Spiros Bousbouras | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.