By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

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

P: 2
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.  

Share this Question
Share on Google+
2 Replies


100+
P: 131
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

P: 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.