• Register
+1 vote
5 views

Problem:

Being a novice in c++ I have a question for all of the experts here. “Void value not ignored as it ought to be c++” What does this line actually mean? I am trying to compile my program but it’s not compiling at all and gives that error.

Here is the error portion of my sample code below:

#define ARRAY_SIZE 1024
void getdata(int arr[], int a){
    for (int p = 0; p < a; p++) {
        int n = srand(time(NULL));
        arr[p] = n;
    }
}
int main(void){
//Do something
}

Thanks for your solution

ago by (4.8k points)  

1 Answer

0 votes

Solution:

This is a GCC error message that means the return value of a function is 'void', but that you are trying to assign it to a non-void variable.

Look at the line 

        int n = srand(time(NULL));

The prototype for srand is void srand(unsigned int).
This means it returns nothing ... but you're using the value it returns. That’s the reason you are getting the error.

You may give it a try:

#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 1024
void getdata(int arr[], int a){
    for (int p = 0; p < a; p++) {
        int n = srand(time(NULL));
        arr[p] = n;
    }
}

int main(void){
    int arr[ARRAY_SIZE];
    srand(time(0));
    getdata(arr, ARRAY_SIZE);
}

Good day!

ago by (13.9k points)  
...