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

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 15 views
15 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 game 4.3k points
1 vote
1 answer 632 views
632 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
1 vote
1 answer 28 views
28 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 Gavin 15.3k points
0 votes
1 answer 13 views
13 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 sasha 1.2k points
0 votes
1 answer 5 views
5 views
I have to read a file that has this format: Id: 23 Name: Roberto Last Name: My Last Name Id: 24 Name: Roberto2 Last Name: My Last Name2 And I have to read that file to be able to work with that data but I don't know how to read it. I have a struct created as follows: struct Data {      int Id;      string Name;      string Surname; }; How do I go about having that data in an array?
asked Aug 17 game 4.3k points