Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not understand errors in basic c++ program

#include <iostream>

using namespace std;

int main(void) {
    int number, guess;

    srand(time(NULL));
    number = rand() % 101;

    cout << "Guess a number between 0-100: ";
    cin >> guess;

    if(number > guess) {
        cout << "The number is greater!\n";
    }
    else if(number < guess) {
        cout << "The number is smaller!\n";
    }
    else {
        cout << "Cognratulations! The number is "number"!\n";
    }
    cin-get();

    return 0;
}

error: 'srand' was not declared in this scope
error: 'rand' was not declared in this scope
error :expected ';' before 'number'
like image 795
Jonathan Dewein Avatar asked Dec 01 '25 08:12

Jonathan Dewein


1 Answers

You need to add:

#include <cstdlib>

to include srand() and rand()

When you need to use functions like this, looking at the man pages (or googling them) will tell you which header(s) you need to include.

http://www.cplusplus.com/reference/clibrary/cstdlib/

like image 66
Brian Roach Avatar answered Dec 04 '25 00:12

Brian Roach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!