• Register
1 vote
187 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 3 views
3 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 5.6k points
0 votes
1 answer 20 views
20 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
1 vote
1 answer 975 views
975 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 3 views
3 views
Problem: I have this following code: int M = 3; int C = 5; int match = 3; for ( int k =0; k < C; k ++ ) { match --; if ( match == 0 && k = M ) { std::cout << " equals" << std::endl; } } But it gives out an error saying: Error: expression must be a ... trying to modify "match" or "k" value here, but why this error? if I only write it like: if ( match == 0 ) it is ok. Could someone explain it to me?
asked Dec 23, 2020 Mashhoodch 5.6k points
1 vote
1 answer 47 views
47 views
Problem: I have a sample c++ code below that throws me back an error: &ldquo;c++ expression must be a modifiable value&rdquo; What could be the reason? int A = 3; int B = 5; int match = 3; for ( int k =0; k < B; k ++ ) { match --; if ( match == 0 && k = A ) { std::cout << " equals" << std::endl; } } Thanks for your help.
asked Mar 10, 2020 Gavin 15.3k points
0 votes
1 answer 19 views
19 views
I wonder if you could change the value of an external input variable that is treated as a function parameter, I remember seeing a way to do it but I don't remember very well. Suppose we use a function for the sum of two numbers, which has two parameters, the first ... of the sum =" << number << endl; The result of the sum should be stored in the variable. Would this procedure really work?
asked Oct 1, 2020 sasha 5.3k points
0 votes
1 answer 2 views
2 views
Problem: "Not all code paths return a value" means that inside a function that's supposed to explicitly return something why it's not void or a constructor/IEnumerator)?how compiler found a way to hit the end of the function without a return statement telling it what it's supposed to return?
asked Dec 9, 2020 Mashhoodch 5.6k points
0 votes
1 answer 58 views
58 views
Problem : A simple question for which I want to find the answer here. &ldquo;As parameters to a function, class objects can be passed by reference only.&rdquo; void myRefFunction(type myRefVariable) { } void main() {     myRefFunction(myRefVariable); } For a simple data ... object is passed rather than creating the copy and passing it. So question is does C++ pass the object by reference or value?
asked Jan 29, 2020 jwilliam 3.9k points
0 votes
1 answer 2 views
2 views
Problem: I run the following C codes and got a warning: control reaches end of non-void function int main(void) {}what does it mean? and if there is anything wrong that I havent noticed, please point it out
asked Dec 21, 2020 Mashhoodch 5.6k points
0 votes
1 answer 7 views
7 views
Problem: Why do we use int main in C++? How does a main function in C++ differ from main in C? Why void main is wrong? How to know the exit status of your program. That's the reason why you have the int main() -- you return your exit status. It's wrong because this is not what the C++ Standard specifies as a legal main . Nobody cares about what the other languages specify?
asked Dec 12, 2020 Mashhoodch 5.6k points