Magic 8-Ball excercisse issue

When taking my code into Visual Studio Code to have my own version of the application, I’m getting 2 issues.

It wont let me use srand(time(NULL));

It first has an issue with ‘time’: “error: ‘time’ was not declared in this scope”

Next it has an issue with NULL : “error: invalid use of void expression”

In the 8-Ball Project we were given no indication that we’d have any issues with the random number seeding.

Full code for reference:

#include

#include

int main(){

//Your future is here

//Output opening phrase

std::cout<< “MAGIC 8-BALL: \n\n”;

srand(time(NULL));

int answer = std::rand() % 10;

switch(answer){

case 0:

 std::cout<<"100%\n";

 break;

case 1:

 std::cout<<"Probably.. yeah I'm gonna go with probably\n";

 break;  

case 2:

 std::cout<<"Emmm, sure yeah why not\n";

 break;

case 3:

 std::cout<<"Suuuuuuuuuuree...\n";

 break;

case 4:

 std::cout<<"Don't ask me now\n";

 break;

case 5:

 std::cout<<"I SAID DON'T ASK ME NOW\n";

 break;

case 6:

 std::cout<<"Haaahahahaha... no.\n";

 break;

 case 7:

 std::cout<<"Hmmm.. Dunno mate\n";

 break;

case 8:

 std::cout<<"Fat chance!\n";

 break;

 case 9:

 std::cout<<"No way\n";

 break;      

}

system(“pause”);

}

Hi!

From what I can tell it seems like you haven’t imported the correct things to use srand(time(NULL)), namely

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

But it could maybe be your compiler?

For more on this topic:

Remember when pasting your code you should press the </> before pasting so as to format it. It’s hard to tell what’s going on otherwise.

2 Likes

#include <time.h>

This fixed it :slight_smile: I had the other two (Don’t know why they didn’t paste in)

Thank you!!

1 Like

Welcome to the forums :slight_smile:

1 Like