samedi 25 février 2017

How to generate a random number between 1000 and -1000 c++?

I have to code guess the number game and I don't know how to generate the random number. The code that I have so far only generates negative numbers.

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

using namespace std;

int main(){

    cout<<"Alustame m2ngu"<<endl;

    srand(time(NULL));

    int N=rand()%1000+-1000, X, K=0;

    while(K<=11){

        cout<<"Sisesta uus arv vahemikus -1000 ja 1000:"<<endl;
        cin>>X;

        K++;

        if(X<N){

            cout<<"Peidetud arv on suurem"<<endl;

        }

        if(X>N){

            cout<<"Peidetud arv on v2iksem"<<endl;

        }

    }

    if(K<=11){

        cout<<"Peidetud arv on "<<N<<endl;

    }

    else{

        cout<<"Te kaotasite. Peidetud arv oli "<<N<<endl;

    }

    system("pause");

    return 0;

}




Aucun commentaire:

Enregistrer un commentaire