#what does this error mean?

143 messages · Page 1 of 1 (latest)

viral ivy
#

trying to make a 10 digit random prime number generator:heres the full code,do criticise it im a very begginer

real shellBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#

@viral ivy

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

viral ivy
#
#include <cstdlib>
#include <ctime>
#include <iostream>
int nfunc(int){
 srand(time(0));
    return rand() % 999999999;

}
bool primvar(int n) {

  for (int i = 2; i <= n / 2; --i) {
    if (n % i == 0) {
      std::cout << "not prime";
      return true;
    } 
  }return false;
}
int main() {
  int primerand; 
  for(bool c=true;!c;){ 
  if (primvar(nfunc(int))){
    primerand=(nfunc(int));
    c=false;
               }}
  std::cout << primerand ;
  return 0;
}```
tame turtle
#

You're putting types where the function parameter should be

#

And ofc, doing srand(0) every call makes it not random

#

Srand(0) should be called once per program

viral ivy
reef kraken
tame turtle
#

Mb

reef kraken
#

you just have to wait a second for it to be random

tame turtle
#

Ah

#

Yeah

viral ivy
tame turtle
#

Idk

#

U dont even use the parameter

#

No idea why u put it there

viral ivy
#

but if its a void function it wont return anything?

reef kraken
#

for(bool c=true;!c;) think about this loop for a second

tame turtle
#

The (int)

#

Not return

viral ivy
tame turtle
#

I meant both

viral ivy
tame turtle
#

But in signature you put (int)

#

In the function declaration

viral ivy
tame turtle
#

int func(int)

#

The (int)

#

Not int but (int)

#

The thing in ()

viral ivy
#

i have misunderstood

#

lemme try something

#

ah wait,when calling the func with srand in main,will rand in the func still use srand? @tame turtle

tame turtle
#

When you call a function it executes code inside

#

Srand is a global function

#

Only one

viral ivy
tame turtle
#

And affects every rand()

viral ivy
#

i see

tame turtle
#

This is unreadable

viral ivy
#

its unreadable on purpose

tame turtle
#

It doesnt affect assembly much

viral ivy
#

worth the try

#

the pseudocode as well

viral ivy
#

everytime

tame turtle
#

Yeah

#

Compiler optimizes it

viral ivy
#

?

tame turtle
#

;asm -O2

int main(){
if(char var = 0){
    var = 4;
}
}
hollow tokenBOT
#
Assembly Output
main:
  xor eax, eax
  ret

viral ivy
#

this bot is very cool

tame turtle
#

As you see

#

Optimized away

#

No if exists

viral ivy
viral ivy
tame turtle
#

Because the condition is always 0

tame turtle
tame turtle
#

Only if you have else but then it'll still be as if the if doesnt exist

viral ivy
#

but the if will eventually?

#

how would the compiler know that

tame turtle
#

because its smart

#

Compilers do crazy optimizations

#

Anything goes as long as the program runs expectedly

viral ivy
#

;asm -O2

int main(){
int var =0;
if(var == 0){
    var = 4;
}
}```
hollow tokenBOT
#
Assembly Output
main:
  xor eax, eax
  ret

viral ivy
#

ah

#

but the if statement does execute here?

tame turtle
#

Where exactly

viral ivy
#

var is equal to 0

reef kraken
#

the program works as if it were executed in the way you expect

#

that is sufficient

tame turtle
#

If you want to know RE, learn asm

viral ivy
#

got it

viral ivy
devout sandal
#

It doesn't because you don't use it

#

Try printing it out

#

To your terminal

#

It will still look different on asm

#

But just see

tame turtle
#

;asm -O2

#include <iostream>
int main(){
int var =0;
if(var == 0){
    var = 4;
}
std::cout << var;
}```
hollow tokenBOT
#
Assembly Output
main:
  sub rsp, 8
  mov esi, 4
  mov edi, OFFSET FLAT:std::cout
  call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
  xor eax, eax
  add rsp, 8
  ret

tame turtle
#

Already optimizes it and just sets it to 4

#

Doesnt even set it

#

Just gives the 4 as param

viral ivy
#

cool,but in my program i do print it out

tame turtle
viral ivy
#

alright holdon

devout sandal
# hollow token

You could do something like
X = 10 * 5 + 10/2 or whatever and you will still get this

tame turtle
#

Yup

#

Since nothing is done

#

So compiler just says meh, it does nothing so lets scrap it all

devout sandal
#

If you try printing it it will also not calculate them as the compiler can pre calculate it and write a value to give

#

The actual behavior stays the same either way

#

Your program behaves as if it did do whatever step you wrote in your code

#

On anything above O0 ofc

tame turtle
#

Except one pesky

#

(-Ofast)

devout sandal
#

Gamer move

tame turtle
#

But yeah, as long as behavior remains the same, compiler can do whatever it wants

viral ivy
#

alright

#
int main() {                                     
  srand(time(0));                   
  int primerand;                                 
  for (bool c = true; !c;) {                     
    if (primvar(nfunc())) {                   
      primerand = (nfunc());                     
      c = false;                                 
    }                                            
    std::cout << c<<":bool is";                 
  }                                              
  std::cout << primerand << ": prime is" << std::
  return 0;                                      
}                                                
#

the bool is is probably unnecasarry

viral ivy
devout sandal
viral ivy
#

yes

#

and bool is doesnt orint

#

p*

devout sandal
#

Also not initializing variables

#

int primerand = 0;
Or primerand {} much preferred

viral ivy
devout sandal
#

Try to think of what your for loop condition does

#

C = true

#

And loop will only execute while c is not true

#

!c means c is not true

#

It's the same thing as
If c != true
continue loop

viral ivy
devout sandal
#

Different in what way

viral ivy
devout sandal
#

The primevar function might do something different, return different stuff but an if statement only does one thing
If(condition met)
Execute

viral ivy
#

wait,so is

      primerand = (nfunc());
      c = true;```
different from
```int randomNumber = nfunc(); 
    if (primvar(randomNumber)) { 
        primerand = randomNumber; ```
#

ah it is

#

well i got it to technically work

#

its just that it keeps printing 3/other singler digit prime nums

#

and it takes like 2 whole seconds

viral ivy
#

got it to work,thanks guys

#

i appreaciate that this server only hints at the answer most of the time

#

!solved