• Register
1 vote
403 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

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote
 
Best answer

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!

13 9 6
94,240 points
0 votes

Solution:

  int a = srand(time(NULL));

The prototype for srand is void srand(unsigned int) (provided you added <stdlib.h>).
This implies it returns nothing ..however you're using the value it returns (???) to assign, by initialization, to a.

Edit: this is what you require to do:

#include <stdlib.h> /* srand(), rand() */
#include <time.h>   /* time() */

#define ARRAY_SIZE 1024

void getdata(int arr[], int n)
{
    for (int i = 0; i < n; i++)
    {
        arr[i] = rand();
    }
}

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

error: void value not ignored as it ought to be

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

Example:

void myFunction()
{
   //...stuff...
}

int main()
{
   int myInt = myFunction(); //Compile error!

    return 0;
}

You aren't approved to assign void to integers, or any other type.

In the OP's situation:

int a = srand(time(NULL));

...is not approved srand(), according to the documentation, returns void.

    int a = srand(time(NULL))
        arr[i] = a;

Must be

 arr[i] = rand();

And put srand(time(NULL)) somewhere at the very starting of your program.

"void value not ignored as it ought to be" this error happens at the time function like srand(time(NULL)) does not return something and you are treating it as it is returning something. As in case of pop() function in queue ,in case you will store the popped element in a variable you will obtain the similar error since it does not return anything.

Check this line

result = getEdge(arr,toke);

You're attempting to capture the return value of a function for which the return type is void.

keep in mind, void is an incomplete type, and it's not compatible with any other type (in this instance, LHS is char), therefore the compiler properly complains.

That said,

*token = strtok(str, "_,");

Within the function also does not seem very accurate. Inspect the types there.

Here you are declaring a function without a return value:

void getEdge(char str[],char *token)

And here you are performing with a function which returns a char-value

char result;
result = getEdge(arr,toke);

So a solution could be to alter your function in:

char getEdge(char str[],char *token)
{
    ......
    return char_value;
}

printTemperature() is declared void (for example nothing returned) however it holds a return statement, and you've tried to assign the (void) value. You possibly imply to declare the function as 'float'.

10 6 4
31,120 points

Related questions

0 votes
1 answer 54 views
54 views
Problem: I have the following function : void getdata(int arr[], int n) { for (int i = 0; i < n; i++) { int a = srand(time(NULL)); arr[i] = a; } } And I call it in main: getdata(arr, 1024); I get "void value not ignored as it ought to be" but I don't understand what is wrong.Why do I get this error?
asked Dec 21, 2020 Mashhoodch 13k points
0 votes
1 answer 5 views
5 views
Problem: I get "void value not ignored as it ought to be" but I don't understand what is wrong. Why do I get this error?
asked Mar 15 ummesalma 22.8k points
0 votes
1 answer 2 views
2 views
Problem: I am unable to find out the solution of :error void value not ignored as it ought to be.Who can help me?
asked Apr 4 ashik 10.7k points
0 votes
1 answer 39 views
39 views
Problem: Hi there! I am confused in while loops and I need to write a program in which a value is taken as input from the user. If the value is less than 0, the program should increment it by 5 as long as the value remains less than 0. As the value becomes greater than or equal to 0, it should print the number of times the value has been incremented and the final value. Any help??
asked Jan 21 Code Learner 9.2k points
0 votes
1 answer 24 views
24 views
I find myself with a problem, I am not very clear about how an array with Strings is established and how it would be done to access each String and how it would be accessed each letter of one of the String. The concept would be the following: words = ... the array was selected, I need to go through the characters of the selected string and then modify those characters. How would it be programmed?
asked Aug 11, 2020 game 4.6k points
0 votes
1 answer 6 views
6 views
Problem: Can anyone explain me why this happened and can be the solution for it: Reference to overloaded function could not be resolved; did you mean to call it?
asked Mar 14 Wafa Abu Yousef 6.1k points
1 vote
1 answer 1.5K views
1.5K views
Problem : I am very new to the C++programming, and so I keep facing the below error saying: Reference to overloaded function could not be resolved; did you mean to call it? Below is my code that is causing me all sorts of troubles : #include <stdio.h> #include < ... have multiple files, all of the files fail to work. Can anyone explain me why my code is failing and can be the solution for it?
asked Dec 30, 2019 alecxe 7.5k points
0 votes
1 answer 1 view
1 view
Problem: Someone please suggest some solution>Why can't I pass a property or indexer as a ref parameter when .NET reflector shows that it's done in the .NET Framework?
asked 1 day ago Sana8989 8.5k points
0 votes
1 answer 2 views
2 views
Problem: I am trying to get the index of an element in a vector of strings, to use it as an index in another vector of int type, is this possible ? Please help me to fix this. Thanks.
asked Apr 2 Shovo210 13.2k points
0 votes
1 answer 2 views
2 views
Problem. Someone know why we write void in c++ code
asked 1 day ago Subhendu 4.1k points